ITEXT是一个比较强大的PDF文档生成工具,同时还可以生成RTF及HTML文档,官网帮助文档地址:
[url]http://itextdocs.lowagie.com/tutorial/general/index.php[/url]
/**
* 中英文的简单输出
*
* @throws IOException
*/
private void test1() throws IOException {
Document document = new Document();
try {
FileOutputStream fos = new FileOutputStream( "hello.pdf");
PdfWriter.getInstance(document, fos);
document.open();
document.add( new Paragraph( "Hello World"));
// 此处需要引用iTextAsian.jar
BaseFont bfChinese = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
document.add( new Paragraph( "中引文egnli大家好啊", FontChinese));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
* 中英文的简单输出
*
* @throws IOException
*/
private void test1() throws IOException {
Document document = new Document();
try {
FileOutputStream fos = new FileOutputStream( "hello.pdf");
PdfWriter.getInstance(document, fos);
document.open();
document.add( new Paragraph( "Hello World"));
// 此处需要引用iTextAsian.jar
BaseFont bfChinese = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
document.add( new Paragraph( "中引文egnli大家好啊", FontChinese));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
/**
* 多种格式的输出(PDF,RTF,HTML)
*/
private void test6() {
System.out.println( "Hello World in PDF, RTF and HTML");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create 3 different writers that listen to the document
PdfWriter pdf = PdfWriter.getInstance(document, new FileOutputStream( "HelloWorldPdf.pdf"));
RtfWriter2 rtf = RtfWriter2.getInstance(document, new FileOutputStream( "HelloWorldRtf.rtf"));
HtmlWriter.getInstance(document, new FileOutputStream( "HelloWorldHtml.html"));
// step 3: we open the document
document.open();
// step 4: we add a paragraph to the document
document.add( new Paragraph( "Hello World"));
// we make references
Anchor pdfRef = new Anchor( "see Hello World in PDF.");
pdfRef.setReference( "./HelloWorldPdf.pdf");
Anchor rtfRef = new Anchor( "see Hello World in RTF.");
rtfRef.setReference( "./HelloWorldRtf.rtf");
// we add the references, but only to the HTML page:
pdf.pause();
rtf.pause();
document.add(pdfRef);
document.add(Chunk.NEWLINE);
document.add(rtfRef);
pdf.resume();
rtf.resume();
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
* 多种格式的输出(PDF,RTF,HTML)
*/
private void test6() {
System.out.println( "Hello World in PDF, RTF and HTML");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create 3 different writers that listen to the document
PdfWriter pdf = PdfWriter.getInstance(document, new FileOutputStream( "HelloWorldPdf.pdf"));
RtfWriter2 rtf = RtfWriter2.getInstance(document, new FileOutputStream( "HelloWorldRtf.rtf"));
HtmlWriter.getInstance(document, new FileOutputStream( "HelloWorldHtml.html"));
// step 3: we open the document
document.open();
// step 4: we add a paragraph to the document
document.add( new Paragraph( "Hello World"));
// we make references
Anchor pdfRef = new Anchor( "see Hello World in PDF.");
pdfRef.setReference( "./HelloWorldPdf.pdf");
Anchor rtfRef = new Anchor( "see Hello World in RTF.");
rtfRef.setReference( "./HelloWorldRtf.rtf");
// we add the references, but only to the HTML page:
pdf.pause();
rtf.pause();
document.add(pdfRef);
document.add(Chunk.NEWLINE);
document.add(rtfRef);
pdf.resume();
rtf.resume();
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
/**
* 对PDF文件进行加密
*/
private void test7() {
System.out.println( "Hello World Encrypted");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream( "HelloEncrypted.pdf"));
// 这里需要引用到第三包JAR包:bcprov-jdk14-138.jar
// 设置了两个密码:第一个为用户密码pwu,第二个为所有者密码pwo
writer.setEncryption( "pwu".getBytes(), "pwo".getBytes(), PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING,
PdfWriter.STANDARD_ENCRYPTION_128);
// step 3: we open the document
document.open();
// step 4: we add a paragraph to the document
document.add( new Paragraph( "Hello World"));
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
* 对PDF文件进行加密
*/
private void test7() {
System.out.println( "Hello World Encrypted");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream( "HelloEncrypted.pdf"));
// 这里需要引用到第三包JAR包:bcprov-jdk14-138.jar
// 设置了两个密码:第一个为用户密码pwu,第二个为所有者密码pwo
writer.setEncryption( "pwu".getBytes(), "pwo".getBytes(), PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING,
PdfWriter.STANDARD_ENCRYPTION_128);
// step 3: we open the document
document.open();
// step 4: we add a paragraph to the document
document.add( new Paragraph( "Hello World"));
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
/**
* 设置文档的元数据(Metadata)
*/
private void test8() {
System.out.println( "Metadata");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileOutputStream( "HelloWorldMeta.pdf"));
// step 3: we add some metadata open the document
document.addTitle( "Hello World example");
document.addSubject( "This example explains how to add metadata.");
document.addKeywords( "iText, Hello World, step 3, metadata");
document.addCreator( "My program using iText");
document.addAuthor( "Bruno Lowagie");
document.open();
// step 4: we add a paragraph to the document
document.add( new Paragraph( "Hello World"));
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
* 设置文档的元数据(Metadata)
*/
private void test8() {
System.out.println( "Metadata");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileOutputStream( "HelloWorldMeta.pdf"));
// step 3: we add some metadata open the document
document.addTitle( "Hello World example");
document.addSubject( "This example explains how to add metadata.");
document.addKeywords( "iText, Hello World, step 3, metadata");
document.addCreator( "My program using iText");
document.addAuthor( "Bruno Lowagie");
document.open();
// step 4: we add a paragraph to the document
document.add( new Paragraph( "Hello World"));
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
转载于:https://blog.51cto.com/kinkding/146041