2017-02-01 1 views
2

텍스트가있는 페이지 PDF 파일의 가운데에 그림을 추가하고 싶지만 제대로 처리 할 수 ​​없습니다. 이미지 SetAbsolutePosition에 사용하지만 텍스트가 그림 아래에 있지 않습니다. 내 PDF 파일 페이지의 다음 형식으로 필요ItextSharp 페이지 아래에 텍스트가있는 페이지의 중심에 이미지를 추가하십시오.

:

   PdfWriter writer = PdfWriter.GetInstance(doc, fs); 

       ITextEvents ev = new ITextEvents(); 
       writer.PageEvent = ev; 
       doc.Open(); 

       var paragraph = new Paragraph(); 
       var paragraph1 = new Paragraph(); 
       var chunk = new Chunk("Text under picture", f14nb); 
       var chunk1 = new Chunk("Code of picture", f14); 

       img = ScaleImg(Image.GetInstance(imgNane_2)); 
       img.SetAbsolutePosition((PageSize.A4.Width - img.ScaledWidth)/2, 
        ((PageSize.A4.Height - img.ScaledHeight)/2)); 

       paragraph.Add(img); 
       paragraph1.Add(chunk); 
       paragraph1.Add(chunk1); 
       doc.Add(paragraph); 
       doc.Add(paragraph1); 

       doc.Close(); 

private Image ScaleImg(Image img) 
{ 
    if (img.Height > img.Width) 
    { 
     //Maximum height is 800 pixels. 
     float percentage = 0.0f; 
     percentage = 640/img.Height; 
     img.ScalePercent(percentage * 100); 
    } 
    else 
    { 
     //Maximum width is 600 pixels. 
     float percentage = 0.0f; 
     percentage = 500/img.Width; 
     img.ScalePercent(percentage * 100); 
    } 
    return img; 
} 

는 내가 해결을위한 다른 방법을 사용해야한다는 생각 : enter image description here

나는 다음 코드를 사용 내 문제지만, 나는 어느 것을 모른다.

감사합니다.

답변

0

어떻게했는지 알았습니다.

 public Image getWatermarkedImage(Document Doc, Image img, String watermarkText) 
    { 
     float width = img.ScaledWidth; 
     float height = img.ScaledHeight; 

     PdfTemplate template = cb.CreateTemplate(width, height); 

     template.AddImage(img, width, 0, 0, height, 0, 0); 
     ColumnText.ShowTextAligned(template, Element.ALIGN_RIGHT, 
      new Phrase(watermarkText, fontBold_14), width - 10, 10, 0); 

     return Image.GetInstance(template); 
    } 

이 이미지에서 (주 코드) 텍스트를 추가 할 것이다 :

   var codeOfPicture = "*Code of picture* - *Код картинки*"; 

       var chunk = new Chunk("Text under picture", font_14); 

       img = ScaleImg(Image.GetInstance(imgNane_1));//imgNane_2)); 

       var tmpImg = getWatermarkedImage(doc, img, codeOfPicture); 
       var textY = ((PageSize.A4.Height - img.ScaledHeight)/2) - 15; 
       var textX = PageSize.A4.Width/2; 

       tmpImg.SetAbsolutePosition((PageSize.A4.Width - img.ScaledWidth)/2, 
        ((PageSize.A4.Height - img.ScaledHeight)/2)); 

       doc.Add(tmpImg); 

       ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, new Phrase(chunk), textX, textY, 0); 

텍스트와 이미지이며