Itext5库的对PDF文件的基本操作(一)---新建文档,设置文档属性,设置文档密码
程序员文章站
2024-03-19 12:56:52
...
首先引入依赖,为了方便起见,我们采用maven方式构建demo项目。在pom中添加itext库的相应依赖。
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-xtra -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-xtra</artifactId>
<version>5.5.13</version>
</dependency>
<!-- 解析HTML 为PDF -->
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.13</version>
</dependency>
这里由于itext5是完全开源的。而itext7是商用付费的用的AGL协议。所以只用itext5.引入上图所示依赖即可。
一.创建pdf文档并修饰相关基本属性。
代码如下
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* @Author: TateBrown
* @date: 2018/9/9 20:22
* @param:
* @return:
*/
//创建pdf文档
public class Create{
public static final String RESULT="demo1.pdf";
public static void main(String[] args) throws FileNotFoundException, DocumentException {
Document document=new Document();
//获取一个pdfWriter实例
PdfWriter.getInstance(document,new FileOutputStream(RESULT));
document.open();
document.add(new Paragraph("Hello World!"));
document.close();
// //设置页面尺寸
// Rectangle pagesize=new Rectangle(216f,720f);
// //设置文档尺寸,后面四个参数为上下左右四边外边距
// Document document1=new Document(pagesize,36f,72f,108f,180f);
// //设置用户尽可能大的尺寸的pdf
// PdfWriter.getInstance(document,new FileOutputStream(RESULT)).setUserunit(75000f);
}
}
2.设置pdf文档的相关属性
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* @Author: TateBrown
* @date: 2018/9/9 20:57
* @param:
* @return:
*/
//设置pdf文件相关属性,比如页面大小,页面背景色,页边空白,Title,Author,Subject,Keywords
public class SetAttribute {
public static void main(String[] args) throws FileNotFoundException, DocumentException{
String src="demo2.pdf";
//页面大小
Rectangle rect=new Rectangle(PageSize.A4.rotate());
//页面背景色
rect.setBackgroundColor(BaseColor.BLUE);
Document doc=new Document(rect);
FileOutputStream out=new FileOutputStream(src);
PdfWriter writer=PdfWriter.getInstance(doc,out);
//版本
writer.setPdfVersion(PdfWriter.VERSION_1_4);
//设置相关属性
doc.addTitle("Title");
doc.addAuthor("TateBrown");
doc.addSubject("subject");
doc.addKeywords("Keywords");
doc.addCreator("cretor");
//设置页边空白间距,类似网页盒模型的margin
doc.setMargins(10,10,30,30);
doc.open();
doc.add(new Paragraph("Hello World!"));
doc.close();
}
}
3.设置pdf的权限,权限分为两种,使用者权限和创建者权限。两者可以做的事情是不一样的,所以分开。
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* @Author: TateBrown
* @date: 2018/9/9 21:05
* @param:
* @return:
*/
//设置pdf密码
public class SetPassword {
public static void main(String[] args) throws FileNotFoundException, DocumentException{
Document doc=new Document();
FileOutputStream out=new FileOutputStream("demo.pdf");
PdfWriter writer=PdfWriter.getInstance(doc,out);
writer.setEncryption("123".getBytes(),"123456".getBytes(),PdfWriter.ALLOW_SCREENREADERS,PdfWriter.STANDARD_ENCRYPTION_128);
//123为查看的密码,123456是作者使用的密码,可以进行修改
doc.open();
doc.add(new Paragraph("Hello World"));
doc.close();
}
}
4.向pdf中添加多种对象。片段,短语等。
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @Author: TateBrown
* @date: 2018/9/9 21:31
* @param:
* @return:
*/
//插入多种对象
public class AddChunk_Phrase_Paragraph_List {
public static void main(String[] args) throws FileNotFoundException, DocumentException,IOException{
Document doc=new Document();
PdfWriter pdfWriter=PdfWriter.getInstance(doc,new FileOutputStream("demo.pdf"));
doc.open();
doc.add(new Chunk("Hello"));
doc.add(new Chunk("123"));
doc.close();
}
}
5.添加页码
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* @Author: TateBrown
* @date: 2018/9/9 21:10
* @param:
* @return:
*/
public class AddPage {
public static void main(String[] args) throws FileNotFoundException, DocumentException{
Document doc=new Document();
doc.add(new Paragraph("First page"));
doc.newPage();
PdfWriter writer=PdfWriter.getInstance(doc, new FileOutputStream("demo.pdf"));
writer.setPageEmpty(false);
doc.add(new Paragraph("2"));
doc.close();
}
}
6.添加背景图水印图
/**
* @Author: TateBrown
* @date: 2018/9/9 21:16
* @param:
* @return:
*/
//添加背景图和水印
public class AddBackGroundPic {
public static void main(String[] args) throws FileNotFoundException, DocumentException,IOException{
PdfReader reader=new PdfReader("demo.pdf");
PdfStamper stamp=new PdfStamper(reader,new FileOutputStream("demo.pdf"));
Image img=Image.getInstance("background.jpg");
img.setAbsolutePosition(200,400);
PdfContentByte cb=stamp.getUnderContent(1);
cb.addImage(img);
//文字水印图
PdfContentByte over=stamp.getOverContent(2);
over.beginText();
//设置字体
BaseFont bf=BaseFont.createFont(BaseFont.HELVETICA,BaseFont.WINANSI, BaseFont.EMBEDDED);
over.setFontAndSize(bf,18);
over.setTextMatrix(30,30);
over.showTextAligned(Element.ALIGN_LEFT,"水印",230,430,45);
over.endText();
//背景图片
Image img2 = Image.getInstance("background.jpg");
img2.setAbsolutePosition(0, 0);
PdfContentByte under2 = stamp.getUnderContent(3);
under2.addImage(img2);
stamp.close();
reader.close();
}
}
未完待续- -