如何在iText生成的PDF中添加图像到我的标题? [英] how to add an image to my header in iText generated PDF?

查看:625
本文介绍了如何在iText生成的PDF中添加图像到我的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText生成PDF。我创建了一个自定义的PdfPageEventHelper来为每个页面添加页眉(和页脚)。

I'm using iText to generate a PDF. I created a custom PdfPageEventHelper to add a header (and footer) to each page.

我的问题是我不知道如何添加图像以便显示在标题框。我只知道如何将图像添加到文档内容本身(如果有意义的话)。

My problem is I don't know how to add the image so it is displayed in the "header box". I only know how to add the image to the document content itself (if that makes sense).

以下是一些代码片段...

Here's some code snippets ...

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();
    }

  }

}

非常感谢关于将图像添加到标题的适当方式的任何建议!!

Any suggestions on the appropriate way to add the image to the header are greatly appreciated!!

Rob

推荐答案

你犯了两个重大错误。


  1. 你正在创建一个新的对象实例每一个新页面。这将导致臃肿的PDF,因为图像字节将被添加为与页面一样多的次数。请在 onEndPage()方法之外创建 Image 对象,然后重复使用它。这样,图像字节将仅添加到PDF一次。

  2. 如文档所述,文档传递给 onEndPage()作为参数的方法应被视为只读参数。禁止向其添加内容。它与您使用新文档(PageSize.A4,36,36,154,54)创建的对象不同。实际上,它是由 PdfWriter 实例在内部创建的 PdfDocument 类的实例。要添加图像,您需要从编写器获取 PdfContentByte ,并使用 addImage()添加图像。

  1. You are creating a new instance of the object for every new page. This will result in a bloated PDF as the image bytes will be added as many times as there as pages. Please create the Image object outside the onEndPage() method, and reuse it. This way, the image bytes will be added to the PDF only once.
  2. As documented, the Document passed to the onEndPage() method as a parameter should be considered as a read-only parameter. It is forbidden to add content to it. It's a different object than the one you created with new Document(PageSize.A4, 36, 36, 154, 54). In reality, it's an instance of a PdfDocument class created internally by the PdfWriter instance. To add an image, you need to get the PdfContentByte from the writer, and add the image using addImage().

阅读文档可以轻松避免这样的错误。您可以通过阅读我的书 iText in Action 来节省大量时间。

Errors like this can easily be avoided by reading the documentation. You can save plenty of time by reading my book iText in Action.

这篇关于如何在iText生成的PDF中添加图像到我的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