2011-01-26 7 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)を使用していました。まあ、私はすぐに文書が回転された場合に問題を引き起こすことを認識しました。 OK。問題はありません。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

"トリムボックス"があれば、どのように対処しますか? – ch3rryc0ke

+1

PdfReaderは、getBoxSize()を使用して、指定されたPDFページから任意の「ボックス」を取得できます:http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfReader.html#getBoxSize(int、java.lang。文字列) –

関連する問題