简体   繁体   中英

ItextPDF Adding Headers and Footers Complex Format

A couple of days ago I asked this question: Itext PDF How To Add HTML Pre-formatted to PDF , but @bruno-lowagie told me to follow instructions on this existing thread: How To Add HTML Headers And Footers to a Page , I followed carefully the instructions, but found that that approach works for simple html headers and footers like:

<h1>Header Only Line</h1>

or

<h2>Footer Only Line</h2>

But my use case requires to add more complex data in header and footer like images, So I tried with a header that has an img element pointing to an image in the same server like this:

http://localhost:8080/DocGen/resources/images/main_header.jpg

I added some start and end "marks" to see if they got processed so my header was like this:

<p>----Header Start---</p>

<p><img alt="" src="http://localhost:8080/DocGen/resources/images/main_header.jpg" style="height:126px; width:683px" /></p>

<p>--Header End--</p>

But I'm getting an output pdf like this:

在标题上输出PDF NO图像

Edited : As you can see it doesn't show the image and didn't also show my end mark.

What should I do to successfully add headers and footers with images embedded?

Thanks a lot.

PS: Sorry for any inconvenience as I am new here, and I hope my question is clear.

EDIT : Code, it's like in the other thread:

     public class HtmlHeaderFooter {
        private String DEST = null;//"results/events/html_header_footer.pdf";
        private String HEADER = null;
        private String FOOTER = null;

        private float leftMargin;
        private float rightMargin;
        private float topMargin;
        private float bottomMargin;

        private Rectangle pageSize = null;

        public class HeaderFooter extends PdfPageEventHelper {
            protected ElementList header;
            protected ElementList footer;
            public HeaderFooter() throws IOException {

                header = XMLWorkerHelper.parseToElementList(HEADER, null);
                footer = XMLWorkerHelper.parseToElementList(FOOTER, null);
            }
            @Override
            public void onEndPage(PdfWriter writer, Document document) {
                try {

                    ColumnText ct = new ColumnText(writer.getDirectContent());
                    ct.setSimpleColumn(new Rectangle(36, 832, 559, 810));
                    for (Element e : header) {
                        System.out.println("Element on header: " + e.toString());
                        ct.addElement(e);
                    }
                    ct.go();
                    ct.setSimpleColumn(new Rectangle(36, 10, 559, 32));
                    for (Element e : footer) {
                        System.out.println("Element on footer: " + e.toString());
                        ct.addElement(e);
                    }
                    ct.go();

                } catch (DocumentException de) {
                    throw new ExceptionConverter(de);
                }
            }
        }

     public void createPdfAlt(String outputFile, String inputFile){
            Document document = new Document(pageSize, leftMargin, rightMargin, topMargin, bottomMargin);

            FileOutputStream outputStream;
            try {
                outputStream = new FileOutputStream(DEST);
                //System.out.println("Doc: " + document.);
                PdfWriter writer = PdfWriter.getInstance(document, outputStream);
                  writer.setPageEvent(new HeaderFooter());
document.open();

            PdfContentByte cb = writer.getDirectContent();

            // Load existing PDF
            PdfReader reader = new PdfReader(new FileInputStream(inputFile));
            PdfImportedPage page = writer.getImportedPage(reader, 1); 
        //  document.setPageSize(reader.getPageSize(1));
            // Copy first page of existing PDF into output PDF
            document.newPage();
            cb.addTemplate(page, 0, 0);
document.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

In my Managed Bean I set Header, Footer, outputfile and so on:

HtmlHeaderFooter htmlHeaderFooter = new HtmlHeaderFooter();
            htmlHeaderFooter.setFOOTER(footerContent);
            htmlHeaderFooter.setHEADER(headerContent);


            //htmlHeaderFooter.setPageSize(xml2pdf.getPageSize());
            htmlHeaderFooter.setPageSize(com.itextpdf.text.PageSize.A4); 
            htmlHeaderFooter.setLeftMargin(template2Export.getLeftMargin());
            htmlHeaderFooter.setRightMargin(template2Export.getRightMargin());
            htmlHeaderFooter.setTopMargin(template2Export.getSuperiorMargin());
            htmlHeaderFooter.setBottomMargin(template2Export.getInferiorMargin());

            htmlHeaderFooter.setDEST("salidaConHeaderAndFooter.pdf");
            htmlHeaderFooter.createPdfAlt("PDFCompleto1.pdf", "test3.pdf");

EDIT 2 : Header should look like this

输出PDF中的样本标题

If you are talking about the html code "as is" it's like this:

<p>----Header Start---</p>

    <p><img alt="" src="http://localhost:8080/DocGen/resources/images/main_header.jpg" style="height:126px; width:683px" /></p>

    <p>--Header End--</p>

You draw the header here:

ct.setSimpleColumn(new Rectangle(36, 832, 559, 810));

So you allow for about 22pt height (832 - 810) to draw all the header material.

On the other hand your header is expected to display this

<p>----Header Start---</p>

<p><img alt="" src="http://localhost:8080/DocGen/resources/images/main_header.jpg" style="height:126px; width:683px" /></p>

<p>--Header End--</p>

This header requires two paragraphs plus 126px (94.5pt). Thus, it does not fit. Consequently, only the first paragraph (which is the only header content that fits) is drawn.

You might want to start by allowing a lot of space, eg

ct.setSimpleColumn(new Rectangle(36, 832, 559, 0));

and then reduce it step by step according to your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM