2012-10-17 82 views
4

我正在使用iText来生成PDF。我创建了一个自定义的PdfPageEventHelper,为每个页面添加一个页眉(和页脚)。如何在iText生成的PDF中将图像添加到我的标题中?

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

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

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

    } 

} 

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

罗布

+0

应该'writer'在'onEndPage'与'pdfWriter'交换? –

+0

@denisk这是一个错字,抱歉 - 但感谢您的好眼睛。 –

回答

11

你犯了两个重大错误。

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

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

+0

为什么地球上这是低调? –

+0

@BrunoLowagie非常有帮助!谢谢。我会花更多时间在你的书上。与此同时,你(和Denisk)建议的工作,但它有一个副作用。当我添加'img.setAbsolutePosition(35,742); writer.getDirectContent()。addImage(img);'把图片放在每个页面的顶部,我的所有文档内容(标题除外)都被推送到第二页。第一页是空白的。任何想法我做错了什么?如果有帮助,我可以用详细的代码发布一个新的stackoverflow问题。 –

+0

@RobertHume发布一个新问题的方法是 –

5

您可以尝试

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

代替

document.add(img); 

onPageEnd

+0

谢谢,非常有用,在一天结束时布鲁诺的答案更详细和有帮助,所以我不得不“检查”他 - 我希望你明白!再次感谢。 –

+0

@RobertHume我完全同意你对这个 –

1

我设置abolute位置和排列的图像(在这种情况下,我把我的形象在头)

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); 
1

在页面顶部添加图像的通用解决方案。我们也可以通过将图像置于顶部来实现。它可能会解决您的要求

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

}

+0

OP正在讨论在页面事件监听器中添加图像,而在这样的监听器中,您不应该直接通过'document.add'添加任何内容,参见参考资料。例如@布鲁诺的答案,第2项。因此,您的解决方案并不适用于当前的情况。 – mkl

相关问题