2014-10-20 68 views
0

我有一些免费文本注释的PDF文件。复制注释

我想在PDF上执行邮件合并功能。我想制作一份PDF副本,并根据一些文本替换方法替换自由文本注释。

为了简单起见,我有一个程序需要注释并在其后面添加“LHC”。唉,副本可以工作,但注释保持不变。

我会尽量但是使用PdfAnnotation,我不确定如何从PdfDictionaryPdfAnnotation

转换见下

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(); 
+0

您替换文本内容而不是外观流。因此,可以使用现有的外观流。此外使用'PDFCopy'没有意义,您应该使用'PdfStamper'。 – mkl 2014-10-20 09:07:05

+0

我不确定这是否正确。无论如何,我正在检查它。我会在一到两天内回复我的发现。也许最好的方法是复制页面,然后从源文件获取注释并将它们重新添加回来。 – 2014-10-23 08:10:15

回答

2

参考我的代码: http://itextsharp.10939.n7.nabble.com/How-to-edit-annotations-td3352.html

(有另一个在stackoverflow的链接,我找不到,当我找到它时,我会在这里添加它)

步骤:

第1步。从阅读器创建一个压模。

步骤2.阅读所有注释

步骤3.删除一组按键,并作为后备任何字典项

您现在已经进行注释的编辑/复制和更改的值。

以下是代码:

//步骤1.创建压模

 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 

     // remember to set the page size before opening document 
     // otherwise the page is already set. 
     /* chapter02/HelloWorldMetadata.java */ 
     document.Open(); 

     // the pdf content 
     // cb does not work with stamper 


     // create the new pagez and add it to the pdf 
     // this segment of code is meant for writer 
     FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.ReadWrite); 


     PdfStamper writer = new PdfStamper(reader, fs, reader.PdfVersion, false); 

     for (int pg = 1; pg < reader.NumberOfPages; pg++) 
     { 

      // taken from http://itextsharp.10939.n7.nabble.com/How-to-edit-annotations-td3352.html 

      PdfDictionary pagedic = reader.GetPageN(pg); 
      PdfArray annotarray = (PdfArray)PdfReader.GetPdfObject(pagedic.Get(PdfName.ANNOTS)); 

      if (annotarray == null || annotarray.Size == 0) 
       continue; 

//步骤2读所有注释

  foreach (PdfIndirectReference annot in annotarray.ArrayList) 
      { 
       PdfDictionary annotationDic = (PdfDictionary)PdfReader.GetPdfObject(annot); 
       PdfName subType = (PdfName)annotationDic.Get(PdfName.SUBTYPE); 
       if (subType.Equals(PdfName.TEXT) || subType.Equals(PdfName.FREETEXT)) 
       { 

// 3.更改某个注释的不同属性的值以及删除几个键&字典

    annotationDic.Put(PdfName.CONTENTS, new PdfString("These are changed contents", PdfObject.TEXT_UNICODE)); 
       } 
       PdfString contents = annotationDic.GetAsString(PdfName.CONTENTS); 
       if (contents != null) 
       { 
        String value = contents.ToString(); 
        annotationDic.Put(PdfName.CONTENTS, new PdfString(value)); 
        annotationDic.Remove(PdfName.AP); 
        List<PdfName> tobeDel = new List<PdfName>(); 
        foreach (PdfName key in annotationDic.Keys) 
        { 
         if (key.CompareTo(PdfName.AP) == 0 || 
          key.CompareTo(PdfName.RC) == 0 || 
          annotationDic.Get(key).IsDictionary()) 
         { 
          tobeDel.Add(key); 
         } 
        } 
        foreach (PdfName key in tobeDel) 
        { 
         annotationDic.Remove(key); 
        } 
       } 
       writer.MarkUsed(annotationDic); 

      } 
      if ((pg + 1) < reader.NumberOfPages) 
      { 
       document.NewPage(); 
      } 
     } 


     // close the streams and voilá the file should be changed :) 

     writer.Close(); 
     reader.Close();