5

我正在使用 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();
    }

  }

}

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

4

4 回答 4

11

你犯了两个重大错误。

  1. 您正在为每个新页面创建对象的新实例。这将导致 PDF 膨胀,因为图像字节的添加次数与页面一样多。请在方法之外创建Image对象onEndPage(),然后重用它。这样,图像字节只会被添加到 PDF 中一次。
  2. 如文档所述,作为参数Document传递给onEndPage()方法的参数应被视为只读参数。禁止向其添加内容。它与您使用创建的对象不同new Document(PageSize.A4, 36, 36, 154, 54)。实际上,它是由实例PdfDocument在内部创建的类的PdfWriter实例。要添加图像,您需要PdfContentByte从作者那里获取 ,然后使用addImage().

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

于 2012-10-18T06:25:40.677 回答
4

你能试一下吗

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

代替

document.add(img);

里面onPageEnd

于 2012-10-17T20:34:25.957 回答
0

我已经为图像设置了绝对位置和对齐方式(在这种情况下,我将图像放在标题中)

    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);
于 2015-03-12T10:43:58.180 回答
0

在页面顶部添加图像的通用解决方案。我们也可以通过将图像放置在顶部来做到这一点。它可能会解决您的要求

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

}

于 2016-03-16T07:23:48.450 回答