1. 程式人生 > >java生成PDF,各種格式、樣式、水印都有

java生成PDF,各種格式、樣式、水印都有

程式碼中有兩處需要圖片,請自行替換。 一個是水印、一個是手指。 

下面是預覽:

//建立PDF時需要的工具類

public class DataUtil {

    /**
* 分割路徑
* @param path
* @return 返回分割後的路徑
*/
public static String[] separatePath(String path){
if(StringUtils.isBlank(path)){
return null;
}
String[] sep = path.split("\\.");
return new String[]{sep[0],sep[1]};
}

}

//建立PDF的工具類

package com.workmanagement.util;


import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;


import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;




public class PDFUtil {

public static void main(String[] args) throws Exception {
imageWaterMark(createPDF(), "F://title.jpg"); 
}


/**
* 建立PDF文件
* @return
* @throws Exception
* @throws docException
*/
public static String createPDF() throws Exception {
        
    //輸出路徑
    String outPath = "F://test.pdf";//DataUtil.createTempPath(".pdf");
   
        //設定紙張
        Rectangle rect = new Rectangle(PageSize.A4);
   
        //建立文件例項
        Document doc=new Document(rect);
        
        //新增中文字型
        BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        
        //設定字型樣式
        Font textFont = new Font(bfChinese,11,Font.NORMAL); //正常
        Font redTextFont = new Font(bfChinese,11,Font.NORMAL,Color.RED); //正常,紅色
        Font boldFont = new Font(bfChinese,11,Font.BOLD); //加粗
        Font redBoldFont = new Font(bfChinese,11,Font.BOLD,Color.RED); //加粗,紅色
        Font firsetTitleFont = new Font(bfChinese,22,Font.BOLD); //一級標題
        Font secondTitleFont = new Font(bfChinese,15,Font.BOLD); //二級標題
        Font underlineFont = new Font(bfChinese,11,Font.UNDERLINE); //下劃線斜體
        
        //手指圖片
        Image hand = Image.getInstance("F:\\hand.png");
        
        //建立輸出流
        PdfWriter.getInstance(doc, new FileOutputStream(new File(outPath)));
        
        doc.open();
        doc.newPage();
        
        //段落  
        Paragraph p1 = new Paragraph();  
        //短語
        Phrase ph1 = new Phrase();  
        //塊
        Chunk c1 = new Chunk("*********", boldFont) ;
        Chunk c11 = new Chunk("(信用報告提供機構l ogo)", textFont) ;
        //將塊新增到短語
        ph1.add(c1);
        ph1.add(c11);
        //將短語新增到段落
        p1.add(ph1);
        //將段落新增到短語
        doc.add(p1);
        
        p1 = new Paragraph();  
        ph1 = new Phrase(); 
        Chunk c2 = new Chunk("報告編號:", boldFont) ;
        Chunk c22 = new Chunk("SN-201604010001", textFont) ;
        ph1.add(c2);
        ph1.add(c22);
        p1.add(ph1);
        doc.add(p1);
        
        p1 = new Paragraph("企業信用報告", firsetTitleFont);
        p1.setLeading(50);
        p1.setAlignment(Element.ALIGN_CENTER);
        doc.add(p1);


        p1 = new Paragraph("(企業版)", textFont);
        p1.setLeading(20);
        p1.setAlignment(Element.ALIGN_CENTER);
        doc.add(p1);
            
        p1 = new Paragraph();  
        p1.setLeading(20);
        p1.setAlignment(Element.ALIGN_CENTER);
        ph1 = new Phrase(); 
        Chunk c3 = new Chunk("查詢時間:", boldFont) ;
        Chunk c33 = new Chunk("2016-04-01 00:00:00", textFont) ;
        Chunk c4 = new Chunk(leftPad("查詢人:", 10), boldFont) ;
        Chunk c44 = new Chunk("admin(使用者登入名)", textFont) ;
        ph1.add(c3);
        ph1.add(c33);
        ph1.add(c4);
        ph1.add(c44);
        p1.add(ph1);
        doc.add(p1);
            
        p1 = new Paragraph("報告說明", secondTitleFont);
        p1.setLeading(50);
        p1.setAlignment(Element.ALIGN_CENTER);
        doc.add(p1);
        
        p1 = new Paragraph(" ");  
        p1.setLeading(30);
        doc.add(p1);
        
        p1 = new Paragraph();  
        ph1 = new Phrase(); 
        Chunk c5 = new Chunk("1.本報告由", textFont) ;
        Chunk c6 = new Chunk("****信用資訊服務中心", underlineFont) ;
        c6.setSkew(0, 30);
        Chunk c7 = new Chunk(" 出具,依據截止報告時間小微企業信用資訊資料庫記錄的資訊生成。除異議標註和查詢記錄外,報告中的資訊均由相關報數機構和資訊主體提供,", textFont);
        Chunk c8 = new Chunk("不保證其真實性和準確性,但承諾在資訊整合、彙總、展示的全過程中保持客觀、中立的地位。", textFont) ;
        ph1.add(c5);
        ph1.add(c6);
        ph1.add(c7);
        ph1.add(c6);
        ph1.add(c8);
        p1.add(ph1);
        doc.add(p1);


        p1 = new Paragraph();  
        ph1 = new Phrase(); 
        Chunk c9 = new Chunk("2.異議標註是", textFont) ;
        Chunk c10 = new Chunk(" 對報告中的資訊記錄或對資訊主體所作的說明。", textFont);
        ph1.add(c9);
        ph1.add(c6);
        ph1.add(c10);
        p1.add(ph1);
        doc.add(p1);
            
        p1 = new Paragraph("3.資訊主體說明是資訊主體對報數機構提供的資訊記錄所作的簡要說明。", textFont);  
        doc.add(p1);
        
        p1 = new Paragraph();  
        ph1 = new Phrase(); 
        Chunk c12 = new Chunk("4.資訊主體有權對本報告中的內容提出異議。如有異議,可聯絡報數機構,也可到", textFont) ;
        Chunk c13 = new Chunk(" 提出異議申請。", textFont);
        ph1.add(c12);
        ph1.add(c6);
        ph1.add(c13);
        p1.add(ph1);
        doc.add(p1);
        
        p1 = new Paragraph("5.更多諮詢,請致電客戶服務熱線********。", textFont);  
        doc.add(p1);
        
        p1 = new Paragraph("資訊概況", secondTitleFont);
        p1.setSpacingBefore(30);
        p1.setSpacingAfter(30);
        p1.setAlignment(Element.ALIGN_CENTER);
        doc.add(p1);
        
        p1 = new Paragraph("資訊主體實繳資本**萬元,****年**月**日股東****公司向***公司轉讓**萬元股權。", textFont);  
        p1.setFirstLineIndent(23);
        p1.setSpacingAfter(15);
        doc.add(p1);


        p1 = new Paragraph("資訊主體於20**年首次與金融機構發生信貸關係,報告期內,共在**家金融機構辦理過信貸業務,目前**家金融機構對資訊主體授信共**萬元,**家金融機構**萬元貸款未結清。提供對外擔保****萬元。", textFont);  
        p1.setFirstLineIndent(23);
        p1.setSpacingAfter(15);
        doc.add(p1);


        p1 = new Paragraph("報告期內,資訊主體共有**條人民銀行表彰記錄、**條外匯管理表彰記錄、**條環保部門表彰記錄、**條質監部門表彰記錄、**條稅務部門表彰記錄、**條財政部門表彰記錄、**條工商部門表彰記錄、**條衛生部門表彰記錄、**條海關部門表彰記錄、**條其他部門表彰記錄。", textFont);  
        p1.setFirstLineIndent(23);
        p1.setSpacingAfter(15);
        doc.add(p1);
        
        p1 = new Paragraph("報告期內,資訊主體共有**條人民銀行處罰記錄、**條外匯管理處罰記錄、**條環保部門處罰記錄、**條質監部門處罰記錄、**條稅務部門處罰記錄、**條財政部門處罰記錄、**條工商部門處罰記錄、**條衛生部門處罰記錄、**條海關部門處罰記錄、**條其他部門處罰記錄。共有**條欠稅記錄、**條欠水/電/汽記錄、**條法院判決執行記錄。", textFont);  
        p1.setFirstLineIndent(23);
        doc.add(p1);
        
        p1 = new Paragraph("資訊主體最近一次繳納社會保險為****年**月**日,最近一次繳納公積金為****年**月**日。", textFont);  
        p1.setFirstLineIndent(23);
        doc.add(p1);
        
        p1 = new Paragraph("目前,報告中共有*條報數機構說明、*條資訊主體說明、*條服務中心說明。", textFont);  
        p1.setFirstLineIndent(23);
        p1.setSpacingAfter(15);
        doc.add(p1);
        
        p1 = new Paragraph();  
        p1.setSpacingAfter(10);
        ph1 = new Phrase(); 
        Chunk c14 = new Chunk(hand, 0, 0);
        Chunk c15 = new Chunk(leftPad("身份資訊", 7), boldFont);
        ph1.add(c14);
        ph1.add(c15);
        p1.add(ph1);
        doc.add(p1);
        
        // 建立一個有4列的表格  
        PdfPTable table = new PdfPTable(4);
        table.setTotalWidth(new float[]{ 105, 170, 105, 170 }); //設定列寬
        table.setLockedWidth(true); //鎖定列寬
        
        PdfPCell cell;
        cell = new PdfPCell(new Phrase("企業名稱", boldFont));
        cell.setBorderWidthLeft(3);
        cell.setBorderWidthTop(3);
        cell.setMinimumHeight(30); //設定單元格高度
        cell.setUseAscender(true); //設定可以居中
        cell.setHorizontalAlignment(Cell.ALIGN_CENTER); //設定水平居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        cell = new PdfPCell();
        cell.setBorderWidthRight(3);
        cell.setBorderWidthTop(3);
        cell.setUseAscender(true); //設定可以居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        cell.setColspan(3);
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("統一社會信用程式碼(組織機構程式碼)", boldFont));
        cell.setBorderWidthLeft(3);
        cell.setMinimumHeight(40); //設定單元格高度
        cell.setUseAscender(true); //設定可以居中
        cell.setHorizontalAlignment(Cell.ALIGN_CENTER); //設定水平居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", textFont));
        cell.setUseAscender(true); //設定可以居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("註冊地址", boldFont));
        cell.setUseAscender(true); //設定可以居中
        cell.setHorizontalAlignment(Cell.ALIGN_CENTER); //設定水平居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", textFont));
        cell.setBorderWidthRight(3);
        cell.setUseAscender(true); //設定可以居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("行業分類", boldFont));
        cell.setBorderWidthLeft(3);
        cell.setMinimumHeight(30); //設定單元格高度
        cell.setUseAscender(true); //設定可以居中
        cell.setHorizontalAlignment(Cell.ALIGN_CENTER); //設定水平居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", textFont));
        cell.setUseAscender(true); //設定可以居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("實繳資本", boldFont));
        cell.setUseAscender(true); //設定可以居中
        cell.setHorizontalAlignment(Cell.ALIGN_CENTER); //設定水平居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", textFont));
        cell.setBorderWidthRight(3);
        cell.setUseAscender(true); //設定可以居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        
        cell = new PdfPCell(new Phrase("資料來源", boldFont));
        cell.setBorderWidthLeft(3);
        cell.setBorderWidthBottom(3);
        cell.setMinimumHeight(30); //設定單元格高度
        cell.setUseAscender(true); //設定可以居中
        cell.setHorizontalAlignment(Cell.ALIGN_CENTER); //設定水平居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", textFont));
        cell.setBorderWidthBottom(3);
        cell.setUseAscender(true); //設定可以居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("採集時間", boldFont));
        cell.setBorderWidthBottom(3);
        cell.setUseAscender(true); //設定可以居中
        cell.setHorizontalAlignment(Cell.ALIGN_CENTER); //設定水平居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(" ", textFont));
        cell.setBorderWidthRight(3);
        cell.setBorderWidthBottom(3);
        cell.setUseAscender(true); //設定可以居中
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中
        table.addCell(cell);
        doc.add(table);
        
