pdf复制功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hehyyoulan/article/details/88942706

package com.fineway.hcs.file.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

// BaseFont bfComic14 = BaseFont.createFont(“c:\windows\fonts\SIMYOU.TTF”,
//
// BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//
// Font font = new Font(bfComic0, 14);
//
// String text1 = "啊发生的发球特工是大哥是法国时的风格是This is the quite popular True Type "+new java.util.Date();
//
// document.add(new Paragraph(text1, font));

public class PDFTemplet {
public static void main(String args[]) throws Exception {
// 模板路径 C:/hhywk/lib/success_temp.pdf
String templatePath = “C:/hhywk/lib/success_temp.pdf”;
// String templatePath = “D:/fail.pdf”;
// 生成的新文件路径
// String newPDFPath = “D:/success_temp.pdf”;
String newPDFPath = “C:/hhywk/lib/success.pdf”;
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
out = new FileOutputStream(newPDFPath);
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
// BaseFont bf = BaseFont.createFont("/SIMYOU.TTF",
// BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
// BaseFont bf = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”,
// BaseFont.EMBEDDED);
BaseFont bf = BaseFont.createFont(“C:\hhywk\lib\simsun.ttf”,
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 12, Font.NORMAL);
form.addSubstitutionFont(bf);
stamper.setFormFlattening(false);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
int pagecount= reader.getNumberOfPages();
for(int i=1 ;i<pagecount+1;i++){
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
copy.addPage(importPage);
}
doc.close();
} catch (IOException e) {
} catch (DocumentException e) {
}
}
}

猜你喜欢

转载自blog.csdn.net/hehyyoulan/article/details/88942706