Java操作PDF之iText详细入门
Posted 零度anngle
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java操作PDF之iText详细入门相关的知识,希望对你有一定的参考价值。
iText是著名的开放项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、html文件转化为PDF文件。http://itextpdf.com/
版本:itextpdf-5.2.1.jar
1、生成一个PDF
Java代码
- //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
Java代码
- //页面大小
- 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、设置密码
Java代码
- 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
Java代码
- 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、添加水印(背景图)
Java代码
- //图片水印
- 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
Java代码
- //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
Java代码
- //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, 以上是关于Java操作PDF之iText详细入门的主要内容,如果未能解决你的问题,请参考以下文章