        p1 = new Paragraph();  
        p1.setSpacingBefore(20);
        p1.setSpacingAfter(10);
        ph1 = new Phrase(); 
        Chunk c17 = new Chunk(hand, 0, 0);
        Chunk c18 = new Chunk(leftPad("出資情況", 7), boldFont);
        ph1.add(c17);
        ph1.add(c18);
        p1.add(ph1);
        doc.add(p1);
        
        table = new PdfPTable(9);
        table.setTotalWidth(new float[]{80, 60, 60, 50, 60, 60, 60, 60, 60}); //設定列寬
        table.setLockedWidth(true); //鎖定列寬
        
        table = createCell(table, new String[]{"出資方名稱","證件型別","證件號碼","幣種","出資金額","出資方式","出資佔比","資料來源","採集時間"}, 2, 9);
        doc.add(table);
        
        p1 = new Paragraph();  
        p1.setSpacingBefore(20);
        p1.setSpacingAfter(10);
        ph1 = new Phrase(); 
        Chunk c62 = new Chunk(hand, 0, 0);
        Chunk c63 = new Chunk(leftPad("行政處罰資訊", 9), boldFont);
        ph1.add(c62);
        ph1.add(c63);
        p1.add(ph1);
        doc.add(p1);
        
        table = new PdfPTable(10);
        table.setTotalWidth(new float[]{55, 55, 55, 55, 55, 55, 55, 55, 55, 55}); //設定列寬
        table.setLockedWidth(true); //鎖定列寬
        
