2011-05-23 199 views
4

我試圖創建將直接發送PDF文件到我的打印機的方法(導致出現打印對話框)。iTextSharp的PDF打印

下面是我一直在努力的代碼 - 大部分在論壇here找到。如果我使用iTextSharp的創建一個新的PDF文檔,但只要我嘗試一些JavaScript注入到一個已經存在的文件,我調用print()方法說

對象不支持的時候得到一個例外,它工作正常屬性或方法 '打印'

<script type="text/javascript"> 
    function load() { 
     try { 
      var x = document.getElementById("frame1"); 
      x.print(); 
     } 
     catch (err) { 
     } 
    } 
</script> 

<body onload="load();"> 
    <form id="form1" runat="server"> 
    <div> 
     <iframe id="frame1" src="C:/1686850_1.pdf" runat="server" frameborder="0" style="height: 0px; width: 0px;" /> 
    </div> 
    </form> 
</body> 
</html> 

.CS文件

using System.IO; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

public partial class Print : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     SetPDF(File.ReadAllBytes("C:\\1686850.pdf"), "C:\\1686850_1.pdf"); //test files 
    } 

    private void SetPDF(byte[] file, string outputPath) 
    { 
     PdfReader reader = new PdfReader(file); 
     int pageCount = reader.NumberOfPages; 
     Rectangle pageSize = reader.GetPageSize(1); 

     Document pdf = new Document(pageSize); 
     PdfWriter writer = PdfWriter.GetInstance(pdf, new FileStream(outputPath, FileMode.Create)); 
     pdf.Open(); 

     //This action leads directly to printer dialogue 
     PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer); 
     writer.AddJavaScript(jAction); 
     //Omitting this loop and simply adding some text to the file produces the behavior I want. 
     for (int i = 0; i < pageCount; i++) 
     { 
      pdf.NewPage(); 
      PdfImportedPage page = writer.GetImportedPage(reader, i + 1); 
      writer.DirectContent.AddTemplate(page, 0, 0); 
     } 
     pdf.Close(); 

     //Open the pdf in the frame 
     frame1.Attributes["src"] = outputPath; 
    } 
} 

回答

9

我找到了一種方法,在這裏做這樣的:http://wskidmore.com/2011/03/pdf-initial-view-settings-itextsharp/

此基礎上,我寫了這個代碼:

private void PrintMenu() 
{ 
    ... 
    var notUriPath = Server.MapPath("~/" + filePathName); 

    var doc = new Document(); 
    var reader = new PdfReader(notUriPath); 
    using (var memoryStream = new MemoryStream()) 
    { 
     var writer = PdfWriter.GetInstance(doc, memoryStream); 
     doc.Open(); 

     // this action leads directly to printer dialogue 
     var jAction = PdfAction.JavaScript("this.print(true);\r", writer); 
     writer.AddJavaScript(jAction); 

     var cb = writer.DirectContent; 
     doc.AddDocListener(writer); 

     for (var p = 1; p <= reader.NumberOfPages; p++) 
     { 
      doc.SetPageSize(reader.GetPageSize(p)); 
      doc.NewPage(); 
      var page = writer.GetImportedPage(reader, p); 
      var rot = reader.GetPageRotation(p); 
      if (rot == 90 || rot == 270) 
       cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height); 
      else 
       cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0); 
     } 

     reader.Close(); 
     doc.Close(); 
     File.WriteAllBytes(notUriPath, memoryStream.ToArray()); 
    } 

    theIframeForPrint.Attributes.Add("src", fullFilePath); 
} 

我希望它能幫助!

+1

感謝的人,它的工作原理..我複製此代碼'//這個動作直接導致打印機對話 VAR jAction = PdfAction.JavaScript (「this.print(true); \ r」,writer); writer.AddJavaScript(jAction);' – 2015-04-23 08:40:47

0

你想添加JavaScript到PDF打開打印d ialog,而不是網頁(除非你真的想要的網頁打印對話框,而不是PDF打印對話框)。我之前做過這個,但是沒有使用iTextSharp;但是快速的Google搜索應該告訴你如何使用iTextSharp添加Javascript來打開打印對話框。

+0

我認爲這是我在做什麼? :)我的目標,真的,是打印從我的網頁上的PDF文檔,而無需使用PDF閱讀器 – foamy 2011-05-24 06:39:50