iTextSharp PDF 打印

发布于 2024-11-09 04:26:09 字数 2170 浏览 0 评论 0原文

我正在尝试创建一种方法,将 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;
    }
}

I'm trying to create a method that will send a PDF file directly to my printer (causing the print dialog to appear).

Below is the code I've been working on - most of it found in the forums here. It works fine if I use iTextSharp to create a new PDF document, but as soon as I try to inject some JavaScript into an existing file, I get an exception when calling the print() method saying

Object doesn't support property or method '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 file

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法少女 2024-11-16 04:26:09

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

我希望它有所帮助!

I found a way to do this here: http://wskidmore.com/2011/03/pdf-initial-view-settings-itextsharp/

Based on that, I wrote this code:

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);
}

I hope it helps!

穿越时光隧道 2024-11-16 04:26:09

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

You want to add Javascript to the PDF to open the print dialog, not the web page (unless you really do want the web page print dialog, not the PDF print dialog). I have done this before, but not with iTextSharp; but a quick Google search should tell you how to add Javascript using iTextSharp to open the print dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文