1. 程式人生 > >Itext筆記之pdf頁首頁尾

Itext筆記之pdf頁首頁尾

效果展示:





原始碼:

public class App 
{
    public static void main( String[] args ) throws Exception {
        Document document = new Document(PageSize.A4,50,50,50,50);
        PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("/home/hzq/a.pdf"));
        BaseFont baseFont = BaseFont.createFont("simhei.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
        Font font = new Font(baseFont,12);
        Rectangle rect = new Rectangle(36, 54, 559, 788);
        rect.setBorderColor(BaseColor.BLACK);
        writer.setBoxSize("art", rect);

        HeaderFoot headerFoot = new HeaderFoot();
        writer.setPageEvent(headerFoot);

        document.open();
        Paragraph paragraph = new Paragraph("This is First Page 第一頁",font);
        document.add(paragraph);

        document.newPage();
        document.add(new Paragraph("This is Second Page 第二頁",font));

        document.newPage();
        document.add(new Paragraph("This is Second Page 第三頁",font));
        document.close();

    }
}

HeaderFoot類:

public class HeaderFoot extends PdfPageEventHelper {
    private String header;
    private PdfTemplate template;

    public void setHeader(String header){
        this.header = header;
    }
    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        template = writer.getDirectContent().createTemplate(30,16);
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        BaseFont baseFont = null;
        try {
            baseFont = BaseFont.createFont("simhei.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Font font = new Font(baseFont,12);
        Rectangle rect = writer.getBoxSize("art");
        //新增頁首
        switch(writer.getPageNumber() % 2) {
            case 0:
                ColumnText.showTextAligned(writer.getDirectContent(),
                        Element.ALIGN_LEFT, new Phrase("偶數頁頁首",font),
                        rect.getLeft(230), rect.getTop(), 0);
                break;
            case 1:
                ColumnText.showTextAligned(writer.getDirectContent(),
                        Element.ALIGN_LEFT, new Phrase("奇數頁頁首",font),
                        rect.getLeft(230), rect.getTop(), 0);
                break;
        }
        //新增頁碼
        try {
            PdfPTable table = new PdfPTable(3);
            //設定表格的寬度有兩種方法,分別如下
            table.setTotalWidth(new float[]{80,12,40});
            //將寬度鎖定
            table.setLockedWidth(true);
            table.getDefaultCell().setFixedHeight(12);
            table.getDefaultCell().setBorder(Rectangle.NO_BORDER);

            PdfPCell cell = new PdfPCell( new Paragraph("第 "+writer.getPageNumber()+" 頁/共 ",font));
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);

            PdfPCell cell1 = new PdfPCell(Image.getInstance(template));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell1);

            PdfPCell cell2 = new PdfPCell(new Paragraph("頁",font));
            cell2.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell2);
            table.writeSelectedRows(0,-1,235,50,writer.getDirectContent());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onCloseDocument(PdfWriter writer, Document document) {
        ColumnText.showTextAligned(template,Element.PARAGRAPH,new Phrase(String.valueOf(writer.getPageNumber()-1)),2,2,0);
    }
}

jar包:
    <dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>itextpdf</artifactId>
      <version>5.4.5</version>
    </dependency>
    <dependency>
      <groupId>com.itextpdf.tool</groupId>
      <artifactId>xmlworker</artifactId>
      <version>5.4.5</version>
    </dependency>

專案下載路徑:http://download.csdn.net/detail/mmd0308/9886919