zoukankan      html  css  js  c++  java
  • 【java】itext pdf 分页

    importjava.io.FileOutputStream;

    importcom.lowagie.text.Document;

    importcom.lowagie.text.Element;

    importcom.lowagie.text.ExceptionConverter;

    importcom.lowagie.text.Font;

    importcom.lowagie.text.PageSize;

    importcom.lowagie.text.Paragraph;

    importcom.lowagie.text.pdf.BaseFont;

    importcom.lowagie.text.pdf.PdfContentByte;

    importcom.lowagie.text.pdf.PdfPageEventHelper;

    importcom.lowagie.text.pdf.PdfTemplate;

    importcom.lowagie.text.pdf.PdfWriter;

    public classPdfExport extendsPdfPageEventHelper {

            publicPdfTemplate tpl;

            publicBaseFont bf;

            public static voidmain (String[] args) {

                Document document = newDocument (PageSize.A4, 20, 20, 20, 20);

                try{

                    PdfWriter writer = PdfWriter.getInstance(document, newFileOutputStream ("D:\\HelloItext.pdf") );

                    writer.setPageEvent (newPdfExport() );

                    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

                    document.open();

                    Paragraph title = newParagraph ("测试内容。。。。", newFont (bfChinese,15) );

                    title.setAlignment (Element.ALIGN_CENTER);

                    document.add (title);

                } catch(Exception de) {

                    de.printStackTrace();

                }

                document.close();

            }

            public voidonOpenDocument (PdfWriter writer, Documentdocument) {

                try{

                    tpl= writer.getDirectContent().createTemplate(100, 100);

                    bf= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

                } catch(Exception e) {

                    throw newExceptionConverter (e);

                }

            }

            public voidonEndPage (PdfWriter writer, Documentdocument) {

                //在每页结束的时候把“第x页”信息写道模版指定位置

                PdfContentByte cb =writer.getDirectContent();

                cb.saveState();

                String text = "第"+writer.getPageNumber() + "页,共";

                cb.beginText();

                cb.setFontAndSize (bf, 8);

                cb.setTextMatrix (460, 786); //定位“第x页,共” 在具体的页面调试时候需要更改这xy的坐标

                cb.showText (text);

                cb.endText();

                cb.addTemplate (tpl, 492, 786); //定位“y页” 在具体的页面调试时候需要更改这xy的坐标

                cb.saveState();

                cb.stroke();

                cb.restoreState();

                cb.closePath();//sanityCheck();

            }

            public voidonCloseDocument (PdfWriter writer, Documentdocument) {

                //关闭document的时候获取总页数,并把总页数按模版写道之前预留的位置

                tpl.beginText();

                tpl.setFontAndSize (bf, 8);

                tpl.showText (Integer.toString(writer.getPageNumber() - 1) + "页");

                tpl.endText();

                tpl.closePath();//sanityCheck();

            }

    }


  • 相关阅读:
    析构函数中的virtual是否必要?
    程序员必看的书
    UML类图几种关系的总结
    VS 2008的64位编译环境的安装和使用
    VB获取CAD属性值
    30分钟LINQ教程
    ADO.NET
    C#编写Windows服务程序图文教程
    Json的序列化与反序列化以及乱入的k_BackingField
    C#中的委托(Delegate)和事件(Event)
  • 原文地址:https://www.cnblogs.com/ae6623/p/4416614.html
Copyright © 2011-2022 走看看