pdf复制功能

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) {
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值