        table = createCell(table, new String[]{"處罰文書號","裁定處罰部門","處罰時間","違法或違規行為描述","涉及金額","處罰金額","處罰決定","整改情況","資料來源","採集時間"}, 2, 10);
        doc.add(table);
        
        p1 = new Paragraph();  
        p1.setSpacingBefore(20);
        p1.setSpacingAfter(10);
        ph1 = new Phrase(); 
        Chunk c65 = new Chunk(hand, 0, 0);
        Chunk c66 = new Chunk(leftPad("司法資訊", 7), boldFont);
        ph1.add(c65);
        ph1.add(c66);
        p1.add(ph1);
        doc.add(p1);
        
        table = new PdfPTable(7);
        table.setTotalWidth(new float[]{70, 80, 80, 80, 80, 80, 80}); //設定列寬
        table.setLockedWidth(true); //鎖定列寬
        
        table = createCell(table, new String[]{"案號","立案法院","立案日期","案由","執行標的","執行標的金額","已執行標的"}, 2, 7);
        table = createCell(table, new String[]{"已執行標的金額","執行標的日期","案件狀態","結案日期","執行結案方式","資料來源","採集時間"}, 2, 7);
        doc.add(table);
        
        p1 = new Paragraph();  
        p1.setSpacingBefore(20);
        p1.setSpacingAfter(10);
        ph1 = new Phrase(); 
        Chunk c68 = new Chunk(hand, 0, 0);
        Chunk c69 = new Chunk(leftPad("銀行授信", 7), boldFont);
        ph1.add(c68);
        ph1.add(c69);
        p1.add(ph1);
        doc.add(p1);
        
