2011-01-26 22 views
1

所以,我試圖簡單地向pdf文檔的左上角的pdf添加文本註釋。當前代碼是這樣的:使用iTextSharp添加註釋以旋轉裁切框

public static byte[] StampPDFDocument(byte[] pdf, string stampString) { 
      using (var ms = new MemoryStream()) { 

       var reader = new iTextSharp.text.pdf.PdfReader(pdf); 
       var stamper = new iTextSharp.text.pdf.PdfStamper(reader, ms); 

       var box = reader.GetCropBox(1); 
       var left = box.Left; 
       var top = box.Top; 

       iTextSharp.text.Rectangle newRectangle = new iTextSharp.text.Rectangle(left + 20, top - 20, left + 250, top - 40); 
       var pcb = new iTextSharp.text.pdf.PdfContentByte(stamper.Writer); 
       pcb.SetColorFill(iTextSharp.text.BaseColor.RED); 

       var annot = iTextSharp.text.pdf.PdfAnnotation.CreateFreeText(stamper.Writer, newRectangle, stampString, pcb); 
       annot.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_PRINT; 

       annot.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0); 
       stamper.AddAnnotation(annot, 1); 
       stamper.Close(); 
       return ms.ToArray(); 
      } 
     } 

現在,原始代碼只是使用box = reader.GetPageSize(1)。那麼,如果文檔已被旋轉,我很快意識到會導致問題。好。沒問題,有一個叫做reader.GetPageSizeWithRotation的函數。這就像一個魅力。但是,現在我正在獲取具有不同裁剪框的文檔。所以我添加的註釋位於裁剪框區域之外。所以這個當前的代碼只適用於非旋轉的文檔。問題是,無論文檔是旋轉還是包含與文檔不同的裁剪框,不管文檔是旋轉還是包含不同的裁剪框,我們如何獲得pdf文檔中的左上角核心?

回答

2

這就是我結束了。

public static byte[] StampPDFDocument(byte[] pdf, string stampString) { 
    using (var ms = new MemoryStream()) { 
     var reader = new iTextSharp.text.pdf.PdfReader(pdf); 
     var stamper = new iTextSharp.text.pdf.PdfStamper(reader, ms);  

     int rotation = reader.GetPageRotation(1);    

     var box = reader.GetPageSizeWithRotation(1); 
     var cropbox = reader.GetCropBox(1); 

     float left = cropbox.Left; 
     float top = cropbox.Top; 

     if (rotation == 90) { 
      left = cropbox.Bottom; 
      top = box.Height - cropbox.Left; 
      cropbox = new iTextSharp.text.Rectangle(left, top, left + cropbox.Height, top - cropbox.Width); 
     } 
     else if (rotation == 180) { 
      left = box.Width - cropbox.Left - cropbox.Width; 
      top = box.Height - cropbox.Bottom;     
      cropbox = new iTextSharp.text.Rectangle(left, top, left + cropbox.Width, top - cropbox.Height); 
     } 
     else if (rotation == 270) { 
      left = box.Width - cropbox.Top; 
      top = cropbox.Right; 
      cropbox = new iTextSharp.text.Rectangle(left, top, left + cropbox.Height, top - cropbox.Width); 
     }    

     iTextSharp.text.Rectangle newRectangle = new iTextSharp.text.Rectangle(left + 20, top - 20, left + 250, top - 40); 

     var pcb = new iTextSharp.text.pdf.PdfContentByte(stamper.Writer); 
     pcb.SetColorFill(iTextSharp.text.BaseColor.RED); 

     var annot = iTextSharp.text.pdf.PdfAnnotation.CreateFreeText(stamper.Writer, newRectangle, stampString, pcb); 
     annot.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_PRINT; 
     annot.Rotate = reader.GetPageRotation(1); 

     annot.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0); 
     stamper.AddAnnotation(annot, 1); 
     stamper.Close(); 
     return ms.ToArray(); 
    } 
} 
1

這裏的源getPageSizeWithRotation:

public Rectangle getPageSizeWithRotation(int index) { 
    return getPageSizeWithRotation(pageRefs.getPageNRelease(index)); 
} 

public Rectangle getPageSizeWithRotation(PdfDictionary page) { 
    Rectangle rect = getPageSize(page); 
    int rotation = getPageRotation(page); 
    while (rotation > 0) { 
     rect = rect.rotate(); 
     rotation -= 90; 
    } 
    return rect; 
} 

因此,所有你需要做的推出自己是編寫調用getCropBox(),而不是getPageSize()功能。

PS:getCropBox()將返回媒體框,如果沒有裁剪框,​​所以你不必分別調用getCropBox和getPageSize。

+0

如何處理「trimbox」如果它在那裏? – ch3rryc0ke 2012-11-08 22:40:11