2013-07-27 52 views
0

看起来PDF文件有几层,我不能绘制新的对象覆盖。我试图使用PDFStamper和GetOverContent()使对象成为最上层,但失败了,我不知道为什么。我只想将所有对象添加到PDF的最顶层。可能吗?iTextSharp编辑一个PDF,但看不到新添加的对象

如何在PDF的顶层添加所有新对象?

这里是我的代码:

public static bool SaveFile(string srcFile, string DesFile, List<EditPage> pageLs, List<PdfObj> atOtherLs) 
    { 
     PdfReader reader = new PdfReader(srcFile); 

     Document doc = new Document(); 
     MemoryStream ms = new MemoryStream(); 
     PdfWriter writer = PdfWriter.GetInstance(doc, ms); 
     doc.Open(); 
     PdfContentByte cb = writer.DirectContent; 

     for (int i = 1; i <= reader.NumberOfPages; i++) 
     { 
      iTextSharp.text.Rectangle ps = reader.GetPageSize(i); 


      PdfImportedPage page = writer.GetImportedPage(reader, i); 
      EditPage ep = GetEditPage(pageLs, atOtherLs, i - 1); 



      WriteEditePage(doc, cb, ps, ep); 
     } 

     reader.Close(); 
     doc.Close(); 

     File.WriteAllBytes(DesFile, ms.ToArray()); 
     ms.Close(); 

     return true; 
    } 


public static void WriteEditePage(Document doc, PdfContentByte cb, Rectangle ps, EditPage ep) 
    { 
     for (int i = 0; i < ep.Objs.Count; i++) 
     { 
      if (ep.Objs[i].PdfType != PdfObjTypes.Gdi) 
       continue; 

      GdiObj gdi = ep.Objs[i] as GdiObj; 

      cb.SetColorStroke(new Color(gdi.Color)); 
      cb.SetColorFill(new Color(gdi.Color)); 
      cb.SetLineWidth(gdi.Thick/FV); 
      if (gdi.Dash != System.Drawing.Drawing2D.DashStyle.Solid) 
      { 
       cb.SetLineDash(2, 0.75f); 
      } 
      else 
      { 
       cb.SetLineDash(1); 
      } 

      switch (gdi.GdiType) 
      { 
       case GdiObjTypes.Points: 
        WritePoints(cb, ps, gdi as GdiPoint); 
        break; 
       case GdiObjTypes.Line: 
        WriteLine(cb, ps, gdi as GdiLine); 
        break; 
       case GdiObjTypes.Rectangle: 
        WriteRetangle(cb, ps, gdi as GdiRectangle); 
        break; 
       case GdiObjTypes.Ellipse: 
        WriteEllipse(cb, ps, gdi as GdiEllipse); 
        break; 
       case GdiObjTypes.Image: 
        WriteImage(cb, ps, gdi as GdiImage); 
        break; 
       case GdiObjTypes.Text: 
        WriteText(cb, ps, gdi as GdiText); 
        break; 
      } 
     } 
    } 
+1

* PDFStamper和GetOverContent *会比您当前的代码更正确。请提供说明检查问题的结果pdf。 – mkl

回答

0

您使用了错误的类的内容添加到现有的PDF文档。请阅读chapter 6 of my book,更具体地说是第6.3.1节。当然,这些例子都是用Java编写的,而且您使用的是iTextSharp,但是如果您需要C#示例,则可以在Sourceforge上找到它们。

读你的代码示例,我不知道GetEditPage()WriteEditPage()应该做什么。如果静态ColumnText.ShowTextAligned()不符合您的目的(例如,因为文本未被包装),那么您需要创建一个ColumnText实例并将该内容添加到该实例。

相关问题