        table = new PdfPTable(8);
        table.setTotalWidth(new float[]{40, 100, 75, 75, 75, 60, 60, 60}); //設定列寬
        table.setLockedWidth(true); //鎖定列寬
        
        table = createCell(table, new String[]{"編號","授信金融機構","授信額度","起始日期","終止日期","銀行內部評級結果","資料來源","採集時間"}, 2, 8);
        doc.add(table);
        
        p1 = new Paragraph();  
        p1.setSpacingBefore(20);
        p1.setSpacingAfter(10);
        ph1 = new Phrase(); 
        Chunk c71 = new Chunk(hand, 0, 0);
        Chunk c72 = new Chunk(leftPad("銀行貸款", 7), boldFont);
        ph1.add(c71);
        ph1.add(c72);
        p1.add(ph1);
        doc.add(p1);
        
        table = new PdfPTable(10);
        table.setTotalWidth(new float[]{30, 55, 75, 55, 55, 55, 55, 55, 55, 55}); //設定列寬
        table.setLockedWidth(true); //鎖定列寬
        
        table = createCell(table, new String[]{" ","編號","貸款金融機構","貸款餘額","貸款日期","到期日","五級分類","欠息金額","資料來源","採集時間"}, 1, 10);
        table = createCell(table, null, 3, 10);
        table = createCell(table, null, 3, 10);
        doc.add(table);
        
