使用 iTextSharp 复制 PDF 表单

发布于 2024-11-06 10:00:08 字数 1943 浏览 6 评论 0原文

我成功地使用 iTextSharp 读取带有表单的 PDF,填写表单上的字段,然后将其写回到客户端。我现在收到一个要求,如果某些页面全是空白,则应将其删除(出于问题的目的,我可以检查布尔变量以了解是否需要删除这些页面。我的理解是这样做在 iTextSharp 中,您实际上是将 PDF 从一个复制到另一个,并省略了要删除的页面

我可以进行此操作,但我丢失了复制的 PDF 上的表单;即在尝试复制之前没有写出任何值。 PDF“删除”一些页面,值被正确写入。

当我复制表单时,如何保留已经创建的 PDF 表单,或者是否有更好的方法删除页面?文件但未填写表单(可能是因为复制时未保留表单):

    string file = "output.pdf";
    PdfReader reader = new PdfReader("template.pdf");

    Document doc = new Document();
    using (MemoryStream ms = new MemoryStream())
    {

        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
        doc.Open();
        doc.AddDocListener(writer);
        for (int i = 1; i <= reader.NumberOfPages; i++)
        {
            bool skipPage = this.SkipPage; // some nifty logic here
            if (skipPage)
                continue;

            doc.SetPageSize(reader.GetPageSize(i));
            doc.NewPage();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage page = writer.GetImportedPage(reader, i);
            int rotation = reader.GetPageRotation(i);
            if (rotation == 90 || rotation == 270)
                cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height);
            else
                cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
        }
        reader.Close();
        doc.Close();
        using (FileStream fs = new FileStream(file, FileMode.Create))
        {
            // this is the part stumping me; I need to use a PdfStamper to write 
            // out some values to fields on the form AFTER the pages are removed.
            // This works, but there doesn't seem to be a form on the copied page...
            this.stamper = new PdfStamper(new PdfReader(ms.ToArray()), fs);
            // write out fields here...
            stamper.FormFlattening = true;
            stamper.SetFullCompression();
            stamper.Close();
        }
    }

I am successfully using iTextSharp to read in a PDF with a form, fill out fields on the form, and write it back out to the client. I've now gotten a requirement that certain pages should be removed if they're all blank (for purposes of the question, I can check a boolean variable to know whether or not I need to remove the pages. My understanding is to do this in iTextSharp you are actually copying the PDF from one to another, and omitting the pages to be removed.

I have this working, but I'm losing the form on the copied PDF; i.e. no values are being written out where before trying to copy the PDF to "remove" some pages, the values were being written correctly.

How can I retain the PDF form I've already created when I copy the form, or is there a better way of removing pages? Here is my code so far, that writes the PDF to a file but doesn't fill out the form (presumably because the form isn't preserved when copying):

    string file = "output.pdf";
    PdfReader reader = new PdfReader("template.pdf");

    Document doc = new Document();
    using (MemoryStream ms = new MemoryStream())
    {

        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
        doc.Open();
        doc.AddDocListener(writer);
        for (int i = 1; i <= reader.NumberOfPages; i++)
        {
            bool skipPage = this.SkipPage; // some nifty logic here
            if (skipPage)
                continue;

            doc.SetPageSize(reader.GetPageSize(i));
            doc.NewPage();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage page = writer.GetImportedPage(reader, i);
            int rotation = reader.GetPageRotation(i);
            if (rotation == 90 || rotation == 270)
                cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height);
            else
                cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
        }
        reader.Close();
        doc.Close();
        using (FileStream fs = new FileStream(file, FileMode.Create))
        {
            // this is the part stumping me; I need to use a PdfStamper to write 
            // out some values to fields on the form AFTER the pages are removed.
            // This works, but there doesn't seem to be a form on the copied page...
            this.stamper = new PdfStamper(new PdfReader(ms.ToArray()), fs);
            // write out fields here...
            stamper.FormFlattening = true;
            stamper.SetFullCompression();
            stamper.Close();
        }
    }

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

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

发布评论

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

评论(1

萌无敌 2024-11-13 10:00:08

没关系,我似乎已经能够通过使用 PDF 阅读器上的 SelectPages 方法来解决这个问题,并且我可以避免实际复制文件。

Nevermind, I seem to have been able to figure it out by using the SelectPages method on the PDF reader, and I can avoid having to do an actual copy of the files.

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