簡體   English   中英

如何在iText生成的PDF的標題中添加圖像?

[英]how to add an image to my header in iText generated PDF?

我正在使用iText生成PDF。 我創建了一個自定義PdfPageEventHelper,以向每個頁面添加頁眉(和頁腳)。

我的問題是我不知道如何添加圖像,以使其顯示在“頁眉框”中。 我只知道如何將圖像添加到文檔內容本身(如果這是有道理的)。

這是一些代碼片段...

public static void main(String[] args) {
  Rectangle headerBox = new Rectangle(36, 54, 559, 788);
  /* ... */
  Document document = new Document(PageSize.A4, 36, 36, 154, 54);
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILENAME));
  HeaderFooter event = new HeaderFooter();
  writer.setBoxSize("headerBox", headerBox);
  writer.setPageEvent(event);
  document.open();
  addContent();
  document.close();
}

static class HeaderFooter extends PdfPageEventHelper {

  public void onEndPage(PdfWriter writer, Document document) {
    Rectangle rect = writer.getBoxSize("headerBox");
    // add header text
    ColumnText.showTextAligned(writer.getDirectContent(),
      Element.ALIGN_RIGHT, new Phrase("Hello", fontHeader1),
      rect.getLeft(), rect.getTop(), 0);

    // add header image
    try {
      Image img = Image.getInstance("c:/mylogo.PNG");
      img.scaleToFit(100,100);
      document.add(img);
    } catch (Exception x) {
      x.printStackTrace();
    }

  }

}

任何有關將圖像添加到標題的適當方式的建議都將不勝感激!!

您正在犯兩個主要錯誤。

  1. 您正在為每個新頁面創建對象的新實例。 這將導致PDF膨脹,因為圖像字節的添加次數將與頁面一樣多。 請在onEndPage()方法之外創建Image對象,然后重用它。 這樣,圖像字節將僅添加到PDF一次。
  2. 如文檔所述, onEndPage() Document作為參數傳遞給onEndPage()方法,應將其視為只讀參數。 禁止向其中添加內容。 與使用new Document(PageSize.A4, 36, 36, 154, 54)創建的對象不同。 實際上,它是由PdfWriter實例在內部創建的PdfDocument類的實例。 要添加圖像,您需要從PdfContentByte獲取PdfContentByte ,然后使用addImage()添加圖像。

閱讀文檔可以輕松避免此類錯誤。 通過閱讀我的《 iText in Action》,您可以節省大量時間。

你能試一下嗎

img.setAbsolutePosition(10, 10);
writer.getDirectContent().addImage(img);

代替

document.add(img);

onPageEnd里面?

我已經設定了圖片的位置和對齊方式(在這種情況下,我將圖片放在了標題中)

    try {
        Image img = Image.getInstance("url/logo.png");
        img.scaleToFit(100,100);  
        img.setAbsolutePosition((rect.getLeft() + rect.getRight()) / 2 - 45, rect.getTop() - 50);
        img.setAlignment(Element.ALIGN_CENTER);          
        writer.getDirectContent().addImage(img);
      } catch (Exception x) {
        x.printStackTrace();
      }

我還調整了文檔頁邊距,以在文檔的頁眉和頁腳中留出一定的空間。

document.setMargins(20, 20, 100, 100);

在頁面頂部添加圖像的通用解決方案。 我們也可以通過將圖像放置在頂部來實現。 它可能會解決您的要求

public static void main(String[] args) throws MalformedURLException, IOException, DocumentException {
      Document document = new Document();
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("HelloWorld.pdf"));
            document.open();

            //
            // Scale the image to a certain percentage
            //
            String filename = "image.jpg";
            Image image = Image.getInstance(filename);
            image = Image.getInstance(filename);
            image.scalePercent(200f);
            image.setAbsolutePosition(0, (float) (PageSize.A4.getHeight() - 20.0));
            System.out.println(image.getScaledHeight());
            document.add(image);

            //
            // Scales the image so that it fits a certain width and
            // height
            //
            image.scaleToFit(100f, 200f);
            document.add(image);
            document.add(new Chunk("This is chunk 3. "));
            System.out.println("created");
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM