itext 操作PDF
Posted slowdownthenrunfast
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了itext 操作PDF相关的知识,希望对你有一定的参考价值。
http://lichunhui.iteye.com/blog/1550584(操作PDF)
pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.citi.learn</groupId>
<artifactId>learn</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>learn-Lucene</artifactId>
<name>learn-Lucene</name>
<description>learn-Lucene</description>
<properties>
<lucene.version>2.9.2</lucene.version>
<itextpdf.version>5.5.2</itextpdf.version>
</properties>
<dependencies>
<!-- logger -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>$itextpdf.version</version>
</dependency>
<!-- Lucene -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>$lucene.version</version>
</dependency>
<!-- <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-common</artifactId>
<version>$lucene.version</version> </dependency> -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers</artifactId>
<version>$lucene.version</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queries</artifactId>
<version>$lucene.version</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>$lucene.version</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>$lucene.version</version>
</dependency>
<!-- Pao ding analyzer -->
<dependency>
<groupId>lucene</groupId>
<artifactId>paoding-analysis</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- compile source jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
测试类:
package com.lucene.test.pdf;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfTest
private static String FILE_DIR = "c:/temp/lucene/data/";
// 创建pdf文件
public void createPdf() throws FileNotFoundException, DocumentException
// Step 1—Create a Document.
Document document = new Document();
// Step 2—Get a PdfWriter instance.
PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR
+ "createSamplePDF.pdf"));
// Step 3—Open the Document.
document.open();
// Step 4—Add content.
document.add(new Paragraph("Hello World,shit"));
// Step 5—Close the Document.
document.close();
// 2、页面大小,页面背景色,页边空白,Title,Author,Subject,Keywords
public void createCplPdf() throws FileNotFoundException, DocumentException
// 页面大小
Rectangle rect = new Rectangle(PageSize.B5.rotate());
// 页面背景色
rect.setBackgroundColor(BaseColor.ORANGE);
Document doc = new Document(rect);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(
FILE_DIR + "createSamplePDF.pdf"));
// PDF版本(默认1.4)
writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
// 文档属性
doc.addTitle("Title@sample");
doc.addAuthor("Author@rensanning");
doc.addSubject("Subject@iText sample");
doc.addKeywords("Keywords@iText");
doc.addCreator("Creator@iText");
// 页边空白
doc.setMargins(10, 20, 30, 40);
doc.open();
doc.add(new Paragraph("Hello World"));
doc.close();
public static void main(String[] args) throws FileNotFoundException,
DocumentException
PdfTest test = new PdfTest();
// test.createPdf();
test.createCplPdf();
生成一个PDF
//Step 1—Create a Document.
Document document = new Document();
//Step 2—Get a PdfWriter instance.
PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createSamplePDF.pdf"));
//Step 3—Open the Document.
document.open();
//Step 4—Add content.
document.add(new Paragraph("Hello World"));
//Step 5—Close the Document.
document.close();
2、页面大小,页面背景色,页边空白,Title,Author,Subject,Keywords
//页面大小
Rectangle rect = new Rectangle(PageSize.B5.rotate());
//页面背景色
rect.setBackgroundColor(BaseColor.ORANGE);
Document doc = new Document(rect);
PdfWriter writer = PdfWriter.getInstance(doc, out);
//PDF版本(默认1.4)
writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
//文档属性
doc.addTitle("Title@sample");
doc.addAuthor("Author@rensanning");
doc.addSubject("Subject@iText sample");
doc.addKeywords("Keywords@iText");
doc.addCreator("Creator@iText");
//页边空白
doc.setMargins(10, 20, 30, 40);
doc.open();
doc.add(new Paragraph("Hello World"));
3、设置密码
PdfWriter writer = PdfWriter.getInstance(doc, out);
// 设置密码为:"World"
writer.setEncryption("Hello".getBytes(), "World".getBytes(),
PdfWriter.ALLOW_SCREENREADERS,
PdfWriter.STANDARD_ENCRYPTION_128);
doc.open();
doc.add(new Paragraph("Hello World"));
4添加Page
document.open();
document.add(new Paragraph("First page"));
document.add(new Paragraph(Document.getVersion()));
document.newPage();
writer.setPageEmpty(false);
document.newPage();
document.add(new Paragraph("New page"));
5添加水印(背景图)
//图片水印
PdfReader reader = new PdfReader(FILE_DIR + "setWatermark.pdf");
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR
+ "setWatermark2.pdf"));
Image img = Image.getInstance("resource/watermark.jpg");
img.setAbsolutePosition(200, 400);
PdfContentByte under = stamp.getUnderContent(1);
under.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, "DUPLICATE", 230, 430, 45);
over.endText();
//背景图
Image img2 = Image.getInstance("resource/test.jpg");
img2.setAbsolutePosition(0, 0);
PdfContentByte under2 = stamp.getUnderContent(3);
under2.addImage(img2);
stamp.close();
reader.close();
以上是关于itext 操作PDF的主要内容,如果未能解决你的问题,请参考以下文章