使用itextpdf处理pdf文件


package tflv.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JLabel;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfTest {

	public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException {
//		Document document = new Document(PageSize.A4);
        File file = new File("C:\\Users\\tflv\\Downloads\\附件1:合同单-demo.pdf");
//        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
//        document.open();
//        PdfContentByte cb = writer.getDirectContent();
//        writer.setPageEvent(new MyHeaderFooter());// 页眉/页脚
 
// Load existing PDF
        File file2 = new File("C:\\Users\\tflv\\Downloads\\附件1:合同单.pdf");
        PdfReader reader = new PdfReader(new FileInputStream(file2));
//        PdfImportedPage page = writer.getImportedPage(reader, 1);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(file));
//        PdfWriter writer = stamper.getWriter();
        onEndPage(stamper);
        shiying(reader, stamper);
        stamper.close();
        reader.close();
// Copy first page of existing PDF into output PDF
//        document.newPage();
//        cb.addTemplate(page, 0, 0);
        
//        document.close();
	}

	public static void onEndPage(PdfStamper writer) throws DocumentException, IOException {
		Font hfFont = new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.NORMAL);
	        PdfPTable table = new PdfPTable(1);
	        try {
	            table.setTotalWidth(PageSize.A4.getWidth() - 100);
//	            table.setWidths(new int[] { 24, 24, 3});
	            table.setLockedWidth(true);
	            table.getDefaultCell().setFixedHeight(-10);
	            table.getDefaultCell().setBorder(0);
	            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
	            table.addCell(new Paragraph("我是页眉/页脚", hfFont));// 可以直接使用addCell(str),不过不能指定字体,中文无法显示
	            
	            // 将页眉写到document中,位置可以指定,指定到下面就是页脚
	            table.writeSelectedRows(0, -1, 50, PageSize.A4.getHeight() - 20, writer.getOverContent(1));
	        } catch (Exception de) {
	            throw new ExceptionConverter(de);
	        }

    }
	
	public static void shiying(PdfReader reader, PdfStamper stamper) throws DocumentException, IOException {
		BaseFont hfFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
		PdfGState gs = new PdfGState();
        //改透明度
        gs.setFillOpacity(0.5f);
        gs.setStrokeOpacity(0.4f);
        int total = reader.getNumberOfPages() + 1;

        PdfContentByte under;
        // 添加一个水印
        for (int i = 1; i < total; i++) {
            // 在内容上方加水印
            under = stamper.getOverContent(i);
            //在内容下方加水印
            //under = stamper.getUnderContent(i);
            gs.setFillOpacity(0.5f);
            under.setGState(gs);
            under.beginText();
            //改变颜色
            under.setColorFill(BaseColor.LIGHT_GRAY);
            //改水印文字大小
            under.setFontAndSize(hfFont, 150);
            under.setTextMatrix(70, 200);
            //后3个参数,x坐标,y坐标,角度
            under.showTextAligned(Element.ALIGN_CENTER, "科源程序", 300, 350, 55);

            under.endText();
        }
	}

}

你可能感兴趣的:(pdf,java)