Itext PDF 编辑 合并 图片转PDF以及表单域

Itext PDF 编辑 合并 图片转PDF以及表单域

编辑PDF

 
 
 
x
 
 
 
 
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.junit.Test;
 
import java.io.FileOutputStream;
 
/**
 * @Classname ItextPDF
 * @Description TODO
 * @Date 2021/9/17 0017 16:27
 * @Created by Mr.Fang
 */
public class ItextPDF {
 
    /**
     * @return void
     * @Description 编辑现有 PDF 文件
     * @date 2021/9/17 0017 16:28
     * @auther Mr.Fang
     **/
    @Test
    public void editPdf() throws Exception {
        String src = "C:/IText合同.pdf";
        String desc = "C:/IText合同-edit.pdf";
        // 这里字体使用了本地字体,中文不设置字体 PDF 文件上显示空白
        BaseFont baseFont_zh = BaseFont
            .createFont("C:\\Windows\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //创建一个 pdf 读入流
        PdfReader reader = new PdfReader(src);
        //根据一个 PdfReader 创建一个 pdfStamper.用来生成新的pdf.
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(desc));
        //指定 PDF 文件页面
        PdfContentByte canvas = stamper.getUnderContent(1);
        canvas.saveState(); // 保存状态
        canvas.beginText(); // 开始写入
        canvas.setFontAndSize(baseFont_zh, 12); // 设置字体 大小
        canvas.setTextMatrix(138, 687); // 坐标 横坐标 纵坐标 这里如果 px取值 需要 px*0.75
        canvas.showText("2021-09-17");
        canvas.endText(); // 写入结束
        canvas.restoreState(); // 恢复状态
        stamper.setFormFlattening(true); // 禁止编辑
        stamper.close(); // 关闭流
    }

}
 

编辑PDF附件

合并PDF

 
 
 
xxxxxxxxxx
 
 
 
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname ItextPDF
 * @Description TODO
 * @Date 2021/9/17 0017 16:27
 * @Created by Mr.Fang
 */
public class ItextPDF {

    /**
     * @return void
     * @Description 合并 PDF
     * @date 2021/9/17 0017 16:56
     * @auther Mr.Fang
     **/
    @Test
    public void mergePdf() throws Exception {
        List<String> list = new ArrayList(); // 需要合并的文件路径
        list.add("C:/IText合同-edit.pdf");
        list.add("C:/自我申明.pdf");
        String desc = "C:/IText合同-merge.pdf";
        Document document = new Document(new PdfReader(list.get(0)).getPageSize(1));
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(desc));
        document.open();
        for (int i = 0; i < list.size(); i++) {
            PdfReader reader = new PdfReader(list.get(i));
            int n = reader.getNumberOfPages();
            for (int j = 1; j <= n; j++) {
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, j);
                copy.addPage(page);
            }
        }
        document.close();
    }

}
 

合并PDF附件

图片转PDF

 
 
 
xxxxxxxxxx
 
 
 
 
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname ItextPDF
 * @Description TODO
 * @Date 2021/9/17 0017 16:27
 * @Created by Mr.Fang
 */
public class ItextPDF {

    /**
     * @return void
     * @Description 图片转 PDF
     * @date 2021/9/17 0017 17:20
     * @auther Mr.Fang
     **/
    @Test
    public void imageToPdf() throws Exception {
        List<String> list = new ArrayList(); // 需要合并的文件路径
        list.add("C:/1.png");
        list.add("C:/2.png");
        list.add("C:/3.png");
        String desc = "C:/imageToPdf.pdf";
        // 创建一个 document 流
        Document document = new Document(PageSize.A4);
        FileOutputStream fos = new FileOutputStream(desc);
        PdfWriter.getInstance(document, fos);
        //打开文档
        document.open();
        // 添加PDF文档的某些信息,比如作者,主题等等.必须 open 以后才起作用
        document.addTitle("标题:合并图片");
        document.addAuthor("作者:Mr.Fang");
        document.addSubject("主题:图片转PDF");
        document.addCreator("创建者:Mr.Fang");
        for (String source : list) {
            //获取图片的宽高
            com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(source);
            // 也可设置页面尺寸
//            float imageHeight = image.getScaledHeight();
//            float imageWidth = image.getScaledWidth();
//            document.setPageSize(new Rectangle(imageWidth,imageHeight));
            //图片居中
            image.scaleToFit(PageSize.A4); // 图片大小缩小在 A4 尺寸以内自适应
            image.setAlignment(Image.ALIGN_CENTER); // 对齐方式居中
            image.setCompressionLevel(0); // 压缩 0-9
//            image.scalePercent(40); // 百分比缩小图片
            //新建一页添加图片
            document.newPage();
            document.add(image);
        }
        document.close();
        fos.flush();
        fos.close();
    }

}

 

图片转PDF附件

表单域

  1. 创建一个 word,当然直接用 PDF也可以。
  2. 转成 PDF 文件,使用Adobe Acrobat DC 工具扫描添加表单域,其他工具也可以。
  3. 另存为 PDF 文件
 
 
 
xxxxxxxxxx
 
 
 
 
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname ItextPDF
 * @Description TODO
 * @Date 2021/9/17 0017 16:27
 * @Created by Mr.Fang
 */
public class ItextPDF {

    /**
     * @return void
     * @Description 表单域
     * @date 2021/9/17 0017 17:40
     * @auther Mr.Fang
     **/
    @Test
    public void formPdf() throws Exception {
        String src = "C:/Itext表单域-form.pdf";
        String desc = "C:/form.pdf";
        // key value 赋值
        Map<String, String> map = new HashMap<>();
        map.put("fill_1", "表单1");
        map.put("fill_2", "表单2");
        map.put("fill_3", "表单3");
        map.put("fill_4", "表单4");
        // 设置字体否则中文不显示
        BaseFont baseFont_zh = BaseFont
        .createFont("C:\\Windows\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //创建一个 pdf 读入流
        PdfReader reader = new PdfReader(src);
        //根据一个 PdfReader 创建一个pdfStamper 用来生成新的pdf.
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(desc));
        AcroFields form = stamper.getAcroFields();
        form.addSubstitutionFont(baseFont_zh);
        //遍历map装入数据
        for (Map.Entry<String, String> entry : map.entrySet()) {
            form.setField(entry.getKey(), entry.getValue());
        }
        stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
        stamper.close();
    }

}

 

表单域附件

maven

 
 
 
xxxxxxxxxx
 
 
 
 
<dependency>
   <groupId>com.itextpdf</groupId>
   <artifactId>itextpdf</artifactId>
   <version>5.5.13</version>
</dependency>
 

其他

表单域

表单域2

 

 

posted @ 2021-09-18 15:11  天葬  阅读(375)  评论(0编辑  收藏  举报