0
我有一些免费文本注释的PDF文件。复制注释
我想在PDF上执行邮件合并功能。我想制作一份PDF副本,并根据一些文本替换方法替换自由文本注释。
为了简单起见,我有一个程序需要注释并在其后面添加“LHC”。唉,副本可以工作,但注释保持不变。
我会尽量但是使用PdfAnnotation
,我不确定如何从PdfDictionary
到PdfAnnotation
转换见下
string oldFile = "C:\\Temp\\oldFile.pdf";
string newFile = "C:\\Temp\\newFile.pdf";
// open the reader
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
// open the writer
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfCopy writer = new PdfCopy(document,fs);
document.Open();
// the pdf content
PdfContentByte cb = writer.DirectContent;
// adding Free Text Annotation
for (int pg = 1; pg < reader.NumberOfPages; pg++)
{
PdfDictionary pageDict = reader.GetPageN(pg);
PdfArray annotArray = pageDict.GetAsArray(PdfName.ANNOTS);
for (int i = 0; i < annotArray.Size; ++i)
{
PdfDictionary curAnnot = annotArray.GetAsDict(i);
PdfName contents = new PdfName("Contents");
PdfString str = curAnnot.GetAsString(contents);
String newString = str.ToString() + "LHC";
curAnnot.Remove(contents);
curAnnot.Put(contents, new PdfString(newString));
}
PdfImportedPage page = writer.GetImportedPage(reader, pg);
// PdfImportedPage pageOut = writer.destinationPdfReader(reader, pg);
//cb.AddTemplate(page, 0, 0);
writer.AddPage(page);
PdfAnnotation annot = new PdfAnnotation(writer, new Rectangle(0, 0));
writer.AddAnnotation(annot);
}
document.Close();
fs.Close();
writer.Close();
reader.Close();
您替换文本内容而不是外观流。因此,可以使用现有的外观流。此外使用'PDFCopy'没有意义,您应该使用'PdfStamper'。 – mkl 2014-10-20 09:07:05
我不确定这是否正确。无论如何,我正在检查它。我会在一到两天内回复我的发现。也许最好的方法是复制页面,然后从源文件获取注释并将它们重新添加回来。 – 2014-10-23 08:10:15