PDF添加水印

来源:互联网 发布:哥哥帅体验知乎 编辑:程序博客网 时间:2024/05/16 04:22

 

今天碰到要在原有的PDF上添加REPRINT的水印标记,查了好多文档,总算解决了,大概做一下记录吧:

首先添加 ITextPDF等jar包,具体网上查一下:

D:\Java\Itext_jar\iTextAsian.jar
D:\Java\Itext_jar\itextpdf-5.3.2.jar
D:\Java\Itext_jar\itext-pdfa-5.3.2.jar
D:\Java\Itext_jar\itext-xtra-5.3.2.jar

代码:

package one;

import java.io.*; 
import com.itextpdf.text.*; 
import com.itextpdf.text.pdf.*; 
 
/**
 * PDF生成
 */ 
public class OperatePDF { 
 
    public static void buidPDF(String pdfFile, String imageFile, 
            String waterMarkName, int permission) { 
        try { 
            File file = File.createTempFile("tempFile", ".pdf"); // 创建临时文件 
 
            // 生成PDF 
            if (createPDFFile(file)) { 
                waterMark(file.getPath(), imageFile, pdfFile, waterMarkName, 
                        permission); // 添加水印 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
 
    /**
     * 创建PDF文件
     * 
     * @param file
     *            临时文件
     * @return 成功/失败
     */ 
    public static boolean createPDFFile(File file) { 
        Rectangle rect = new Rectangle(PageSize.A4); 
        Document doc = new Document(rect, 50.0F, 50.0F, 50.0F, 50.0F); 
        try { 
            PdfWriter.getInstance(doc, new FileOutputStream(file)); 
            doc.open(); 
 
            BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", 
                    "Identity-H", true);// 使用系统字体 
 
            Font font = new Font(bf, 14.0F, 0); 
            font.setStyle(37); // 设置样式 
            font.setFamily("宋体"); // 设置字体 
 
            Paragraph p = new Paragraph("付 款 通 知 书\r\n", font); 
            p.setAlignment(1); 
            doc.add(p); 
            doc.close(); 
            return true; 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return false; 
    } 
 
    public static void waterMark(String inputFile, String imageFile, 
            String outputFile, String waterMarkName, int permission) { 
        try { 
            PdfReader reader = new PdfReader(inputFile); 
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream( 
                    outputFile)); 
 
            BaseFont base = BaseFont.createFont( 
                    "C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);// 使用系统字体 
 
            int total = reader.getNumberOfPages() + 1; 
            Image image = Image.getInstance(imageFile); 
 
            // 图片位置 
            image.setAbsolutePosition(200, 330); 
            PdfContentByte under; 
            int j = waterMarkName.length(); 
            char c = 0; 
            int rise = 0; 


            for (int i = 1; i < total; i++) { 
                rise = 45;  //水印整体倾斜度
                under = stamper.getUnderContent(i); 
                under.beginText(); 
                under.setFontAndSize(base, 30);  //设置字体以及大小
                
                
                PdfGState gs = new PdfGState();
                gs.setFillOpacity(0.3f);    // 设置透明度为0.3
                under.setGState(gs);

                under.showTextAligned(Element.ALIGN_CENTER, "R E P R I N T", 600 / 2 - 50,800 / 2 - 50, 45);    //每个字体的倾斜度 ,以及水印的起始位置 
  
                // 添加水印图片 
                under.addImage(image); 
 
                // 画个圈 
                under.ellipse(250, 450, 350, 550); 
                under.setLineWidth(1f); 
                under.stroke(); 
            } 
            stamper.close(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
 
    public static void main(String[] args) { 
        String imageFilePath = "E:/pyt/tmp/char.png"; // 水印图片路径 
        String pdfFilePath = "E:/pyt/tmp/Manual Refund Letter_1455696197092.pdf"; // 文件生成路径 
        String pdfFilePath_copy = "E:/pyt/tmp/Manual Refund Letter_1455696197092_copy.pdf"; // 文件生成路径 
      //  buidPDF(pdfFilePath, imageFilePath, "正版授权", 16);
        waterMark(pdfFilePath, imageFilePath,pdfFilePath_copy, "REPRINT", 16);
       
    } 

 

找了别人的代码,然后又在网上查了点东西添加进去了,还有生成PDF文件的代码没删,放着吧。

 

 

0 0
原创粉丝点击