        p1 = new Paragraph();  
        p1.setSpacingBefore(20);
        p1.setSpacingAfter(10);
        ph1 = new Phrase(); 
        Chunk c74 = new Chunk(hand, 0, 0);
        Chunk c75 = new Chunk(leftPad("貼現", 5), boldFont);
        ph1.add(c74);
        ph1.add(c75);
        p1.add(ph1);
        doc.add(p1);
        
        table = new PdfPTable(8);
        table.setTotalWidth(new float[]{40, 155, 60, 60, 60, 60, 60, 60}); //設定列寬
        table.setLockedWidth(true); //鎖定列寬
        
        table = createCell(table, new String[]{"編號","貼現金融機構","貼現金額","貼現日","貼現率","五級分類","資料來源","採集時間"}, 2, 8);
        doc.add(table);
        
        p1 = new Paragraph();  
        p1.setSpacingBefore(20);
        p1.setSpacingAfter(10);
        ph1 = new Phrase(); 
        Chunk c85 = new Chunk(hand, 0, 0);
        Chunk c86 = new Chunk(leftPad("資訊主體說明", 9), boldFont);
        ph1.add(c85);
        ph1.add(c86);
        p1.add(ph1);
        doc.add(p1);
        
        table = new PdfPTable(1);
        table.setTotalWidth(new float[]{500}); //設定列寬
        table.setLockedWidth(true); //鎖定列寬
        
        table = createCell(table, null, 1,1);
        doc.add(table);
        
        p1 = new Paragraph();  
        p1.setSpacingBefore(20);
        p1.setSpacingAfter(10);
        ph1 = new Phrase(); 
        Chunk c87 = new Chunk(hand, 0, 0);
        Chunk c88 = new Chunk(leftPad("查詢記錄", 7), boldFont);
        ph1.add(c87);
        ph1.add(c88);
        p1.add(ph1);
        doc.add(p1);
        
        table = new PdfPTable(4);
        table.setTotalWidth(new float[]{220, 100, 100, 100}); //設定列寬
        table.setLockedWidth(true); //鎖定列寬
        
        table = createCell(table, new String[]{"序號","查詢日期","查詢使用者","查詢原因"}, 3, 4);
        doc.add(table);
        
        p1 = new Paragraph();  
        p1.setSpacingBefore(20);
        p1.setSpacingAfter(10);
        ph1 = new Phrase(); 
        Chunk c89 = new Chunk("異議標註(用於記錄資訊主體對某類資訊提出異議處理情況,置於每類資訊項下方):", redBoldFont);
        Chunk c90 = new Chunk("資訊主體於2016年**月**日提出異議:我公司從未發生過***;業務發生機構於2016年**月**日提交說明:該筆資訊確實存在;資訊主體於2016年**月**日提出宣告:該筆資訊為我公司***所致。(在每一板塊下詳細記錄資訊主體提出異議處理過程和結果。)", redTextFont);
        ph1.add(c89);
        ph1.add(c90);
        p1.add(ph1);
        doc.add(p1);
        
        doc.close();
        return outPath;
    }

