简体   繁体   English

ItextPDF添加页眉和页脚复杂格式

[英]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: 几天前,我问了一个问题: Itext PDF如何将预格式化的HTML添加到PDF中 ,但是@ bruno-lowagie告诉我要按照此现有线程上的说明进行操作: 如何向页面添加HTML页眉和页脚 ,我遵循了仔细阅读说明,但发现该方法适用于简单的html页眉和页脚,例如:

<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: 但是我的用例需要在像图像的页眉和页脚中添加更复杂的数据,因此我尝试使用具有指向同一服务器中图像的img元素的标头,如下所示:

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:

在标题上输出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. PS:很抱歉给我带来新的不便,我希望我的问题很清楚。

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: 在我的Managed Bean中,设置Header,Footer,outputfile等:

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 编辑2 :标题应如下所示

输出PDF中的样本标题

If you are talking about the html code "as is" it's like this: 如果您说的是“按原样”的html代码,则如下所示:

<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. 因此,您需要大约22pt的高度(832-810)来绘制所有页眉材料。

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). 此标头需要两个段落加上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. 然后根据您的要求逐步减少它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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