2014-10-21 2 views
0

아래 코드에서 기존 텍스트 위에 텍스트 레이어를 배치하려고합니다 (기존의 기존 텍스트는 흰색 글꼴로 작성하여 "숨김"이며이 대체 프로세스의 자리 표시 자 역할을합니다).). 아래의 코드는이 게시물에 크리스 하스에서 제공하는 코드에 매우 크게 의존 :iTextSharp - 텍스트 덮어 쓰기

Getting Coordinates of string using ITextExtractionStrategy and LocationTextExtractionStrategy in Itextsharp

하지만, 내 텍스트 배치에 대해 "왼쪽"위치는 괜찮지 만은 "바닥"텍스트보다 낮은 덮어 쓰기를 시도하고 있습니다 (글꼴이 같더라도). 원본 문서에있는 텍스트의 정확한 좌표를 얻기 위해 이것을 어떻게 변경할 수 있습니까? 원본 문서는 레이아웃 용 테이블을 사용하여 작성되었으므로이 "덮어 쓰기"프로세스에 영향을 줍니까?

byte[] content; 

string tmppath = @"C:\Junk\MSE_1.pdf"; 

using (MemoryStream output = new MemoryStream()) 
{ 
    PdfReader pdf_rdr = new PdfReader(tmppath); 
    PdfStamper stamper = new PdfStamper(pdf_rdr, output); 

    for (int i = 1; i < pdf_rdr.NumberOfPages; i++) 
    { 
     //Create an instance of our strategy 
     var t = new MyLocationTextExtractionStrategy("stuff"); 

     if (t != null) 
     { 
      //Parse the page of the document above 
      //(if the text was found) 
      var ex = PdfTextExtractor.GetTextFromPage(pdf_rdr, i, t); 

      //Loop through each chunk found 
      foreach (var p in t.myPoints) 
      { 
       Console.WriteLine(string.Format("Found text {0} at {1} x {2} on page {3}", p.Text, p.Rect.Left, p.Rect.Bottom, i.ToString())); 

       PdfContentByte pcb = stamper.GetOverContent(i); 

       pcb.BeginText(); 

       try 
       { 
        BaseFont bf = BaseFont.CreateFont(@"C:\Junk\FontFiles\georgia.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 

        pcb.SetFontAndSize(bf, 10); 
        pcb.SetTextMatrix(p.Rect.Left, p.Rect.Bottom); 
        //pcb.SetTextRise(3); 
        pcb.ShowText("STUFF"); 
       } 
       finally 
       { 
        pcb.EndText(); 
       } 
      } 
     } 
    } 

    // Set the flattening flag to true, as the editing is done 
    stamper.FormFlattening = true; 

    // close the pdf stamper 
    stamper.Close(); 

    //close the PDF reader 
    pdf_rdr.Close(); 

    //put the output into the byte array 
    content = output.ToArray(); 
} 

//write the content to a PDF file 
using (FileStream fs = File.Create(@"C:\Junk\MSE_REPLACED.pdf")) 
{ 
    fs.Write(content, 0, (int)content.Length); 
    fs.Flush(); 
} 

답변

2

크리스 '는 캐릭터가 사용하는 지역에 관심이 있기 때문에 MyLocationTextExtractionStrategy는 하강 라인과 텍스트 조각의 상승 라인을 고려합니다.

작업의 경우 텍스트 그리기 작업이 현재 위치를 기준선 시작으로 사용하기 때문에 기준선이 필요합니다.

따라서 Chris의 클래스 변형을 만들어야합니다.이 클래스는 상승 및 하강 선 이외에 텍스트의 기준선을 관리합니다.

는 PS : 영업 이익은

가 나는베이스 라인 작업에 익숙하지 않다 두려워 코멘트에 물었다. 공유 할 수있는 예제가 있습니까? 당신은 그가 하강의 시작에서 왼쪽 하단 모서리를 가진 사각형을 저장시피

public override void RenderText(TextRenderInfo renderInfo) { 
    base.RenderText(renderInfo); 

    //Get the bounding box for the chunk of text 
    var bottomLeft = renderInfo.GetDescentLine().GetStartPoint(); 
    var topRight = renderInfo.GetAscentLine().GetEndPoint(); 

    //Create a rectangle from it 
    var rect = new iTextSharp.text.Rectangle(
              bottomLeft[Vector.I1], 
              bottomLeft[Vector.I2], 
              topRight[Vector.I1], 
              topRight[Vector.I2] 
              ); 

    //Add this to our main collection 
    this.myPoints.Add(new RectAndText(rect, renderInfo.GetText())); 
} 

: 크리스 '코드를 보면

당신은, 당신이 RenderText 구현을 볼 수 있습니다 언급 오른쪽 상단 모서리가 상승 선 끝입니다.

당신이

//Get the bounding box for the chunk of text above the baseline 
    var bottomLeft = renderInfo.GetBaseline().GetStartPoint(); 
    var topRight = renderInfo.GetAscentLine().GetEndPoint(); 

에 의해

//Get the bounding box for the chunk of text 
    var bottomLeft = renderInfo.GetDescentLine().GetStartPoint(); 
    var topRight = renderInfo.GetAscentLine().GetEndPoint(); 

교체 할 경우 덮어 쓰기 코드는 잘 작동합니다.

+0

나는베이스 라인 작업에 익숙하지 않다. 공유 할 수있는 예제가 있습니까? – cro717

+0

그것은 매력처럼 작동했습니다! 당신의 도움을 주셔서 대단히 감사합니다! – cro717