该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
Java操作pdf原理就是覆盖原来的内容!这是我总结百度的代码,自己写的几个方法。
jar包依赖:
com.itextpdf
itextpdf
5.5.11
com.itextpdf
itext-asian
5.2.0
代码:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class SchoolModel {
public static int fondSize = 12;
/**
* 添加图片
* @param input
* @param output
* @throws Exception
*/
public static String addImage(String input, String output, String imgPath){
PdfReader reader = null;
PdfStamper stamper = null;
try {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(output)));
reader = new PdfReader(input);
stamper = new PdfStamper(reader, out);
int total = reader.getNumberOfPages();
Image image = Image.getInstance(imgPath);
/* 设置图片的位置 */
image.setAbsolutePosition(0, 0);
image.setAlignment(Image.UNDERLYING);
/* 设置图片的大小 */
image.scaleAbsolute(595, 842);
for (int i = 1; i <= total; i++){
PdfContentByte content= stamper.getOverContent(i);// 在内容上方加水印
content.addImage(image);
}
}catch (Exception e){
e.printStackTrace();
return "发生异常:" + e.getMessage();
} finally{
try {
stamper.close();
} catch (DocumentException e) {
e.printStackTrace();
return "发生异常:" + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "发生异常:" + e.getMessage();
}
reader.close();
}
return null;
}
/**
* 添加页码
* @param input
* @param output
* @throws Exception
*/
public static String addPage(String input, String output){
PdfReader reader = null;
PdfStamper stamper = null;
Document document = new Document();
Rectangle pageSize = new Rectangle(PageSize.A4);
document.setPageSize(pageSize);
try {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(output)));
reader = new PdfReader(input);
stamper = new PdfStamper(reader, out);
int total = reader.getNumberOfPages();
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
for (int i = 1; i <= total; i++){
PdfContentByte canvas = stamper.getUnderContent(i);
String foot1 = "第 " + i + " 页 /" + total + "共";
float len = bf.getWidthPoint(foot1, fondSize);
Phrase p = new Phrase();
p.setFont(new Font(bf, fondSize, Font.NORMAL, BaseColor.BLUE));
p.add(foot1);
ColumnText.showTextAligned(
canvas,
PdfContentByte.ALIGN_CENTER,
p,
(document.rightMargin() + document.right()
+ document.leftMargin() - document.left() - len) / 2.0F + 20F,
document.bottom() - 20F,
0);
}
}catch (Exception e){
e.printStackTrace();
return "发生异常:" + e.getMessage();
} finally{
try {
stamper.close();
} catch (DocumentException e) {
e.printStackTrace();
return "发生异常:" + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "发生异常:" + e.getMessage();
}
reader.close();
}
return null;
}
/**
* 添加logo
* @param input
* @param output
* @throws Exception
*/
public static String addLogo(String input, String output, String logoPath){
PdfReader reader = null;
PdfStamper stamper = null;
Document document = new Document();
Rectangle pageSize = new Rectangle(PageSize.A4);
document.setPageSize(pageSize);
try {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(output)));
reader = new PdfReader(input);
stamper = new PdfStamper(reader, out);
int total = reader.getNumberOfPages();
if (total < 1){
return "当前pdf文件错误!";
}
PdfContentByte pcb = stamper.getUnderContent(1);
File file = new File(logoPath);
if (!file.exists()){
return "logo文件不存在!";
}
// pcb.addImage()方法要在pcb.beginText();pcb.endText();之外调用,
// 否则生成的PDF打开时会报错: An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem.
byte[] logoBytes = new byte[1000 * 1024]; // 此处数组大小要比logo图片大小要大, 否则图片会损坏;能够直接知道图片大小最好不过.
@SuppressWarnings("resource")
FileInputStream logoIs = new FileInputStream(file);
int logoSize = logoIs.read(logoBytes); // 尝试了一下,此处图片复制不完全,需要专门写个方法,将InputStream转换成Byte数组,详情参考org.apache.io.IOUtils.java的toByteArray(InputStream in)方法
if(logoSize > 0){
byte[] logo = new byte[logoSize];
System.arraycopy(logoBytes, 0, logo, 0, logoSize);
Image image = Image.getInstance(logo);// 如果直接使用logoBytes,并且图片是jar包中的话,会报图片损坏异常;本地图片可直接getInstance时候使用路径。
image.setAbsolutePosition(document.right(120), document.top(60)); // 设置图片显示位置
image.scalePercent(12); // 按照百分比缩放
pcb.addImage(image);
}
}catch (Exception e){
e.printStackTrace();
return "发生异常:" + e.getMessage();
} finally{
try {
stamper.close();
} catch (DocumentException e) {
e.printStackTrace();
return "发生异常:" + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "发生异常:" + e.getMessage();
}
reader.close();
}
return null;
}
}