2011-04-06 68 views
4

我使用iTextSharp的5.0.6读取现有的PDF,遍历页面上的每个冲压文本,然后写出新加盖PDF不加盖有些。我面临的问题是,这不是100%的时间。对于某些PDF,每个页面都按预期盖印,对于其他页面,大多数页面会被盖印,而有些则不会。好像可能有出模具的GetOverContent()不返回最顶层的一个问题,但是这只是一个假设。有没有人有类似的问题?iTextSharp的 - 如预期

using iTextSharp.text; 
using iTextSharp.text.pdf; 
const string WATERMARK_TEXT = "John Doe"; 

static void Main(string[] args) 
{ 
    string masterPdf = "master.pdf"; 
    string pdfToCreate = "watermark.pdf"; 

    byte[] bytes = StampPDF(masterPdf); 
    using (FileStream stream = new FileStream(pdfToCreate, FileMode.Create)) 
    { 
     stream.Write(bytes, 0, bytes.Length); 
    } 

} 

static byte[] StampPDF(string PdfPath) 
{ 
    using (MemoryStream memoryStream = new MemoryStream()) 
    { 
     PdfReader reader = new PdfReader(PdfPath); 
     int pageCount = reader.NumberOfPages; 
     PdfStamper stamper = new PdfStamper(reader, memoryStream); 

     float fontSize = 9; 
     float textAngle = 0f; 
     BaseFont font = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED); 
     BaseColor backgroundColor = new BaseColor(0, 0, 0); 
     BaseColor fontColor = new BaseColor(255, 255, 255); 
     float padding = 2f; 
     float fontWidth = font.GetWidthPoint(WATERMARK_TEXT, fontSize); 
     iTextSharp.text.Rectangle pageSize; 
     PdfContentByte pageContents; 
     for (int i = 1; i <= pageCount; i++) 
     { 
      pageSize = reader.GetPageSize(i); 
      pageContents = stamper.GetOverContent(i); 
      //draw a rectangle 
      pageContents.SetColorFill(backgroundColor); 
      pageContents.MoveTo(pageSize.Width - (fontWidth + padding), 0f); 
      pageContents.LineTo(pageSize.Width, 0f); 
      pageContents.LineTo(pageSize.Width, 14f); 
      pageContents.LineTo(pageSize.Width - (fontWidth + padding), 14f); 
      pageContents.Fill(); 
      //drop our watermark on top of the rectangle we just created 
      pageContents.BeginText(); 
      pageContents.SetColorFill(fontColor); 
      pageContents.SetFontAndSize(font, fontSize); 
      pageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, WATERMARK_TEXT, pageSize.Width - fontWidth, 4, textAngle); 
      pageContents.EndText(); 
     } 
     stamper.Close(); 
     reader.Close(); 

     return memoryStream.ToArray(); 
    } 
} 

回答

2

对于那些可能会遇到同样的问题,关键是检查裁剪框。由于PDF的裁剪框的尺寸可能小于其页面尺寸,因此您需要有条件地使用其中一个或另一个。因此,基于该for环上面的代码示例将被改变为这样:

for (int i = 1; i <= pageCount; i++) 
{ 
    mediaBox = reader.GetPageSize(i); 
    cropBox = reader.GetCropBox(i); 
    overContent = stamper.GetOverContent(i); 

    if (cropBox != null && (cropBox.Width < mediaBox.Width || cropBox.Height < cropBox.Height)) 
     mediaBox = cropBox; 

    //draw a rectangle 
    overContent.SetColorFill(backgroundColor); 
    overContent.MoveTo(mediaBox.Right - (fontWidth + fontPadding), mediaBox.Bottom); 
    overContent.LineTo(mediaBox.Right, mediaBox.Bottom); 
    overContent.LineTo(mediaBox.Right, mediaBox.Bottom + rectangleHeight); 
    overContent.LineTo(mediaBox.Right - (fontWidth + fontPadding), mediaBox.Bottom + rectangleHeight); 
    overContent.ClosePathFillStroke(); 
    //drop our watermark on top of the rectangle we just created 
    overContent.BeginText(); 
    overContent.SetColorFill(fontColor); 
    overContent.SetFontAndSize(font, fontSize); 
    overContent.ShowTextAligned(PdfContentByte.ALIGN_LEFT, WATERMARK_TEXT, mediaBox.Right - fontWidth, mediaBox.Bottom + (rectangleHeight - fontSize), textAngle); 
    overContent.EndText(); 
} 
0

你已经犯了两个错误:

  1. 你假设页面没有旋转,但它们可以是:90,180,270注,我从来没有看到了180页,但它的合法性。绘制到旋转的页面时,您必须在绘制时考虑该旋转。有趣的变换矩阵。

  2. 你假定页的(未旋转)左下角是0,0。你的基础在页面的宽度和高度(关闭)的测量,但不调整任何在左下角的偏移。

有三种方法做一个横向页面:
11 “×8.5”
8.5 “X11” @ 90度的旋转
8.5 “X11” @ 270度的旋转

技术上,第四种方法是建立一个11x8.5 @ 180,但任何编写这样的代码的人都应该被惩罚。很多。

有浮动关于如何处理页面旋转,让细节上的各种做题。通过你的代码,我会说你会很快找出llx,lly的东西。

+0

虽然它不是在代码片段表示我已经采取了看看页面旋转的问题,没有所有的PDF文件被旋转(即全部具有0的旋转值)。探索抵消是一个好主意,我会看看。谢谢。 – ScottD 2011-04-06 17:50:44