1. 程式人生 > >Java 給已有 PDF 增加頁碼

Java 給已有 PDF 增加頁碼

如果只需要頁碼展示 “第 N 頁”請參照第一個程式碼段內容即可;
如果需要頁碼展示 “第 N 頁 共 Y 頁”, 請參照第二段程式碼段內容.

package com.dhec.checkReport.util;

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie
.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfPageEventHelper; import com.lowagie.text.pdf.PdfTemplate; import com.lowagie.text.pdf.PdfWriter; public class PdfExport extends PdfPageEventHelper { public PdfTemplate tpl;
public BaseFont bf; public static void main(String[] args) throws IOException { Document document = new Document(); try { PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("D:\\aaaa\\checkRepoetAction.pdf")); writer.setPageEvent
(new PdfExport()); BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); document.open(); Paragraph title = new Paragraph("測試內容。。。。", new Font(bfChinese, 15)); title.setAlignment(Element.ALIGN_CENTER); document.add(title); } catch (Exception de) { de.printStackTrace(); } document.close(); } public void onOpenDocument(PdfWriter writer, Document document) { try { tpl = writer.getDirectContent().createTemplate(100, 100); bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); } catch (Exception e) { throw new ExceptionConverter(e); } } public void onEndPage(PdfWriter writer, Document document) { // 在每頁結束的時候把“第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 void onCloseDocument(PdfWriter writer, Document document) { // 關閉document的時候獲取總頁數,並把總頁數按模版寫道之前預留的位置 tpl.beginText(); tpl.setFontAndSize(bf, 8); tpl.showText(Integer.toString(writer.getPageNumber() - 1) + "頁"); tpl.endText(); tpl.closePath();// sanityCheck(); } }
try
          {
            //讀取檔案
            PdfReader pdfReader = new PdfReader("D:\\aaaa\\checkRepoetAction.pdf");

            //生成檔案
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("D:\\HelloWorld-modified.pdf"));


            for(int i=1; i<= pdfReader.getNumberOfPages(); i++)
            {
                PdfContentByte content = pdfStamper.getUnderContent(i);
                if(i == pdfReader.getNumberOfPages()){
                    //新增文字
                    BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
                    content.beginText();
                    content.setFontAndSize(font, 20);
                    content.setTextMatrix(200, 200);
                    content.showTextAligned(Element.CCITT_ENDOFBLOCK,"第   " + 1 + "  頁\t共   " + 9 + "頁",420,20,0);//左邊距、下邊距
                    content.endText();
                }
            }

            pdfStamper.close();

          } catch (IOException e) {
            e.printStackTrace();
          } catch (DocumentException e) {
            e.printStackTrace();
          }