国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java開源工具iText生成PDF簡單實例

Java開源工具iText生成PDF簡單實例

2019-12-25 13:34junjie JAVA教程

這篇文章主要介紹了Java開源工具iText生成PDF簡單實例,本文給出了3段代碼實例,講解創建一個簡單PDF文件,在PDF中添加表格以及在PDF中添加圖片,需要的朋友可以參考下

iText下載頁面: http://sourceforge.net/projects/itext/files/
1.創建簡單的PDF文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package console.pdf;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
 
/**
 * 使用iText生成PDF文件
 */
public class CreatePDF {
 
  public static void main(String[] args) {
    CreatePDF p001 = new CreatePDF();
 
    String filename = "P001.pdf";
    p001.createPDF(filename);
  }
 
  public void createPDF(String filename) {
    // step 1
    Document document = new Document(PageSize.A4);
    // step 2
    try {
      PdfWriter.getInstance(document, new FileOutputStream(filename));
      
      document.addTitle("ID.NET");
      document.addAuthor("dotuian");
      document.addSubject("This is the subject of the PDF file.");
      document.addKeywords("This is the keyword of the PDF file.");
      
      // step 3
      document.open();
      // step 4
      document.add(new Paragraph("Hello World!"));
 
      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    } finally {
      // step 5
      document.close();
    }
  }
 
  
  
  
}


2.在PDF文件中添加Table

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package console.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.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
/**
 * 使用iText生成PDF文件
 * 在PDF文件中創建表格
 */
public class TableOfPDF {
 
  public static void main(String[] args) {
    TableOfPDF p001 = new TableOfPDF();
 
    String filename = "P002.pdf";
    p001.createPDF(filename);
  }
 
  public void createPDF(String filename) {
    // step 1
    Document document = new Document(PageSize.A4);
    // step 2
    try {
      PdfWriter.getInstance(document, new FileOutputStream(filename));
 
      document.addTitle("ID.NET");
      document.addAuthor("dotuian");
      document.addSubject("This is the subject of the PDF file.");
      document.addKeywords("This is the keyword of the PDF file.");
 
      // step 3
      document.open();
      // step 4
      PdfPTable table = createTable1();
      document.add(table);
      
      table = createTable2();
      table.setSpacingBefore(5);
      table.setSpacingAfter(5);
      document.add(table);
      
      table = createTable3();
      document.add(table);
      
      table = createTable4();
      table.setSpacingBefore(5);
      table.setSpacingAfter(5);
      document.add(table);
      
      table = createTable5();
      document.add(table);
      
      table = createTable6();
      document.add(table);
 
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    } finally {
      // step 5
      document.close();
    }
  }
 
  /**
   * Creates a table; widths are set with setWidths().
   *
   * @return a PdfPTable
   * @throws DocumentException
   */
  public static PdfPTable createTable1() throws DocumentException {
    PdfPTable table = new PdfPTable(3);
    table.setWidthPercentage(288 / 5.23f);
    table.setWidths(new int[] { 2, 1, 1 });
    
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 1"));
    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");
    return table;
  }
 
  /**
   * Creates a table; widths are set with setWidths().
   *
   * @return a PdfPTable
   * @throws DocumentException
   */
  public static PdfPTable createTable2() throws DocumentException {
    PdfPTable table = new PdfPTable(3);
    table.setTotalWidth(288);
    table.setLockedWidth(true);
    table.setWidths(new float[] { 2, 1, 1 });
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 2"));
    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");
    return table;
  }
 
  /**
   * Creates a table; widths are set in the constructor.
   *
   * @return a PdfPTable
   * @throws DocumentException
   */
  public static PdfPTable createTable3() throws DocumentException {
    PdfPTable table = new PdfPTable(new float[] { 2, 1, 1 });
    table.setWidthPercentage(55.067f);
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 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");
    return table;
  }
 
