我正在尝试创建一种将PDF文件直接发送到打印机的方法(导致出现打印对话框)。

下面是我一直在努力的代码-大部分都找到了在这里的论坛上。如果我使用iTextSharp创建新的PDF文档,效果很好,但是当我尝试向现有文件中注入一些JavaScript时,调用print()方法说


对象不支持属性或方法'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;
    }
}


#1 楼

我在这里找到了一种方法: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);
}


希望对您有所帮助!

评论


谢谢,它有效。.我复制了这段代码// //此操作直接导致打印机对话var jAction = PdfAction.JavaScript(“ this.print(true); \ r”,writer); writer.AddJavaScript(jAction);

– Dyrandz Famador
15年4月23日在8:40

#2 楼

您想将Javascript添加到PDF中以打开打印对话框,而不是网页(除非您确实确实想要网页打印对话框,而不是PDF打印对话框)。我之前已经做过,但是没有使用iTextSharp。但是Google的快速搜索应该可以告诉您如何使用iTextSharp打开打印对话框来添加Javascript。

评论


我以为那是我在做什么? :)实际上,我的目标是在不使用PDF阅读器的情况下从我的网页打印PDF文档

–泡沫
2011年5月24日6:39