如何更新,而无需创建一个新的PDF PDF文件? [英] How to update a PDF without creating a new PDF?
问题描述
我需要现有的PDF AcroField用另一个词来代替一个字。我使用iTextSharp的的PDFStamper做同样的,这是工作的罚款。但是,这样做就需要创建一个新的PDF,我想的变化,以现有的PDF本身被反射。如果我设定目标文件名一样的原始文件名则没有变化正在reflected.I是新来iTextSharp的,有什么我做错了吗?请帮助..我提供的那块code的我使用
私人无效ListFieldNames(字符串s)
{
尝试
{
字符串pdfTemplate = @Z:\\ TEMP \\ PDF \\ PassportApplicationForm_Main_English_V1.0.pdf
字符串NEWFILE = @Z:\\ TEMP \\ PDF \\ PassportApplicationForm_Main_English_V1.0.pdf
PdfReader pdfReader =新PdfReader(pdfTemplate); 对于(INT页= 1;页< = pdfReader.NumberOfPages;网页++)
{
PdfReader读卡器=新PdfReader((字符串)pdfTemplate);
使用(PdfStamper压模=新PdfStamper(阅读器,新的FileStream(NEWFILE,FileMode.Create,FileAccess.ReadWrite)))
{
AcroFields形式= stamper.AcroFields;
VAR fieldKeys = form.Fields.Keys;
的foreach(在fieldKeys串fieldKey)
{
//更换地址表单域与我的自定义数据
如果(fieldKey.Contains(地址))
{
form.SetField(fieldKey,S);
}
}
stamper.FormFlattening = TRUE;
stamper.Close(); } }
}
正如我在书中的 iText的行动,则无法读取文件的同时写入。想想Word如何工作的:你不能打开一个Word文档并直接写入。 Word总是创建一个临时文件,写入更改,然后替换它原来的文件,然后扔掉的临时文件。
您可以做到这一点:
- 阅读
PdfReader
原始文件, - 创建为
PdfStamper
的临时文件,当您完成后, - 的临时文件替换原文件。
或者
- 读取原始文件转换成
字节[]
, - 创建
PdfReader
本字节[]
和 - 使用路径原始文件的
PdfStamper
。
这第二个选项是比较危险的,因为如果你做一些事情,导致异常的 PdfStamper
你会失去原始文件。
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:\TEMP\PDF\PassportApplicationForm_Main_English_V1.0.pdf";
string newFile = @"z:\TEMP\PDF\PassportApplicationForm_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();
}
}
}
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.
You can do that too:
- 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.
Or:
- read the original file into a
byte[]
, - create
PdfReader
with thisbyte[]
, and - use the path to the original file for
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屋!