  /**
   * Creates a table; widths are set with special setWidthPercentage() method.
   *
   * @return a PdfPTable
   * @throws DocumentException
   */
  public static PdfPTable createTable4() throws DocumentException {
    PdfPTable table = new PdfPTable(3);
    Rectangle rect = new Rectangle(523, 770);
    table.setWidthPercentage(new float[] { 144, 72, 72 }, rect);
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 4"));
    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");
    return table;
  }
 
  /**
   * Creates a table; widths are set with setTotalWidth().
   *
   * @return a PdfPTable
   * @throws DocumentException
   */
  public static PdfPTable createTable5() throws DocumentException {
    PdfPTable table = new PdfPTable(3);
    table.setTotalWidth(new float[] { 144, 72, 72 });
    table.setLockedWidth(true);
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 5"));
    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");
    return table;
  }
  
  public static PdfPTable createTable6() throws DocumentException{
    PdfPTable table = new PdfPTable(10);
    table.setTotalWidth(595);
    //table.setLockedWidth(true);
    
    
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 6"));
    cell.setColspan(10);
    table.addCell(cell);
    
    for (int i = 1; i < 100; i++) {
      cell = new PdfPCell(new Phrase(String.valueOf(i)));
      cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
      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");
    return table;
  }
  
  
 
}

 


3.在PDF文件中添加圖片,并且指定文本位置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package console.pdf;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
 
/**
 * 使用iText生成PDF文件
 * 在PDF文件中添加背景圖片,并指定文本在PDF文件中的位置
 */
public class BackgroundImageOfPDF {
 
  public static void main(String[] args) {
    BackgroundImageOfPDF p001 = new BackgroundImageOfPDF();
 
    String filename = "P003.pdf";
    p001.createPDF(filename);
  }
 
  public void createPDF(String filename) {
    // step 1
    Document document = new Document(PageSize.A4.rotate(),0,0,0,0);
    // step 2
    try {
      PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(filename));
      
      document.addTitle("ID.NET");
      document.addAuthor("dotuian");
      document.addSubject("This is the subject of the PDF file.");
      document.addKeywords("This is the keyword of the PDF file.");
      
      // step 3
      document.open();
      // step 4
      Image image = Image.getInstance("bg.jpg");
      document.add(image);
 
      PdfContentByte pdfContentByte = pdfwriter.getDirectContent();
      pdfContentByte.beginText();
      BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,BaseFont.WINANSI,BaseFont.EMBEDDED);
      pdfContentByte.setFontAndSize(bf, 12);
 
      for (int i = 0; i <= 842; i = i + 50) {
        for (int j = 0; j <= 595; j = j + 20) {
          pdfContentByte.setTextMatrix(i, j);
          pdfContentByte.showText("(" + i + ":" + j + ")");
        }
      }
      
      pdfContentByte.endText();
      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // step 5
      document.close();
    }
  }
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 超碰最新网址 | 久久久免费电影 | h视频免费观看 | 国产精品午夜电影 | 高清一区二区三区 | 久久精品亚洲成在人线av网址 | 亚洲精品久久久久久久久久久 | 超碰伊人网 | 精品久久久久久久久久久久 | 黄色免费av | 欧美极品视频 | 成人性生交大片免费看网站 | 日本精品在线播放 | 欧美一区二区三区 | 欧美成人免费在线视频 | 亚洲精品在线免费看 | 亚洲人视频在线观看 | 喷水av | 国产成人精品一区二区 | 日韩成人在线视频 | 日韩在线一区二区三区免费视频 | 国产在线高清 | 久久高清精品 | 国产视频www | 中文字幕精品一区久久久久 | 日韩3级在线观看 | av天天干| 午夜精品久久久久久久久 | 欧美伦理电影一区二区 | 中文字幕一区二区三区四区五区 | 成人欧美一区二区三区在线观看 | 亚洲综合视频 | 国产在线一区二区三区 | 久久精品在线视频 | 午夜在线观看视频网站 | 久久久精品综合 | 在线视频国产一区 | 亚洲一区二区精品视频 | 天天爽天天操 | 欧美成视频 | 羞羞视频在线免费 |