/**
* 建立單元格
* @param table
* @param row
* @param cols
* @return
* @throws IOException 
* @throws DocumentException 
*/
private static PdfPTable createCell(PdfPTable table, String[] title, int row, int cols) throws DocumentException, IOException{
//新增中文字型
        BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font font = new Font(bfChinese,11,Font.BOLD);
        
for(int i = 0; i < row; i++){

for(int j = 0; j < cols; j++){

PdfPCell cell = new PdfPCell();
       
if(i==0 && title!=null){//設定表頭
cell = new PdfPCell(new Phrase(title[j], font)); //這樣表頭才能居中
if(table.getRows().size() == 0){
cell.setBorderWidthTop(3);
}
}

if(row==1 && cols==1){ //只有一行一列
cell.setBorderWidthTop(3);
}

if(j==0){//設定左邊的邊框寬度
cell.setBorderWidthLeft(3);
}

if(j==(cols-1)){//設定右邊的邊框寬度
cell.setBorderWidthRight(3);
}

if(i==(row-1)){//設定底部的邊框寬度
cell.setBorderWidthBottom(3);
}

cell.setMinimumHeight(40); //設定單元格高度
       cell.setUseAscender(true); //設定可以居中
       cell.setHorizontalAlignment(Cell.ALIGN_CENTER); //設定水平居中
       cell.setVerticalAlignment(Cell.ALIGN_MIDDLE); //設定垂直居中

table.addCell(cell);
}
}

return table;
}

    /**
     * 加水印(字串)
     * @param inputFile 需要加水印的PDF路徑
     * @param outputFile 輸出生成PDF的路徑
     * @param waterMarkName 水印字元
     */
    public static void stringWaterMark(String inputFile, String waterMarkName) {
try {
String[] spe = DataUtil.separatePath(inputFile);
    String outputFile = spe[0] + "_WM." + spe[1];
   
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));


//新增中文字型
       BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);


int total = reader.getNumberOfPages() + 1;


PdfContentByte under;
int j = waterMarkName.length();
char c = 0;
int rise = 0;
//給每一頁加水印
for (int i = 1; i < total; i++) {
rise = 400;
under = stamper.getUnderContent(i);
under.beginText();
under.setFontAndSize(bfChinese, 30);
under.setTextMatrix(200, 120);
for (int k = 0; k < j; k++) {
under.setTextRise(rise);
c = waterMarkName.charAt(k);
under.showText(c + "");
}


// 新增水印文字
under.endText();
}
stamper.close();
} catch (Exception e) {
e.printStackTrace();
}
}
    
    /**
     * 加水印(圖片)
     * @param inputFile 需要加水印的PDF路徑
     * @param outputFile 輸出生成PDF的路徑
     * @param imageFile 水印圖片路徑
     */
    public static void imageWaterMark(String inputFile, String imageFile) {
    try {
    String[] spe = DataUtil.separatePath(inputFile);
    String outputFile = spe[0] + "_WM." + spe[1];
   
    PdfReader reader = new PdfReader(inputFile);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
   
    int total = reader.getNumberOfPages() + 1;
   
    Image image = Image.getInstance(imageFile);
    image.setAbsolutePosition(-100, 0);//座標
    image.scaleAbsolute(800,1000);//自定義大小
    //image.setRotation(-20);//旋轉 弧度
    //image.setRotationDegrees(-45);//旋轉 角度
    //image.scalePercent(50);//依照比例縮放
   
    PdfGState gs = new PdfGState();
    gs.setFillOpacity(0.2f);// 設定透明度為0.2


    PdfContentByte under;
    //給每一頁加水印
    for (int i = 1; i < total; i++) {
    under = stamper.getUnderContent(i);
    under.beginText();
    // 新增水印圖片
    under.addImage(image);
        under.setGState(gs);
    }
    stamper.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    
    /**
     * 設定左邊距
     * @param str
     * @param i
     * @return
     */
    public static String leftPad(String str, int i) {
        int addSpaceNo = i-str.length();
        String space = ""; 
        for (int k=0; k<addSpaceNo; k++){
                space= " "+space;
        };
        String result =space + str ;
        return result;
     }
    
    /**
     * 設定模擬資料
     * @param list
     * @param num
     */
    public static void add(List<String> list,int num){
        for(int i=0;i<num;i++){
            list.add("test"+i);
        }
    }
    
    /**
     * 設定間距
     * @param tmp
     * @return
     */
    public static String printBlank(int tmp){
          String space="";
          for(int m=0;m<tmp;m++){
              space=space+" ";
          }
          return space;
    }


}