ITEXT : http://sourceForge.net/projects/itext/files/
1. 创建简单的 PDF 文件
Paket Console.pdf; import java.io.filenotfoundException; import java.io.fileoutputStream; import com.itextpdf.text.document; impor com.itextpdf.text.documentException; .text.paragraph; import com.itextpdf.text.pdf.pdfwriter;/** * 使用 itext 生成 pdf 文件 */kelas publik createPdf {public static Main (string [] args) {createPdf p001 = createPdf baru (); String filename = "p001.pdf"; p001.createPdf (nama file); } public void createPdf (string fileName) {// Langkah 1 dokumen = dokumen baru (pagesize.a4); // Langkah 2 coba {PDFWriter.GetInstance (dokumen, FileOutputStream baru (nama file)); document.addtitle ("id.net"); document.addauthor ("dotuian"); document.addsubject ("Ini adalah subjek file PDF."); document.addkeywords ("Ini adalah kata kunci dari file PDF."); // langkah 3 document.open (); // langkah 4 document.add (paragraf baru ("halo dunia!")); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (DocumentException e) {E.PrintStackTrace (); } akhirnya {// langkah 5 document.close (); }}}
2. 在 PDF 文件中添加 Tabel
Paket Console.pdf; import java.io.filenotfoundException; import java.io.fileoutputStream; import com.itextpdf.text.basecolor; impor com.itextpdf.text.document; impor com.itextpdf.text.documentception; .text.pagesize; import com.itextpdf.text.phrase; import com.itextpdf.text.rectangle; import com.itextpdf.text.pdf.pdfpcell; import com.itextpdf.text.pdf.pdfptable; impor com.itextpdf. text.pdf.pdfwriter;/** * 使用 itext 生成 pdf 文件 * 在 pdf 文件中创建表格 */kelas publik Tableofpdf {public static void main (string [] args) {tableofpdf p001 = new tabelofpdf (); String filename = "p002.pdf"; p001.createPdf (nama file); } public void createPdf (string fileName) {// Langkah 1 dokumen = dokumen baru (pagesize.a4); // Langkah 2 coba {PDFWriter.GetInstance (dokumen, FileOutputStream baru (nama file)); document.addtitle ("id.net"); document.addauthor ("dotuian"); document.addsubject ("Ini adalah subjek file PDF."); document.addkeywords ("Ini adalah kata kunci dari file PDF."); // langkah 3 document.open (); // langkah 4 tabel pdfptable = createTable1 (); document.add (tabel); tabel = createTable2 (); Table.SetspacingBefore (5); Table.SetspacingAfter (5); document.add (tabel); tabel = createTable3 (); document.add (tabel); tabel = createTable4 (); Table.SetspacingBefore (5); Table.SetspacingAfter (5); document.add (tabel); tabel = createTable5 (); document.add (tabel); tabel = createTable6 (); document.add (tabel); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (DocumentException e) {E.PrintStackTrace (); } akhirnya {// langkah 5 document.close (); }} /*** membuat tabel; Lebar diatur dengan setWidths (). * * @return a pdfptable * @throws documentexception */ public static pdfptable createTable1 () melempar DocumentException {pdfptable tabel = new Pdfptable (3); Table.SetWidthpercentage (288 / 5.23F); Table.setWidths (int baru [] {2, 1, 1}); Sel pdfpcell; sel = PDFPCell baru (frasa baru ("Tabel 1")); cell.setcolspan (3); Table.addcell (sel); sel = PDFPCell baru (frasa baru ("sel dengan rowspan 2")); cell.setrowspan (2); Table.addcell (sel); table.addcell ("Baris 1; sel 1"); Table.addcell ("Baris 1; sel 2"); Table.addcell ("Baris 2; sel 1"); Table.addcell ("Baris 2; sel 2"); meja kembali; } /*** membuat tabel; Lebar diatur dengan setWidths (). * * @Return a pdfptable * @throws DocumentException */ public static pdfptable createTable2 () melempar DocumentException {pdfptable tabel = new Pdfptable (3); Table.SetTotalWidth (288); Table.setlockedwidth (true); Table.setWidths (float baru [] {2, 1, 1}); Sel pdfpcell; sel = PDFPCell baru (frasa baru ("Tabel 2")); cell.setcolspan (3); Table.addcell (sel); sel = PDFPCell baru (frasa baru ("sel dengan rowspan 2")); cell.setrowspan (2); Table.addcell (sel); table.addcell ("Baris 1; sel 1"); Table.addcell ("Baris 1; sel 2"); Table.addcell ("Baris 2; sel 1"); Table.addcell ("Baris 2; sel 2"); meja kembali; } /*** membuat tabel; Lebar diatur dalam konstruktor. * * @return a pdfptable * @throws documentexception */ public static pdfptable createTable3 () melempar DocumentException {pdfptable tabel = new pdfptable (float baru [] {2, 1, 1}); Table.SetWidthpercentage (55.067F); Sel pdfpcell; sel = PDFPCell baru (frasa baru ("Tabel 3")); cell.setcolspan (3); Table.addcell (sel); sel = PDFPCell baru (frasa baru ("sel dengan rowspan 2")); cell.setrowspan (2); Table.addcell (sel); table.addcell ("Baris 1; sel 1"); Table.addcell ("Baris 1; sel 2"); Table.addcell ("Baris 2; sel 1"); Table.addcell ("Baris 2; sel 2"); meja kembali; } /*** membuat tabel; Lebar diatur dengan metode setWidthperCentage () khusus. * * @Return a pdfptable * @throws DocumentException */ public static pdfptable createTable4 () melempar DocumentException {PDFPtable Table = new Pdfptable (3); Persegi panjang rect = persegi panjang baru (523, 770); Table.setWidthperCentage (float baru [] {144, 72, 72}, rect); Sel pdfpcell; sel = PDFPCell baru (frasa baru ("Tabel 4")); cell.setcolspan (3); Table.addcell (sel); sel = PDFPCell baru (frasa baru ("sel dengan rowspan 2")); cell.setrowspan (2); Table.addcell (sel); table.addcell ("Baris 1; sel 1"); Table.addcell ("Baris 1; sel 2"); Table.addcell ("Baris 2; sel 1"); Table.addcell ("Baris 2; sel 2"); meja kembali; } /*** membuat tabel; Lebar diatur dengan setTotalWidth (). * * @return a pdfptable * @throws DocumentException */ public static pdfptable createTable5 () melempar DocumentException {pdfptable tabel = new Pdfptable (3); Table.setTotalWidth (float baru [] {144, 72, 72}); Table.setlockedwidth (true); Sel pdfpcell; sel = PDFPCell baru (frasa baru ("Tabel 5")); cell.setcolspan (3); Table.addcell (sel); sel = PDFPCell baru (frasa baru ("sel dengan rowspan 2")); cell.setrowspan (2); Table.addcell (sel); table.addcell ("Baris 1; sel 1"); Table.addcell ("Baris 1; sel 2"); Table.addcell ("Baris 2; sel 1"); Table.addcell ("Baris 2; sel 2"); meja kembali; } public static pdfptable createTable6 () melempar DocumentException {pdfptable tabel = new Pdfptable (10); Table.SetTotalWidth (595); //table.setlockedwidth(true); Sel pdfpcell; sel = PDFPCell baru (frasa baru ("Tabel 6")); cell.setcolspan (10); Table.addcell (sel); untuk (int i = 1; i <100; i ++) {cell = new Pdfpcell (frase baru (string.valueof (i)))); cell.setBackgroundColor (Basecolor.light_gray); Table.addcell (sel); } // sel = PDFPCell baru (frase baru ("sel dengan rowspan 2")); // cell.setrowspan (2); // table.addcell (sel); // table.addcell ("baris 1; sel 1 "); // table.addcell (" Baris 1; sel 2 "); // Table.addcell (" Baris 2; sel 1 "); // Table.addcell (" Baris 2; sel 2 "); meja kembali; }}
3. 在 PDF 文件中添加图片 , 并且指定文本位置
Paket Console.pdf; import java.io.filenotfoundException; import java.io.fileoutputStream; import com.itextpdf.text.document; impor com.itextpdf.text.documentException; impor com.itextpdf.text .text.pagesize; import com.itextpdf.text.pdf.basefont; import com.itextpdf.text.pdf.pdfcontentByte;文件中添加背景图片 , 并指定文本在 pdf 文件中的位置 */kelas publik latar belakangImageofpdf {public static void main (string [] args) {latar belakangImageOfpdf p001 = new BackgroundImageOfPdf (); String filename = "p003.pdf"; p001.createPdf (nama file); } public void createPdf (string fileName) {// Langkah 1 Dokumen = dokumen baru (pagesize.a4.rotate (), 0,0,0,0); // Langkah 2 Coba {PDFWrITER PDFWRITER = PDFWRITER.GetInstance (Dokumen, FileOutputStream baru (fileName)); document.addtitle ("id.net"); document.addauthor ("dotuian"); document.addsubject ("Ini adalah subjek file PDF."); document.addkeywords ("Ini adalah kata kunci dari file PDF."); // langkah 3 document.open (); // Langkah 4 Image Image = Image.getInstance ("bg.jpg"); document.add (gambar); PDFContentByte PDFContentByte = PDFWriter.GetDirectContent (); pdfcontentByte.begintext (); Basefont BF = Basefont.CreateFont (Basefont.Times_Roman, Basefont.Winansi, Basefont.EMbedded); pdfcontentByte.setfontandsize (bf, 12); untuk (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 (); } akhirnya {// langkah 5 document.close (); }}}