如何在不创建新 PDF 的情况下更新 PDF? [英] How to update a PDF without creating a new PDF?

查看:57
本文介绍了如何在不创建新 PDF 的情况下更新 PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用另一个词替换现有 PDF AcroField 中的一个词.我正在使用 iTEXTSHARP 的 PDFStamper 来做同样的事情,它工作正常.但是,这样做需要创建一个新的 PDF,我希望更改能够反映在现有的 PDF 本身中.如果我将目标文件名设置为与原始文件名相同,则不会反映任何更改.我是 iTextSharp 的新手,我做错了什么吗?请帮忙..我正在提供我正在使用的一段代码

I am required to replace a word in an existing PDF AcroField with another word. I am using PDFStamper of iTEXTSHARP to do the same and it is working fine. But, in doing so it is required to create a new PDF and i would like the change to be reflected in the existing PDF itself. If I am setting the destination filename same as the original filename then no change is being reflected.I am new to iTextSharp , is there anything I am doing wrong? Please help.. I am providing the piece of code I am using

  private void ListFieldNames(string s)
    {
        try
        {
            string pdfTemplate = @"z:TEMPPDFPassportApplicationForm_Main_English_V1.0.pdf";
            string newFile = @"z:TEMPPDFPassportApplicationForm_Main_English_V1.0.pdf";
            PdfReader pdfReader = new PdfReader(pdfTemplate);

            for (int page = 1; page <= pdfReader.NumberOfPages; page++)
            {
                PdfReader reader = new PdfReader((string)pdfTemplate);
                using (PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create, FileAccess.ReadWrite)))
                {
                    AcroFields form = stamper.AcroFields;
                    var fieldKeys = form.Fields.Keys;
                    foreach (string fieldKey in fieldKeys)
                    {
                        //Replace Address Form field with my custom data
                        if (fieldKey.Contains("Address"))
                        {
                            form.SetField(fieldKey, s);
                        }    
                    }
                    stamper.FormFlattening = true;
                    stamper.Close();

                }

            }
        }

推荐答案

如我的书中所述 iText in Action,您不能同时读取文件和写入文件.想一想 Word 的工作原理:您无法打开 Word 文档并直接向其中写入内容.Word 总是创建一个临时文件,将更改写入其中,然后用它替换原始文件,然后丢弃该临时文件.

As documented in my book iText in Action, you can't read a file and write to it simultaneously. Think of how Word works: you can't open a Word document and write directly to it. Word always creates a temporary file, writes the changes to it, then replaces the original file with it and then throws away the temporary file.

你也可以这样做:

  • PdfReader读取原始文件,
  • PdfStamper 创建一个临时文件,完成后,
  • 用临时文件替换原始文件.
  • read the original file with PdfReader,
  • create a temporary file for PdfStamper, and when you're done,
  • replace the original file with the temporary file.

或者:

  • 将原始文件读入byte[],
  • 用这个byte[]创建PdfReader,和
  • 使用 PdfStamper 的原始文件路径.
  • read the original file into a byte[],
  • create PdfReader with this byte[], and
  • use the path to the original file for PdfStamper.

第二个选项更危险,因为如果您在PdfStamper 中执行导致异常的操作,您将丢失原始文件.

This second option is more dangerous, as you'll lose the original file if you do something that causes an exception in PdfStamper.

这篇关于如何在不创建新 PDF 的情况下更新 PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