itext : http://sourceforge.net/projects/itext/files/
1. 创建简单的 pdf 文件
package console.pdf ؛ import java.io.filenotfoundException ؛ import java.io.fileOutputStream ؛ import com.itextpdf.text .text.paragraph ؛ import com.itextpdf.text.pdf.pdfWriter ؛/** * 使用 itext 生成 pdf 文件 */public class createpdf {public static void main (string [] args) {createpdf p001 = new CreatePdf () ؛ اسم ملف السلسلة = "p001.pdf" ؛ p001.createpdf (اسم الملف) ؛ } public void createpdf (اسم ملف السلسلة) {// الخطوة 1 مستند المستند = مستند جديد (pagesize.a4) ؛ // الخطوة 2 حاول {pdfWriter.getInstance (وثيقة ، FileOutputStream جديدة (اسم الملف)) ؛ document.addtitle ("id.net") ؛ document.addauthor ("dotuian") ؛ document.addsubject ("هذا هو موضوع ملف PDF.") ؛ document.addkeywords ("هذه هي الكلمة الرئيسية لملف pdf.") ؛ // الخطوة 3 document.open () ؛ // الخطوة 4 document.add (فقرة جديدة ("Hello World!")) ؛ } catch (fileNotFoundException e) {e.printStackTrace () ؛ } catch (documentException e) {E.PrintStackTrace () ؛ } أخيرًا {// الخطوة 5 document.close () ؛ }}}
2. 在 PDF 文件中添加 الجدول
package console.pdf ؛ import java.io.filenotfoundException ؛ import java.io.fileOutputStream ؛ import com.itextpdf.text.basecolor ؛ import com.itextpdf.text .text.pagesize ؛ import com.itextpdf.text.phrase ؛ import com.itextpdf.text.rectangle ؛ import com.itextpdf.text.pdf.pdfpcell ؛ import com.itextpdf.pdf.pdfptable ؛ import com.itextpdf. text.pdf.pdfwriter ؛/** * 使用 itext 生成 pdf 文件 * 在 pdf 文件中创建表格 */public class tableofpdf {public static void main (string [] args) {tableofpdf p001 = new tableofpdf () ؛ اسم ملف السلسلة = "p002.pdf" ؛ p001.createpdf (اسم الملف) ؛ } public void createpdf (اسم ملف السلسلة) {// الخطوة 1 مستند المستند = مستند جديد (pagesize.a4) ؛ // الخطوة 2 حاول {pdfWriter.getInstance (وثيقة ، FileOutputStream جديدة (اسم الملف)) ؛ document.addtitle ("id.net") ؛ document.addauthor ("dotuian") ؛ document.addsubject ("هذا هو موضوع ملف PDF.") ؛ document.addkeywords ("هذه هي الكلمة الرئيسية لملف pdf.") ؛ // الخطوة 3 document.open () ؛ // الخطوة 4 pdfptable table = createTable1 () ؛ document.add (الجدول) ؛ الجدول = createTable2 () ؛ table.setspacingbefore (5) ؛ Table.SetSpacingAfter (5) ؛ document.add (الجدول) ؛ الجدول = createTable3 () ؛ document.add (الجدول) ؛ الجدول = createTable4 () ؛ table.setspacingbefore (5) ؛ Table.SetSpacingAfter (5) ؛ document.add (الجدول) ؛ الجدول = createTable5 () ؛ document.add (الجدول) ؛ الجدول = createTable6 () ؛ document.add (الجدول) ؛ } catch (fileNotFoundException e) {e.printStackTrace () ؛ } catch (documentException e) {E.PrintStackTrace () ؛ } أخيرًا {// الخطوة 5 document.close () ؛ }} /*** إنشاء جدول ؛ يتم تعيين العروض مع setWidths (). * * return a pdfptable * athrows documentException */ public pdfptable createTable1 () يلقي documentException {pdfptable table = new pdfptable (3) ؛ table.setWidThPerCenge (288 / 5.23f) ؛ table.setWidths (new int [] {2 ، 1 ، 1}) ؛ خلية PDFPCELL ؛ الخلية = pdfpcell جديد (عبارة جديدة ("الجدول 1")) ؛ cell.setColspan (3) ؛ table.addcell (الخلية) ؛ الخلية = pdfpcell جديدة (عبارة جديدة ("الخلية مع Rowspan 2")) ؛ cell.setRowspan (2) ؛ table.addcell (الخلية) ؛ table.addcell ("الصف 1 ؛ الخلية 1") ؛ table.addcell ("الصف 1 ؛ الخلية 2") ؛ table.addcell ("الصف 2 ؛ الخلية 1") ؛ table.addcell ("الصف 2 ؛ الخلية 2") ؛ جدول العودة } /*** إنشاء جدول ؛ يتم تعيين العروض مع setWidths (). * * return a pdfptable * throws documentException */ public pdfptable createTable2 () يلقي documentException {pdfptable table = new pdfptable (3) ؛ Table.SettotalWidth (288) ؛ table.setlockedwidth (true) ؛ table.setWidths (تعويم جديد [] {2 ، 1 ، 1}) ؛ خلية PDFPCELL ؛ الخلية = pdfpcell جديد (عبارة جديدة ("الجدول 2")) ؛ cell.setColspan (3) ؛ table.addcell (الخلية) ؛ الخلية = pdfpcell جديدة (عبارة جديدة ("الخلية مع Rowspan 2")) ؛ cell.setRowspan (2) ؛ table.addcell (الخلية) ؛ table.addcell ("الصف 1 ؛ الخلية 1") ؛ table.addcell ("الصف 1 ؛ الخلية 2") ؛ table.addcell ("الصف 2 ؛ الخلية 1") ؛ table.addcell ("الصف 2 ؛ الخلية 2") ؛ جدول العودة } /*** إنشاء جدول ؛ يتم تعيين العروض في المنشئ. * * RETURN A PDFPTable * athrows documentException */ public pdfptable createTable3 () يلقي DocumentException {pdfptable table = new pdfptable (تعويم جديد [] {2 ، 1 ، 1}) ؛ table.setWidThPerCenge (55.067f) ؛ خلية PDFPCELL ؛ الخلية = pdfpcell جديد (عبارة جديدة ("الجدول 3")) ؛ cell.setColspan (3) ؛ table.addcell (الخلية) ؛ الخلية = pdfpcell جديدة (عبارة جديدة ("الخلية مع Rowspan 2")) ؛ cell.setRowspan (2) ؛ table.addcell (الخلية) ؛ table.addcell ("الصف 1 ؛ الخلية 1") ؛ table.addcell ("الصف 1 ؛ الخلية 2") ؛ table.addcell ("الصف 2 ؛ الخلية 1") ؛ table.addcell ("الصف 2 ؛ الخلية 2") ؛ جدول العودة } /*** إنشاء جدول ؛ يتم تعيين العروض مع طريقة setWidThperCenage () خاصة. * * return a pdfptable * athrows documentException */ public static pdfptable createTable4 () يلقي documentException {pdfptable table = new pdfptable (3) ؛ المستطيل المستقيم = مستطيل جديد (523 ، 770) ؛ table.setWidThPerCenge (تعويم جديد [] {144 ، 72 ، 72} ، rect) ؛ خلية PDFPCELL ؛ الخلية = pdfpcell جديدة (عبارة جديدة ("الجدول 4")) ؛ cell.setColspan (3) ؛ table.addcell (الخلية) ؛ الخلية = pdfpcell جديدة (عبارة جديدة ("الخلية مع Rowspan 2")) ؛ cell.setRowspan (2) ؛ table.addcell (الخلية) ؛ table.addcell ("الصف 1 ؛ الخلية 1") ؛ table.addcell ("الصف 1 ؛ الخلية 2") ؛ table.addcell ("الصف 2 ؛ الخلية 1") ؛ table.addcell ("الصف 2 ؛ الخلية 2") ؛ جدول العودة } /*** إنشاء جدول ؛ يتم تعيين العروض مع settotalwidth (). * * return a pdfptable * athrows documentException */ prubl static pdfptable createTable5 () يلقي documentException {pdfptable table = new pdfptable (3) ؛ Table.SetTotalWidth (تعويم جديد [] {144 ، 72 ، 72}) ؛ table.setlockedwidth (true) ؛ خلية PDFPCELL ؛ الخلية = pdfpcell جديد (عبارة جديدة ("الجدول 5")) ؛ cell.setColspan (3) ؛ table.addcell (الخلية) ؛ الخلية = pdfpcell جديدة (عبارة جديدة ("الخلية مع Rowspan 2")) ؛ cell.setRowspan (2) ؛ table.addcell (الخلية) ؛ table.addcell ("الصف 1 ؛ الخلية 1") ؛ table.addcell ("الصف 1 ؛ الخلية 2") ؛ table.addcell ("الصف 2 ؛ الخلية 1") ؛ table.addcell ("الصف 2 ؛ الخلية 2") ؛ جدول العودة } static pdfptable createTable6 () يلقي DocumentException {pdfptable table = new pdfptable (10) ؛ Table.SettotalWidth (595) ؛ //table.setlockedwidth(true) ؛ خلية PDFPCELL ؛ الخلية = pdfpcell جديدة (عبارة جديدة ("الجدول 6")) ؛ cell.setColspan (10) ؛ table.addcell (الخلية) ؛ لـ (int i = 1 ؛ i <100 ؛ i ++) {cell = new pdfpcell (عبارة جديدة (string.valueof (i))) ؛ cell.setbackgroundColor (basecolor.light_gray) ؛ table.addcell (الخلية) ؛ } // cell = pdfpcell جديد (عبارة جديدة ("الخلية مع Rowspan 2"))) ؛ ") ؛ // table.addcell (" الصف 1 ؛ الخلية 2 ") ؛ // table.addcell (" الصف 2 ؛ الخلية 1 ") ؛ // table.addcell (" الصف 2 ؛ الخلية 2 ") ؛ جدول العودة }}
3. 在 pdf 文件中添加图片 , 并且指定文本位置
package console.pdf ؛ import java.io.filenotfoundException ؛ import java.io.fileOutputStream ؛ import com.itextpdf.text .text.pagesize ؛ استيراد com.itextpdf.text.pdf.basefont ؛ استيراد com.itextpdf.text.pdf.pdfcontentbyte ؛ import com.itextpdf.text.pdf.pdfwriter文件中添加背景图片 , 并指定文本在 pdf 文件中的位置 */public class backgroundImageOfpdf {public static void main (string [] args) {backgroundImageOfPdf p001 = new backgroundImageOfPdf () ؛ اسم ملف السلسلة = "p003.pdf" ؛ p001.createpdf (اسم الملف) ؛ } public void createpdf (اسم ملف السلسلة) {// الخطوة 1 مستند المستند = مستند جديد (pagesize.a4.rotate () ، 0،0،0،0) ؛ // الخطوة 2 جرب {pdfwriter pdfwriter = pdfwriter.getInstance (وثيقة ، fileOutputStream (filename)) ؛ document.addtitle ("id.net") ؛ document.addauthor ("dotuian") ؛ document.addsubject ("هذا هو موضوع ملف PDF.") ؛ document.addkeywords ("هذه هي الكلمة الرئيسية لملف pdf.") ؛ // الخطوة 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) ؛ لـ (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 (استثناء e) {E.PrintStackTrace () ؛ } أخيرًا {// الخطوة 5 document.close () ؛ }}}