`
rensanning
  • 浏览: 3552736 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:38219
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:607619
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:682795
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:89539
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:402152
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69761
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:91835
社区版块
存档分类
最新评论

Java操作PDF之iText超入门

    博客分类:
  • Java
阅读更多
iText是著名的开放项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。

http://itextpdf.com/

版本:itextpdf-5.2.1.jar

1、生成一个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();


6、插入Chunk, Phrase, Paragraph, List
//Chunk对象: a String, a Font, and some attributes
document.add(new Chunk("China"));
document.add(new Chunk(" "));
Font font = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);
Chunk id = new Chunk("chinese", font);
id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
id.setTextRise(6);
document.add(id);
document.add(Chunk.NEWLINE);

document.add(new Chunk("Japan"));
document.add(new Chunk(" "));
Font font2 = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);
Chunk id2 = new Chunk("japanese", font2);
id2.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
id2.setTextRise(6);
id2.setUnderline(0.2f, -2f);
document.add(id2);
document.add(Chunk.NEWLINE);

//Phrase对象: a List of Chunks with leading
document.newPage();
document.add(new Phrase("Phrase page"));

Phrase director = new Phrase();
Chunk name = new Chunk("China");
name.setUnderline(0.2f, -2f);
director.add(name);
director.add(new Chunk(","));
director.add(new Chunk(" "));
director.add(new Chunk("chinese"));
director.setLeading(24);
document.add(director);

Phrase director2 = new Phrase();
Chunk name2 = new Chunk("Japan");
name2.setUnderline(0.2f, -2f);
director2.add(name2);
director2.add(new Chunk(","));
director2.add(new Chunk(" "));
director2.add(new Chunk("japanese"));
director2.setLeading(24);
document.add(director2);
		
//Paragraph对象: a Phrase with extra properties and a newline
document.newPage();
document.add(new Paragraph("Paragraph page"));

Paragraph info = new Paragraph();
info.add(new Chunk("China "));
info.add(new Chunk("chinese"));
info.add(Chunk.NEWLINE);
info.add(new Phrase("Japan "));
info.add(new Phrase("japanese"));
document.add(info);

//List对象: a sequence of Paragraphs called ListItem
document.newPage();
List list = new List(List.ORDERED);
for (int i = 0; i < 10; i++) {
	ListItem item = new ListItem(String.format("%s: %d movies",
			"country" + (i + 1), (i + 1) * 100), new Font(
			Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE));
	List movielist = new List(List.ORDERED, List.ALPHABETICAL);
	movielist.setLowercase(List.LOWERCASE);
	for (int j = 0; j < 5; j++) {
		ListItem movieitem = new ListItem("Title" + (j + 1));
		List directorlist = new List(List.UNORDERED);
		for (int k = 0; k < 3; k++) {
			directorlist.add(String.format("%s, %s", "Name1" + (k + 1),
					"Name2" + (k + 1)));
		}
		movieitem.add(directorlist);
		movielist.add(movieitem);
	}
	item.add(movielist);
	list.add(item);
}
document.add(list);


7、插入Anchor, Image, Chapter, Section
//Anchor对象: internal and external links
Paragraph country = new Paragraph();
Anchor dest = new Anchor("china", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLUE));
dest.setName("CN");
dest.setReference("http://www.china.com");//external
country.add(dest);
country.add(String.format(": %d sites", 10000));
document.add(country);

document.newPage();
Anchor toUS = new Anchor("Go to first page.", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLUE));
toUS.setReference("#CN");//internal
document.add(toUS);

//Image对象
document.newPage();
Image img = Image.getInstance("resource/test.jpg");
img.setAlignment(Image.LEFT | Image.TEXTWRAP);
img.setBorder(Image.BOX);
img.setBorderWidth(10);
img.setBorderColor(BaseColor.WHITE);
img.scaleToFit(1000, 72);//大小
img.setRotationDegrees(-30);//旋转
document.add(img);

//Chapter, Section对象(目录)
document.newPage();
Paragraph title = new Paragraph("Title");
Chapter chapter = new Chapter(title, 1);

title = new Paragraph("Section A");
Section section = chapter.addSection(title);
section.setBookmarkTitle("bmk");
section.setIndentation(30);
section.setBookmarkOpen(false);
section.setNumberStyle(
Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);

Section subsection = section.addSection(new Paragraph("Sub Section A"));
subsection.setIndentationLeft(20);
subsection.setNumberDepth(1);

document.add(chapter);


8、画图
//左右箭头
document.add(new VerticalPositionMark() {

	public void draw(PdfContentByte canvas, float llx, float lly,
			float urx, float ury, float y) {
		canvas.beginText();
		BaseFont bf = null;
		try {
			bf = BaseFont.createFont(BaseFont.ZAPFDINGBATS, "", BaseFont.EMBEDDED);
		} catch (Exception e) {
			e.printStackTrace();
		}
		canvas.setFontAndSize(bf, 12);
		
		// LEFT
		canvas.showTextAligned(Element.ALIGN_CENTER, String.valueOf((char) 220), llx - 10, y, 0);
		// RIGHT
		canvas.showTextAligned(Element.ALIGN_CENTER, String.valueOf((char) 220), urx + 10, y + 8, 180);
		
		canvas.endText();
	}
});

//直线
Paragraph p1 = new Paragraph("LEFT");
p1.add(new Chunk(new LineSeparator()));
p1.add("R");
document.add(p1);
//点线
Paragraph p2 = new Paragraph("LEFT");
p2.add(new Chunk(new DottedLineSeparator()));
p2.add("R");
document.add(p2);
//下滑线
LineSeparator UNDERLINE = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);
Paragraph p3 = new Paragraph("NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");
p3.add(UNDERLINE);
document.add(p3);



9、设置段落
Paragraph p = new Paragraph("In the previous example, you added a header and footer with the showTextAligned() method. This example demonstrates that it’s sometimes more interesting to use PdfPTable and writeSelectedRows(). You can define a bottom border for each cell so that the header is underlined. This is the most elegant way to add headers and footers, because the table mechanism allows you to position and align lines, images, and text.");

//默认
p.setAlignment(Element.ALIGN_JUSTIFIED);
document.add(p);

document.newPage();
p.setAlignment(Element.ALIGN_JUSTIFIED);
p.setIndentationLeft(1 * 15f);
p.setIndentationRight((5 - 1) * 15f);
document.add(p);

//居右
document.newPage();
p.setAlignment(Element.ALIGN_RIGHT);
p.setSpacingAfter(15f);
document.add(p);

//居左
document.newPage();
p.setAlignment(Element.ALIGN_LEFT);
p.setSpacingBefore(15f);
document.add(p);

//居中
document.newPage();
p.setAlignment(Element.ALIGN_CENTER);
p.setSpacingAfter(15f);
p.setSpacingBefore(15f);
document.add(p);


10、删除Page
FileOutputStream out = new FileOutputStream(FILE_DIR + "deletePage.pdf");

Document document = new Document();

PdfWriter writer = PdfWriter.getInstance(document, out);

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"));

document.close();

PdfReader reader = new PdfReader(FILE_DIR + "deletePage.pdf");
reader.selectPages("1,3");
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR
		+ "deletePage2.pdf"));
stamp.close();
reader.close();


11、插入Page
FileOutputStream out = new FileOutputStream(FILE_DIR + "insertPage.pdf");

Document document = new Document();

PdfWriter.getInstance(document, out);

document.open();
document.add(new Paragraph("1 page"));

document.newPage();
document.add(new Paragraph("2 page"));

document.newPage();
document.add(new Paragraph("3 page"));

document.close();

PdfReader reader = new PdfReader(FILE_DIR + "insertPage.pdf");
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR
		+ "insertPage2.pdf"));

stamp.insertPage(2, reader.getPageSize(1));

ColumnText ct = new ColumnText(null);
ct.addElement(new Paragraph(24, new Chunk("INSERT PAGE")));
ct.setCanvas(stamp.getOverContent(2));
ct.setSimpleColumn(36, 36, 559, 770);

stamp.close();
reader.close();


12、排序page
PdfWriter writer = PdfWriter.getInstance(doc, out);
writer.setLinearPageMode();

doc.open();
doc.add(new Paragraph("1 page"));
doc.newPage();
doc.add(new Paragraph("2 page"));
doc.newPage();
doc.add(new Paragraph("3 page"));
doc.newPage();
doc.add(new Paragraph("4 page"));
doc.newPage();
doc.add(new Paragraph("5 page"));

int[] order = {4,3,2,1};
writer.reorderPages(order);


13、目录
// Code 1
document.add(new Chunk("Chapter 1").setLocalDestination("1"));

document.newPage();
document.add(new Chunk("Chapter 2").setLocalDestination("2"));
document.add(new Paragraph(new Chunk("Sub 2.1").setLocalDestination("2.1")));
document.add(new Paragraph(new Chunk("Sub 2.2").setLocalDestination("2.2")));

document.newPage();
document.add(new Chunk("Chapter 3").setLocalDestination("3"));

// Code 2
PdfContentByte cb = writer.getDirectContent();
PdfOutline root = cb.getRootOutline();

// Code 3
@SuppressWarnings("unused")
PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage("1", false), "Chapter 1");

PdfOutline oline2 = new PdfOutline(root, PdfAction.gotoLocalPage("2", false), "Chapter 2");
oline2.setOpen(false);

@SuppressWarnings("unused")
PdfOutline oline2_1 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.1", false), "Sub 2.1");
@SuppressWarnings("unused")
PdfOutline oline2_2 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.2", false), "Sub 2.2");

@SuppressWarnings("unused")
PdfOutline oline3 = new PdfOutline(root, PdfAction.gotoLocalPage("3", false), "Chapter 3");



14、Header, Footer
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(FILE_DIR + "setHeaderFooter.pdf"));

writer.setPageEvent(new PdfPageEventHelper() {

	public void onEndPage(PdfWriter writer, Document document) {
		
		PdfContentByte cb = writer.getDirectContent();
        cb.saveState();

        cb.beginText();
		BaseFont bf = null;
		try {
			bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
		} catch (Exception e) {
			e.printStackTrace();
		}
		cb.setFontAndSize(bf, 10);
		
        //Header
		float x = document.top(-20);
		
        //左
        cb.showTextAligned(PdfContentByte.ALIGN_LEFT,
                           "H-Left",
                           document.left(), x, 0);
        //中
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
        					writer.getPageNumber()+ " page",
                           (document.right() + document.left())/2,
                           x, 0);
        //右
        cb.showTextAligned(PdfContentByte.ALIGN_RIGHT,
                           "H-Right",
                           document.right(), x, 0);

        //Footer
        float y = document.bottom(-20);

        //左
        cb.showTextAligned(PdfContentByte.ALIGN_LEFT,
                           "F-Left",
                           document.left(), y, 0);
        //中
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
        					writer.getPageNumber()+" page",
                           (document.right() + document.left())/2,
                           y, 0);
        //右
        cb.showTextAligned(PdfContentByte.ALIGN_RIGHT,
                           "F-Right",
                           document.right(), y, 0);

        cb.endText();
        
        cb.restoreState();
	}
});

doc.open();
doc.add(new Paragraph("1 page"));        
doc.newPage();
doc.add(new Paragraph("2 page"));        
doc.newPage();
doc.add(new Paragraph("3 page"));        
doc.newPage();
doc.add(new Paragraph("4 page"));


15、左右文字
PdfWriter writer = PdfWriter.getInstance(document, out);

document.open();

PdfContentByte canvas = writer.getDirectContent();

Phrase phrase1 = new Phrase("This is a test!left");
Phrase phrase2 = new Phrase("This is a test!right");
Phrase phrase3 = new Phrase("This is a test!center");
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase1, 10, 500, 0);
ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, phrase2, 10, 536, 0);
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase3, 10, 572, 0);


16、幻灯片放映
PdfWriter writer = PdfWriter.getInstance(doc, out);

writer.setPdfVersion(PdfWriter.VERSION_1_5);

writer.setViewerPreferences(PdfWriter.PageModeFullScreen);//全屏
writer.setPageEvent(new PdfPageEventHelper() {
	public void onStartPage(PdfWriter writer, Document document) {
		writer.setTransition(new PdfTransition(PdfTransition.DISSOLVE, 3));
		writer.setDuration(5);//间隔时间
	}
});

doc.open();
doc.add(new Paragraph("1 page"));
doc.newPage();
doc.add(new Paragraph("2 page"));
doc.newPage();
doc.add(new Paragraph("3 page"));
doc.newPage();
doc.add(new Paragraph("4 page"));
doc.newPage();
doc.add(new Paragraph("5 page"));


17、压缩PDF到Zip
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(FILE_DIR + "zipPDF.zip"));
for (int i = 1; i <= 3; i++) {
	ZipEntry entry = new ZipEntry("hello_" + i + ".pdf");
	zip.putNextEntry(entry);
	Document document = new Document();
	PdfWriter writer = PdfWriter.getInstance(document, zip);
	writer.setCloseStream(false);
	document.open();
	document.add(new Paragraph("Hello " + i));
	document.close();
	zip.closeEntry();
}
zip.close();



18、分割PDF
FileOutputStream out = new FileOutputStream(FILE_DIR + "splitPDF.pdf");

Document document = new Document();

PdfWriter.getInstance(document, out);

document.open();
document.add(new Paragraph("1 page"));

document.newPage();
document.add(new Paragraph("2 page"));

document.newPage();
document.add(new Paragraph("3 page"));

document.newPage();
document.add(new Paragraph("4 page"));

document.close();

PdfReader reader = new PdfReader(FILE_DIR + "splitPDF.pdf");

Document dd = new Document();
PdfWriter writer = PdfWriter.getInstance(dd, new FileOutputStream(FILE_DIR + "splitPDF1.pdf"));
dd.open();
PdfContentByte cb = writer.getDirectContent();
dd.newPage();
cb.addTemplate(writer.getImportedPage(reader, 1), 0, 0);
dd.newPage();
cb.addTemplate(writer.getImportedPage(reader, 2), 0, 0);
dd.close();
writer.close();

Document dd2 = new Document();
PdfWriter writer2 = PdfWriter.getInstance(dd2, new FileOutputStream(FILE_DIR + "splitPDF2.pdf"));
dd2.open();
PdfContentByte cb2 = writer2.getDirectContent();
dd2.newPage();
cb2.addTemplate(writer2.getImportedPage(reader, 3), 0, 0);
dd2.newPage();
cb2.addTemplate(writer2.getImportedPage(reader, 4), 0, 0);
dd2.close();
writer2.close();


19、合并PDF
PdfReader reader1 = new PdfReader(FILE_DIR + "splitPDF1.pdf");
PdfReader reader2 = new PdfReader(FILE_DIR + "splitPDF2.pdf");

FileOutputStream out = new FileOutputStream(FILE_DIR + "mergePDF.pdf");

Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, out);

document.open();
PdfContentByte cb = writer.getDirectContent();

int totalPages = 0;
totalPages += reader1.getNumberOfPages();
totalPages += reader2.getNumberOfPages();

java.util.List<PdfReader> readers = new ArrayList<PdfReader>();
readers.add(reader1);
readers.add(reader2);

int pageOfCurrentReaderPDF = 0;
Iterator<PdfReader> iteratorPDFReader = readers.iterator();

// Loop through the PDF files and add to the output.
while (iteratorPDFReader.hasNext()) {
	PdfReader pdfReader = iteratorPDFReader.next();

	// Create a new page in the target for each source page.
	while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
		document.newPage();
		pageOfCurrentReaderPDF++;
		PdfImportedPage page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
		cb.addTemplate(page, 0, 0);
	}
	pageOfCurrentReaderPDF = 0;
}
out.flush();
document.close();
out.close();


20、Annotation
PdfWriter writer = PdfWriter.getInstance(doc, out);
writer.setLinearPageMode();

doc.open();
doc.add(new Paragraph("1 page"));
doc.add(new Annotation("Title", "This is a annotation!"));

doc.newPage();
doc.add(new Paragraph("2 page"));
Chunk chunk = new Chunk("\u00a0");
chunk.setAnnotation(PdfAnnotation.createText(writer, null, "Title", "This is a another annotation!", false, "Comment"));
doc.add(chunk);

//添加附件
doc.newPage();
doc.add(new Paragraph("3 page"));
Chunk chunk2 = new Chunk("\u00a0\u00a0");
PdfAnnotation annotation = PdfAnnotation.createFileAttachment(
		writer, null, "Title", null,
		"resource/test2.jpg",
		"img.jpg");
annotation.put(PdfName.NAME,
		new PdfString("Paperclip"));
chunk2.setAnnotation(annotation);
doc.add(chunk2);



21、插入一个Table
PdfPTable table = new PdfPTable(3);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");

document.add(table);


22、表格嵌套
PdfPTable table = new PdfPTable(4);

//1行2列
PdfPTable nested1 = new PdfPTable(2);
nested1.addCell("1.1");
nested1.addCell("1.2");

//2行1列
PdfPTable nested2 = new PdfPTable(1);
nested2.addCell("2.1");
nested2.addCell("2.2");

//将表格插入到指定位置
for (int k = 0; k < 24; ++k) {
    if (k == 1) {
        table.addCell(nested1);
    } else if (k == 20) {
        table.addCell(nested2);
    } else {
        table.addCell("cell " + k);
    }
}

document.add(table);


23、设置表格宽度
PdfPTable table = new PdfPTable(3);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setRowspan(2);
table.addCell(cell);
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");

//100%
table.setWidthPercentage(100);
document.add(table);        
document.add(new Paragraph("\n\n"));

//宽度50% 居左
table.setHorizontalAlignment(Element.ALIGN_LEFT);
document.add(table);
document.add(new Paragraph("\n\n"));

//宽度50% 居中
table.setHorizontalAlignment(Element.ALIGN_CENTER);
document.add(table);
document.add(new Paragraph("\n\n"));

//宽度50% 居右
table.setWidthPercentage(50);
table.setHorizontalAlignment(Element.ALIGN_RIGHT);
document.add(table);
document.add(new Paragraph("\n\n"));

//固定宽度
table.setTotalWidth(300);
table.setLockedWidth(true);
document.add(table);


24、设置表格前后间隔
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("合并3个单元格",fontZH));
cell.setColspan(3);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");

cell = new PdfPCell(new Paragraph("红色边框",fontZH));
cell.setBorderColor(new BaseColor(255, 0, 0));
table.addCell(cell);

cell = new PdfPCell(new Paragraph("合并单2个元格",fontZH));
cell.setColspan(2);
cell.setBackgroundColor(new BaseColor(0xC0, 0xC0, 0xC0));
table.addCell(cell);

table.setWidthPercentage(50);

document.add(new Paragraph("追加2个表格",fontZH));
document.add(table);
document.add(table);

document.newPage();
document.add(new Paragraph("使用'SpacingBefore'和'setSpacingAfter'",fontZH));
table.setSpacingBefore(15f);
document.add(table);
document.add(table);
document.add(new Paragraph("这里没有间隔",fontZH));
table.setSpacingAfter(15f);


25、设置单元格宽度
//按比例设置单元格宽度
float[] widths = {0.1f, 0.1f, 0.05f, 0.75f};
PdfPTable table = new PdfPTable(widths);
table.addCell("10%");
table.addCell("10%");
table.addCell("5%");
table.addCell("75%");
table.addCell("aa");
table.addCell("aa");
table.addCell("a");
table.addCell("aaaaaaaaaaaaaaa");
table.addCell("bb");
table.addCell("bb");
table.addCell("b");
table.addCell("bbbbbbbbbbbbbbb");
table.addCell("cc");
table.addCell("cc");
table.addCell("c");
table.addCell("ccccccccccccccc");
document.add(table);
document.add(new Paragraph("\n\n"));

//调整比例
widths[0] = 20f;
widths[1] = 20f;
widths[2] = 10f;
widths[3] = 50f;
table.setWidths(widths);
document.add(table);

//按绝对值设置单元格宽度
widths[0] = 40f;
widths[1] = 40f;
widths[2] = 20f;
widths[3] = 300f;
Rectangle r = new Rectangle(PageSize.A4.getRight(72), PageSize.A4.getTop(72));
table.setWidthPercentage(widths, r);
document.add(new Paragraph("\n\n"));
document.add(table);


26、设置单元格高度
PdfPTable table = new PdfPTable(2);

PdfPCell cell;

//折行
table.addCell(new PdfPCell(new Paragraph("折行", fontZH)));
cell = new PdfPCell(new Paragraph("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah"));
cell.setNoWrap(false);
table.addCell(cell);

//不折行
table.addCell(new PdfPCell(new Paragraph("不折行", fontZH)));
cell.setNoWrap(true);
table.addCell(cell);

//设置高度
table.addCell(new PdfPCell(new Paragraph("任意高度",fontZH)));
cell = new PdfPCell(new Paragraph("1. blah blah\n2. blah blah blah\n3. blah blah\n4. blah blah blah\n5. blah blah\n6. blah blah blah\n7. blah blah\n8. blah blah blah"));
table.addCell(cell);

//固定高度
table.addCell(new PdfPCell(new Paragraph("固定高度",fontZH)));
cell.setFixedHeight(50f);
table.addCell(cell);

//最小高度
table.addCell(new PdfPCell(new Paragraph("最小高度",fontZH)));
cell = new PdfPCell(new Paragraph("最小高度:50",fontZH));
cell.setMinimumHeight(50f);
table.addCell(cell);

//最后一行拉长到page底部
table.setExtendLastRow(true);
table.addCell(new PdfPCell(new Paragraph("拉长最后一行",fontZH)));
cell = new PdfPCell(new Paragraph("最后一行拉长到page底部",fontZH));
table.addCell(cell);

document.add(table);


27、设置单元格颜色
PdfPTable table = new PdfPTable(4);
PdfPCell cell;
cell = new PdfPCell(new Paragraph("颜色测试",fontZH));
table.addCell(cell);

//红色背景,无边框
cell = new PdfPCell(new Paragraph("红色背景,无边框",fontZH));
cell.setBorder(Rectangle.NO_BORDER);
cell.setBackgroundColor(BaseColor.RED);
table.addCell(cell);

//绿色背景,下边框
cell = new PdfPCell(new Paragraph("绿色背景,下边框",fontZH));
cell.setBorder(Rectangle.BOTTOM);
cell.setBorderColorBottom(BaseColor.MAGENTA);
cell.setBorderWidthBottom(5f);
cell.setBackgroundColor(BaseColor.GREEN);
table.addCell(cell);

//蓝色背景,上边框
cell = new PdfPCell(new Paragraph("蓝色背景,上边框",fontZH));
cell.setBorder(Rectangle.TOP);
cell.setUseBorderPadding(true);
cell.setBorderWidthTop(5f);
cell.setBorderColorTop(BaseColor.CYAN);
cell.setBackgroundColor(BaseColor.BLUE);
table.addCell(cell);

cell = new PdfPCell(new Paragraph("背景灰色度",fontZH));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("0.25"));
cell.setBorder(Rectangle.NO_BORDER);
cell.setGrayFill(0.25f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("0.5"));
cell.setBorder(Rectangle.NO_BORDER);
cell.setGrayFill(0.5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("0.75"));
cell.setBorder(Rectangle.NO_BORDER);
cell.setGrayFill(0.75f);
table.addCell(cell);

document.add(table);


28、插入图像
Image image = Image.getInstance("resource/test2.jpg");
float[] widths = { 1f, 4f };

PdfPTable table = new PdfPTable(widths);

//插入图片
table.addCell(new PdfPCell(new Paragraph("图片测试", fontZH)));
table.addCell(image);

//调整图片大小
table.addCell("This two");
table.addCell(new PdfPCell(image, true));

//不调整
table.addCell("This three");
table.addCell(new PdfPCell(image, false));
document.add(table);


29、设置表头
String[] bogusData = { "M0065920", "SL", "FR86000P", "PCGOLD",
        "119000", "96 06", "2001-08-13", "4350", "6011648299",
        "FLFLMTGP", "153", "119000.00" };
int NumColumns = 12;
// 12
PdfPTable datatable = new PdfPTable(NumColumns);
int headerwidths[] = { 9, 4, 8, 10, 8, 11, 9, 7, 9, 10, 4, 10 }; // percentage
datatable.setWidths(headerwidths);
datatable.setWidthPercentage(100);
datatable.getDefaultCell().setPadding(3);
datatable.getDefaultCell().setBorderWidth(2);
datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

datatable.addCell("Clock #");
datatable.addCell("Trans Type");
datatable.addCell("Cusip");
datatable.addCell("Long Name");
datatable.addCell("Quantity");
datatable.addCell("Fraction Price");
datatable.addCell("Settle Date");
datatable.addCell("Portfolio");
datatable.addCell("ADP Number");
datatable.addCell("Account ID");
datatable.addCell("Reg Rep ID");
datatable.addCell("Amt To Go ");

datatable.setHeaderRows(1);

//边框
datatable.getDefaultCell().setBorderWidth(1);

//背景色
for (int i = 1; i < 1000; i++) {
    for (int x = 0; x < NumColumns; x++) {
        datatable.addCell(bogusData[x]);
    }
}

document.add(datatable);


30、分割表格
//横向分割
PdfContentByte cb = writer.getDirectContent();
PdfPTable table = new PdfPTable(10);
for (int k = 1; k <= 100; ++k) {
    table.addCell("The number " + k);
}
table.setTotalWidth(400);

table.writeSelectedRows(0, 5, 0, -1, 5, 700, cb);
table.writeSelectedRows(5, -1, 0, -1, 210, 700, cb);


31、设置单元格留白
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
Paragraph p = new Paragraph("Quick brown fox jumps over the lazy dog. Quick brown fox jumps over the lazy dog.");
table.addCell(new PdfPCell(new Paragraph("默认",fontZH)));
table.addCell(p);
table.addCell(new PdfPCell(new Paragraph("Padding:10",fontZH)));
cell = new PdfPCell(p);
cell.setPadding(10f);
table.addCell(cell);
table.addCell(new PdfPCell(new Paragraph("Padding:0",fontZH)));
cell = new PdfPCell(p);
cell.setPadding(0f);
table.addCell(cell);
table.addCell(new PdfPCell(new Paragraph("上Padding:0 左Padding:20",fontZH)));
cell = new PdfPCell(p);
cell.setPaddingTop(0f);
cell.setPaddingLeft(20f);
table.addCell(cell);
document.add(table);

document.newPage();
table = new PdfPTable(2);
table.addCell(new PdfPCell(new Paragraph("没有Leading",fontZH)));
table.getDefaultCell().setLeading(0f, 0f);
table.addCell("blah blah\nblah blah blah\nblah blah\nblah blah blah\nblah blah\nblah blah blah\nblah blah\nblah blah blah\n");
table.getDefaultCell().setLeading(14f, 0f);
table.addCell(new PdfPCell(new Paragraph("固定Leading:14pt",fontZH)));
table.addCell("blah blah\nblah blah blah\nblah blah\nblah blah blah\nblah blah\nblah blah blah\nblah blah\nblah blah blah\n");
table.addCell(new PdfPCell(new Paragraph("相对于字体",fontZH)));
table.getDefaultCell().setLeading(0f, 1.0f);
table.addCell("blah blah\nblah blah blah\nblah blah\nblah blah blah\nblah blah\nblah blah blah\nblah blah\nblah blah blah\n");
document.add(table);


32、设置单元格边框
//没有边框
PdfPTable table1 = new PdfPTable(3);  
table1.getDefaultCell().setBorder(PdfPCell.NO_BORDER);  
table1.addCell(new Paragraph("Cell 1")); 
table1.addCell(new Paragraph("Cell 2")); 
table1.addCell(new Paragraph("Cell 3")); 
document.add(table1);

//边框粗细颜色
document.newPage();
Rectangle b1 = new Rectangle(0f, 0f);
b1.setBorderWidthLeft(6f);
b1.setBorderWidthBottom(5f);
b1.setBorderWidthRight(4f);
b1.setBorderWidthTop(2f);
b1.setBorderColorLeft(BaseColor.RED);
b1.setBorderColorBottom(BaseColor.ORANGE);
b1.setBorderColorRight(BaseColor.YELLOW);
b1.setBorderColorTop(BaseColor.GREEN);
PdfPTable table2 = new PdfPTable(1);
PdfPCell cell =  new PdfPCell(new Paragraph("Cell 1"));
cell.cloneNonPositionParameters(b1);
table2.addCell(cell);
document.add(table2);


33、PdfPTableEvent

34、PdfPCellEvent

35、PdfPageEventHelper

36、生成Barcode QRCode
String myString = "http://www.google.com";

Barcode128 code128 = new Barcode128();
code128.setCode(myString.trim());
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
code128Image.setAbsolutePosition(10,700);
code128Image.scalePercent(125);
doc.add(code128Image);

BarcodeQRCode qrcode = new BarcodeQRCode(myString.trim(), 1, 1, null);
Image qrcodeImage = qrcode.getImage();
qrcodeImage.setAbsolutePosition(10,600);
qrcodeImage.scalePercent(200);
doc.add(qrcodeImage);



37、HTML to PDF
Document document = new Document(PageSize.LETTER);
PdfWriter.getInstance(document, new FileOutputStream("c://testpdf1.pdf"));
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader("<h1>This is a test!</h1>"));
document.close();


  • 大小: 4.5 KB
  • 大小: 6 KB
  • 大小: 3.7 KB
  • 大小: 6.1 KB
  • 大小: 14.1 KB
  • 大小: 2.4 KB
  • 大小: 5.4 KB
  • 大小: 10.1 KB
分享到:
评论
23 楼 hehantest 2016-08-09  
请问,如何缩小字体之间的间距?还有如何在PDF中画横线?求解,谢谢
22 楼 medlying 2016-05-19  
张希明 写道
想问下,我的HTML页面如何使用JavaScript,貌似用到JavaScript就不能生成PDF

大兄弟解决了吗,我现在遇到这个问题,html里面有jquery就会出问题
21 楼 hepingzhige13 2016-04-05  
20 楼 张希明 2015-10-09  
想问下,我的HTML页面如何使用JavaScript,貌似用到JavaScript就不能生成PDF
19 楼 魏英杰 2015-06-02  
[color=red][/color]sdf
[size=x-small][/size]
18 楼 maodun1978 2015-05-20  
超强超棒
17 楼 rensanning 2015-04-22  
@haiyangyiba

setEncryption(userPassword, ownerPassword, permissions, encryptionType)

userPassword 用户口令:控制是否能打开PDF
ownerPassword 许可口令:打开后控制操作PDF的权限(拷贝、打印、编辑等)

如果两个都设置了,两个口令都能打卡PDF只是打开后的权限不一样
只设置userPassword,打开是时需要口令
只设置ownerPassword,打开时不需要口令

看看PDF规范的加密部分吧。
16 楼 haiyangyiba 2015-04-21  
感谢楼主分享。
我试了下第3个例子,设置密码,结果设置的Hello和World都可以打开文档,代码如下:
private static void writeFile2Password(File file) throws Exception {
		Document doc = new Document();
		PdfWriter pdf = PdfWriter.getInstance(doc, new FileOutputStream(file));
		pdf.setEncryption("aaa".getBytes(), "bbb".getBytes(), PdfWriter.ALLOW_SCREENREADERS, PdfWriter.STANDARD_ENCRYPTION_128);
		doc.open();
		doc.add(new Paragraph("Hello World!"));
		doc.close();
		System.out.println("文档创建成功");
	}
15 楼 s626551973 2014-12-10  
牛叉
14 楼 WandyQian 2014-11-25  
厉害啊
13 楼 duanjudy 2014-08-28  
厉害,但是我想问个问题;就是我怎么设置下划线的长度??
12 楼 rensanning 2013-11-18  
@forchase
实现FontProvider接口比如叫MyFontProvider,在getFont()方法里设置你的字体库。然后调用HTMLWorker.parseToList()。
HashMap providers = new HashMap();
        providers.put(HTMLWorker.FONT_PROVIDER, new MyFontProvider());
        List<Element> list = HTMLWorker.parseToList(
                new StringReader(html),
                new StyleSheet(),
                providers);
        for (Element e : list) {
            document.add(e);
        }
11 楼 forchase 2013-11-18  
LZ有没有碰到过乱码问题,html一些特殊字符,无法在pdf中正常显示啊
10 楼 hanfengqing 2013-10-28  
楼主,看了你的文章我看到了希望 。
现在我有急事相求。
公司分配一个任务。
AFP文件转换为PDF文件 ,请问使用itextpdf-5.2.1.jar 能实现吗?里面有支持AFP文件格式的实现方式吗??
如果没有,请问有类似的API没有?
非常感谢!
9 楼 Emilio88 2013-10-21  
楼主真牛!!!
请问如何读取PDF的内容呢?我看IText里面有一个PdfReader的类,但是不知道怎么用?我也知道用其他的jar包可以读取PDF,但是我就说想用iText提供的PdfReader请问该如何做?
8 楼 SwordShadow 2013-08-07  
Thank you
7 楼 rensanning 2013-06-17  
yudongdong520 写道
请问有没有可以旋转方向的代码我急切需求

你想 rotate 什么?

整个页面:
document.setPageSize(PageSize.A4.rotate());


图像:
Image image = Image.getInstance(url);
image.setRotationDegrees(90);
document.add(image);


单元格内容:
PdfPTable table = new PdfPTable(3);
PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1 - 90"));
cell1.setRotation(90);
table.addCell(cell1);

6 楼 yudongdong520 2013-06-16  
请问有没有可以旋转方向的代码我急切需求
5 楼 bqw_5189 2013-03-20  
http://www.micmiu.com/opensource/expdoc/itext-html-pdf/
4 楼 bjyuling 2013-01-24  
mark。。。

相关推荐

    iText操作Pdf简单整理

    iText是一款强大的Java类库,专门用于生成和编辑PDF文档。这个开源项目源自sourceforge.net,为开发者提供了灵活且高效的方式来处理PDF文档的创建、更新和格式转换。iText不仅支持生成PDF和rtf格式的文档,还具备将...

    iText入门基础教程

    iText 是一个强大的 Java 类库,专门用于生成和操作 PDF 文档。它允许开发者通过编程方式创建 PDF 文件,包括添加文本、表格、图像,并且可以与 Servlet 容器集成,如在 Spring 框架中使用。这个库因其高效和灵活性...

    IText入门开发文档

    这个入门开发文档将引导你了解如何在Java项目中使用iText来创建PDF文件。首先,我们需要在项目中添加iText的相关依赖,以便使用其提供的各种功能。以下是iText 7.0.2版本的Maven依赖: ```xml &lt;groupId&gt;...

    java使用itext生成pdf的代码示例

    在Java开发中,生成PDF文档是一项常见的需求,特别是在报表、合同和电子书籍等场景下。iText是一个强大的库,专门...这个简单的代码示例可以帮助开发者快速入门iText库,并了解如何在实际项目中生成包含中文的PDF文档。

    iText2.1.7资料大全(制作PDF的java开源框)

    相关教程 iText 简介 iText创建文档简介 iText入门实例 IText使用PDF模板输出报表的实践 我和iText第一次亲密接触 相关JAR iText-2.1.7.jar(核心) iTextAsian.jar(中文支持) iText-rups-2.1.7.jar iText-rtf-...

    itext-in-action java开发pdf

    - **文档生成**:iText 最主要的功能之一就是能够在 Java 应用程序中生成 PDF 文件。这通常用于需要自动化生成报告、发票或其他文档的场景。 - **文档编辑**:除了生成 PDF 外,iText 还可以用来编辑现有的 PDF ...

    IText经典入门教程

    IText 是一个强大的开源库,主要用于创建和处理 PDF 文档。本教程主要针对初学者,旨在引导你逐步了解如何使用 IText 进行 PDF 文件的生成。以下是对标题和描述中涉及的知识点的详细说明: 1. **创建一个 Document ...

    JavaPDF入门教程(附带习题和答案)

    JavaPDF入门教程是针对想要学习如何在Java应用程序中处理PDF文档的初学者设计的资源。PDF(Portable Document Format)是一种广泛使用的文件格式,用于呈现文档,包括文本格式和图像,保持与操作系统、硬件或软件...

    iText.in .action.pdf

    这本书深入浅出地介绍了如何利用iText库进行PDF文档的创建、编辑和操作,是Java开发者掌握PDF技术的重要参考资料。 iText是一个开源的Java库,主要用于生成和修改PDF文档。在Java应用程序中,我们可以借助iText来...

    java生成pdf

    通过这个入门程序,你可以学习到如何在Java环境中使用iText库生成包含中文的PDF文档,并了解如何在Eclipse中进行项目配置。随着深入学习,还可以探索更多高级特性,如模板引擎、表单填写、数字签名等。

    java导出PDF文件实例.rar

    1. **iText-5.0.6.jar**:这是一个强大的开源Java库,专门用于生成和修改PDF文档。iText库提供了丰富的API,可以方便地创建PDF页面,添加文本、图像、表格等元素,还能进行复杂的布局和样式设置。在这个实例中,...

    iText-In-Action

    2. iText入门:讲解如何安装和配置iText库,以及如何在Java项目中引入这个库,让你快速上手PDF编程。 3. 文本与字体:学习如何在PDF文档中添加文本,包括设置字体、字号、颜色,以及处理特殊字符和多语言支持。 4....

    itext-hello word 已实现

    iText是一款强大的Java库,专用于创建和操作PDF文档。本篇文章将详细介绍如何使用iText库来创建一个简单的"Hello World" PDF文档。 首先,我们需要理解iText的基本工作原理。iText提供了一系列API,允许开发者在PDF...

    java:图片列表合并转pdf完整示例

    在Java编程环境中,将图片列表合并转换为PDF文档是一项常见的任务,特别是在报告生成、文档整理或数据可视化等场景。...这个“Images2Pdf”示例只是一个基础起点,帮助你快速入门将图片合并为PDF的Java编程。

Global site tag (gtag.js) - Google Analytics