我们来自五湖四海,不为别的,只因有共同的爱好,为中国互联网发展出一分力!

java生成pdf文件

2011年12月31日11:02 阅读: 22971 次

标签: PDF, Java

01 package com.hundsun.gildata.irp.core.action;
02 ?
03 import java.io.File;
04 import java.io.FileOutputStream;
05 import java.io.IOException;
06 import com.hundsun.gildata.irp.common.util.ItextUtils;
07 import com.itextpdf.text.Document;
08 import com.itextpdf.text.PageSize;
09 import com.itextpdf.text.pdf.PdfWriter;
10 ?
11 public class MakePdfTest {
12 ?????
13 ????private String att;
14 ?
15 ????public void makdPdf() throws IOException {
16 //????? final int headFontSize = 18;
17 //????? final int sectionFontSize = 12;
18 ????????final int margin = 10;
19 ????????String filePath = "d:/aaa/";
20 ????????String fileName = "aa.pdf";
21 ?????????
22 ????????att = filePath + fileName;
23 ?????????
24 ????????File file = new File(filePath);
25 ????????if (!file.exists()) {
26 ????????????file.mkdirs();
27 ????????}
28 ????????Document document = null;
29 ????????FileOutputStream fos = null;
30 ????????PdfWriter pdf = null;
31 ????????try {
32 ????????????document = new Document(PageSize.A4, margin, margin, margin, margin);
33 ????????????fos = new FileOutputStream(filePath + fileName);
34 ????????????pdf = PdfWriter.getInstance(document, fos);
35 ????????????document.open();
36 ?
37 ????????????String summary = "? this is a pdf made bycode\n? 另起一行".replaceAll("\n", "
"
).replaceAll(" ", "  ");
38 ????????????document.add(ItextUtils.processViewpoint(summary));
39 ?????????????
40 ????????????att = filePath + fileName;
41 ?????????????
42 ?
43 ????????} catch (Exception e) {
44 ????????????return;
45 ????????} finally {
46 ????????????if (document != null) {
47 ????????????????document.close();
48 ????????????}
49 ????????????if (pdf != null) {
50 ????????????????pdf.close();
51 ????????????}
52 ????????????if (fos != null) {
53 ????????????????fos.close();
54 ????????????}
55 ?????????????
56 ????????}
57 ????}
58 ?
59 ????public static void main(String[] args) {
60 ?????????
61 ????????MakePdfTest testMake = new MakePdfTest();
62 ????????try {
63 ????????????testMake.makdPdf();
64 ????????????System.out.println(testMake.att);
65 ????????} catch (IOException e) {
66 ????????????e.printStackTrace();
67 ????????}
68 ?
69 ????}
70 ?
71 }
分享到: 更多
蓝客门户