2011-01-26 3 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

"트림 박스"가 있으면 어떻게 처리하나요? – ch3rryc0ke

+1

PdfReader는 getBoxSize()를 사용하여 주어진 PDF 페이지에서 "상자"를 얻을 수 있습니다. http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfReader.html#getBoxSize(int, java.lang. 끈) –

관련 문제