14 Replies Latest reply on Sep 3, 2009 9:16 AM by Rowan Tetu

    Put an Image in a header with iText

    SIBUT Nicolas Newbie


      I want to put an Image in the header of my PDF document.
      My code :

      <p:header borderWidthTop="0" borderWidthBottom="0" alignment="center">
              <p:image value="/img/Nico/banierePDF.jpg" />

      And the error :

      2 juin 2008 11:09:48 com.sun.facelets.FaceletViewHandler handleRenderException
      GRAVE: Error Rendering View[/Nico/rapportDocumentSimple.xhtml]
      java.lang.RuntimeException: java.lang.ClassCastException: Insertion of illegal Element: 32
           at org.jboss.seam.pdf.ui.UIDocument.processHeaders(UIDocument.java:243)
           at org.jboss.seam.pdf.ui.UIDocument.encodeBegin(UIDocument.java:223)
           at javax.faces.component.UIComponent.encodeAll(UIComponent.java:884)
           at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)

      If you have the solution...
      Thanks in advance !!!

        • 2. Re: Put an Image in a header with iText
          Norman Richards Expert

          Sorry.  I don't think it's possible within the constraints of the IText HeaderFooter class.  It is not very flexible.  If you can point me to an example, I'll be happy to make the appropriate changes.  However, the only way I can see to do this is to rewrite/replace HeaderFooter.

          • 3. Re: Put an Image in a header with iText
            SIBUT Nicolas Newbie

            Ok, thanks for tour reply.

            I just want to put my society logo in the top of all my pdf pages.
            header would be the best solution but...

            If anybody have an idea...

            • 4. Re: Put an Image in a header with iText
              Nicklas Karlsson Master

              Is it possible to watermark the logo?

              • 5. Re: Put an Image in a header with iText
                SIBUT Nicolas Newbie

                I thinh it will be possible, but after, how can I do for include this in the top of my pages?

                • 6. Re: Put an Image in a header with iText
                  Nicklas Karlsson Master

                  Never done it but perhaps this can be used as some sort of starting point.

                  • 7. Re: Put an Image in a header with iText
                    SIBUT Nicolas Newbie

                    Perhaps yes but I don't wnat to use java code, I make my pdf with my xhtml view ;)

                    • 8. Re: Put an Image in a header with iText
                      Jason Long Novice

                      This is what I do.  I am using JasperReports, but I fall back to iText to overlay another PDF at the top of the document.

                      The PDF is just the logo in vector graphics format saved as a PDF.

                      This allows for the the logo to scale if the PDF is zoomed into.

                      I am sure there is a better way, but I made this a couple of years ago and it works for me.

                          public byte[] addLogo(JasperPrint jasperPrint) throws Exception {
                              byte[] bytes = null;
                              JRPdfExporter exporter = new JRPdfExporter();
                              exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                              ByteArrayOutputStream baos = new ByteArrayOutputStream();
                              ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
                              exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
                              try {
                                      bytes = baos.toByteArray();
                                      PdfReader pdfReader = new PdfReader(bytes);
                                      Rectangle psize = pdfReader.getPageSize(1);
                                //      System.out.println("psize: (" + psize.width()+","+psize.height() + ")");   
                                      //TODO fix this where is does not break when I move the resource around.
                                      InputStream in = getClass().getClassLoader().getResourceAsStream("com/xxx/model/state/type/paperwork_logo.pdf");
                                      PdfReader pdfReaderLogo = new PdfReader(in);
                                      Rectangle psizeLogo = pdfReaderLogo.getPageSize(1);
                              //        System.out.println("psizeLogo: (" + psizeLogo.width()+","+psizeLogo.height() + ")");
                                      // step 1: creation of a document-object
                                      Document document = new Document(psize);
                      //              // step 2: we create a writer that listens to the document
                                      PdfWriter writer = PdfWriter.getInstance(document, baos2);
                                      PdfContentByte cb = writer.getDirectContent();
                                      PdfImportedPage pageReport = writer.getImportedPage(pdfReader, 1);
                                      PdfImportedPage pageLogo   = writer.getImportedPage(pdfReaderLogo, 1);
                                      cb.addTemplate(pageReport, 0, 0);
                                      cb.addTemplate(pageLogo, .4f, 0f, 0f, .4f, 30, 725);
                                      bytes = baos2.toByteArray();
                                  } catch (Exception de) { 
                              return bytes;       

                      • 9. Re: Put an Image in a header with iText
                        Bolke de Bruin Newbie

                        It is not possible indeed. Besides that the HeaderFooter class is deprecated.

                        Due to the way iText is setup it is not very easy to integrate a footer / header in the way Seam is using/wants to use it. I know I offered to integrate it once but the complexity and cruftiness (of iText) does not appeal very much to me.

                        You might be able to get away with generating a PDF from seam, getting it from the document store then using PdfStamper to add a PdfPTable to the end of the document. Of course you would need to adjust the margins of your document to accomodate for this.

                        Another option would be JapserReports as mentioned elsewhere in this thread. But for me this was not flexible enough either. Too many assumptions which made generating my document as complex as coding it myself with plain iText.

                        • 10. Re: Put an Image in a header with iText
                          Hans Schlegel Newbie

                          After lot of trial and error....
                          I found this Solution.

                          <f:facet name="header">
                              <p:font size="14">
                                  <p:header borderWidthTop="0" borderWidthBottom="0.4" alignment="left"><p:paragraph><p:html>
                                      <table cellpadding="0" cellspacing="0" border="0" width="100%">
                                              <td align="left">Request Liste<sup><font size="1">12</font></sup></td>
                                              <td align="center">Erstellt von: UserName<img src="http://localhost:8080/xyz/img/appl/codes.gif" alt=" " border="0" width="32" height="32"/></td>
                                              <td align="right">Erstellt am:<h:outputText value=" "/><h:outputText value="#{printDate}"><s:convertDateTime locale="#{locale}"
                                  <p:footer borderWidthTop=".4" borderWidthBottom="0" alignment="left">Request Liste - Seite [<p:pageNumber/>]</p:footer>

                          • 11. Re: Put an Image in a header with iText
                            Dan Allen Master

                            Would you be willing to simplify this and make it an FAQ. You would likely help out a lot of folks.

                            As for the discussion of Seam's PDF component tags versus iText, you have to remember that the component tags are there to make creating PDFs quickly and easy. Sure, they are not going to support every feature. If there is feature that should be simple, and it is common enough, we should get that into the component tags (file a JIRA). But don't assume that you can satisfy every business needs with the component tags. If they were as complex as iText, then they wouldn't have any advantage. You may even want to use JasperReports to create more complex PDFs without touching iText.

                            • 12. Re: Put an Image in a header with iText
                              SIBUT Nicolas Newbie

                              Thanks a lot, it works good!!!

                              I have just a little problem, my image is higher than the hight of the header. Is it possible to change the hight of the header or must I change my image size?

                              • 13. Re: Put an Image in a header with iText
                                Hans Schlegel Newbie

                                Hi Dan

                                Sorry for my very late answer...

                                Your suggestion to make a FAQ would be no problem - but to be honest I don't like the p-tags very much.

                                You are right - if the seam pdf library would support most of the iText functionality then it would be
                                too difficult to use - but now it is to limiting - at least for me.

                                My suggestion would be to integrate the Flying Saucer XHTML Renderer - it is not perfect and it is tricky
                                to setup - but ounce done its easy to work with.

                                This could be an addition to the seam pdf library.

                                See: http://www.seamframework.org/Community/HowCanIPrintMyPage

                                • 14. Re: Put an Image in a header with iText
                                  Rowan Tetu Newbie

                                  How can you get rid of the absolute path when referencing the image?

                                  Through the fasesContext component somehow?

                                  Any ideas?