發表於 程式分享

使用iText將多個pdf檔合併成一個pdf檔

範例如下,請參考:

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfSmartCopy;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

public class mergePDF {
    public static void main(String args[]) throws IOException, DocumentException {
        //merge
        String[] files = {"filled_out_job_application.pdf", "replace_stream.pdf"};
        mergeFiles(files, "a.pdf", true);
        mergeFiles(files, "b.pdf", false);
    }

    public void mergeFiles(String[] files, String result, boolean smart) throws IOException, DocumentException {
        Document document = new Document();
        PdfCopy copy;
        if (smart)
            copy = new PdfSmartCopy(document, new FileOutputStream(result));
        else
            copy = new PdfCopy(document, new FileOutputStream(result));
        document.open();

        com.itextpdf.text.pdf.PdfReader[] reader = new com.itextpdf.text.pdf.PdfReader[3];
        for (int i = 0; i < files.length; i++) {
            reader[i] = new com.itextpdf.text.pdf.PdfReader(files[i]);
            copy.addDocument(reader[i]);
            copy.freeReader(reader[i]);
            reader[i].close();
        }
        document.close();
    }
}
發表於 程式分享

使用iText套表產生新的pdf檔~一併解決中文字顯示issue

1.先使用Adobe Acrobat Pro DC 產生 Field Properties(Edit PDF => Prepare Form)

1.PNG

2.下載iText7

http://developers.itextpdf.com/itext-7-java

3.程式如下

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.*;

ublic class testPDF { 
    public static final String SRC = "pdf/src/subscribe.pdf";
    public static final String DEST = "pdf/dest/subscribe_result.pdf";
 
    public static void main(String args[]) throws IOException {
        new testPDF().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); 
 
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        Map<String, PdfFormField> fields = form.getFormFields();
        //中文字issue 
        PdfFont font = PdfFontFactory.createFont("font/kaiu.ttf", PdfEncodings.IDENTITY_H, true);
        fields.get("personal.name").setFont(font); 
        fields.get("personal.loginname").setFont(font);
        fields.get("personal.password").setFont(font);
        fields.get("personal.reason").setFont(font);
        
        fields.get("personal.name").setValue("測試名字").setBackgroundColor(Color.ORANGE);
        fields.get("personal.loginname").setValue("NickName").setBackgroundColor(Color.WHITE);
        fields.get("personal.password").setValue("a12345678").setBackgroundColor(Color.WHITE);
        fields.get("personal.reason").setValue("原因是...").setBackgroundColor(Color.WHITE);
        
        form.flattenFields();
        pdfDoc.close();
    }
}

4.執行後產生之PDF如下圖

2.PNG

5.若使用iText5,中文字的處理如下範例

        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader,
                new FileOutputStream(dest));
        
        AcroFields fields = stamper.getAcroFields();
        //中文字處理
        BaseFont font = BaseFont.createFont("font/kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED );
        fields.setFieldProperty("name","textfont",font,null);        
        fields.setField("name", "ROC 台灣");

        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();