要下載的pdf文件,使用iTextSharp的嘗試此解決方案:如果您想直接保存到一個文件
private MemoryStream createPDF(string html)
{
MemoryStream msOutput = new MemoryStream();
TextReader reader = new StringReader(html);
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c:\\my.pdf", FileMode.Create));
// step 3: we create a worker parse the document
HTMLWorker worker = new HTMLWorker(document);
// step 4: we open document and start the worker on the document
document.Open();
worker.StartDocument();
// step 5: parse the html into the document
worker.Parse(reader);
// step 6: close the document and the worker
worker.EndDocument();
worker.Close();
document.Close();
return msOutput;
}
一旦PDF設置 ITextSharp HTML to PDF?
除非一個替換,嘗試ghostscript打印一頁: Print existing PDF (or other files) in C#
如果你啓動一個shell執行該進程,你可以使用命令行參數:
gsprint "filename.pdf" -from 1 - to 1
另外,web瀏覽器可以直接打印完整的網頁:http://msdn.microsoft.com/en-us/library/b0wes9a3.aspx
我找不到任何引用該web瀏覽器本身就可以打印出「從第一頁X到Y」不打印對話框。
由於我面臨着類似的問題,這裏是一個替代的解決方案:
這個開源項目變成HTML文件類似iTextSharp的(http://code.google.com/p/wkhtmltopdf/)的PDF文檔。我們最終沒有使用iTextSharp,因爲我們想要打印的網站的佈局方式存在幾個格式問題。我們發送命令行參數來將使用webclient下載的html轉換爲pdf文件。
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultNetworkCredentials;
string htmlText = wc.DownloadString("http://websitehere.com);
說完,轉身後的PDF格式,你可以簡單地打印文件:
Process p = new Process();
p.StartInfo.FileName = string.Format("{0}.pdf", fileLocation);
p.StartInfo.Verb = "Print";
p.Start();
p.WaitForExit();
(道歉C#,我比VB.NET再熟悉不過了,但它應該是一個簡單轉換)