简体   繁体   English

如何使用ITextRenderer在每个页面上添加页眉和页脚

[英]How to add header and footer on each page using ITextRenderer

I am stuck to the following condition where i need to generate pdf. 我坚持以下条件,我需要生成PDF格式。

What i am doing is 我在做什么

String A="<html><head></head><body>Some start  html content"

String B="here the string is create **dynamic** where this can be of any length"

 String C=here its end section of html..... </body></html>"

 String finalHtml=A+B+C;

In my controller in using ITextRenderer in order to generate the PDF. 在我的控制器中使用ITextRenderer来生成PDF。 The content of my controller is shown below. 我的控制器的内容如下所示。

@RequestMapping(value="/genratePdf",method=RequestMethod.GET)
public void getPdfGenration(HttpServletRequest request,HttpServletResponse response){ 

    response.setContentType("application/pdf");
    StringBuffer buf = new StringBuffer();
    buf.append(finalHtml);
    try{
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new StringBufferInputStream(buf.toString()));
        ITextRenderer renderer = new ITextRenderer()
        renderer.layout();
        java.io.OutputStream os = response.getOutputStream();
        renderer.createPDF(os);
        os.flush();
        os.close();
     }catch (Exception ex) {
        ex.printStackTrace();
     }

This works fine and return me pdf but the problem i am facing is to put header content on every page .Any help or suggestion. 这工作正常并返回给我pdf,但我面临的问题是在每个页面上放置标题内容。任何帮助或建议。 I am using spring 3.1.and jar i used is iText-paulo-155.jar, itext-2.1.0.jar Thanks in advance. 我使用的是Spring 3.1.and我使用的是iText-paulo-155.jar,itext-2.1.0.jar提前感谢。

If you have a direct access to iText's PdfWriter , you can use PdfPageEvent interface. 如果您可以直接访问iText的PdfWriter ,则可以使用PdfPageEvent接口。 An example of how to create headers and footers via PdfPageEvent can be found here: http://itextpdf.com/examples/iia.php?id=103 有关如何通过PdfPageEvent创建页眉和页脚的示例,请访问: httpPdfPageEvent

But if you want to add something like 'page X of Y' to your header or footer - you should use PdfStamper instead. 但是如果你想在页眉或页脚中添加“Y页面X”之类的东西 - 你应该使用PdfStamper代替。 An example for it can be found here: http://itextpdf.com/examples/iia.php?id=118 可以在此处找到它的示例: http//itextpdf.com/examples/iia.php?id = 118

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

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