itext 下载页面 : http://sourceforge.net/projects/itext/files/
1. 创建简单的 PDF 文件
Console Package.pdf; Import java.io.filenotFoundException; import java.io.fileOutputStream; import com.itextpdf.text.document; import com.itextpdf.text.documentexcept .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 (имя файла); } public void createPdf (String FileName) {// Шаг 1 документ документ = новый документ (pagesize.a4); // Шаг 2 try {pdfwriter.getInstance (document, new FileOutputStream (fileName)); document.addtitle ("id.net"); document.addauthor ("Dotuian"); document.AddSubject («Это предмет файла PDF».); document.addkeywords («Это ключевое слово файла PDF». »); // Шаг 3 Document.open (); // Шаг 4 Document.Add (новый абзац («Привет, мир!»)); } catch (filenotFoundException e) {e.printstackTrace (); } catch (documentException e) {e.printstacktrace (); } наконец {// Шаг 5 Document.Close (); }}}
2. 在 PDF 文件中添加 Таблица
пакет console.pdf; import java.io.filenotfoundexception; import java.io.fileoutputstream; import com.itextpdf.text.basecolor; import com.itextpdf.text.document; importc.itextpdf.text.documentExcept .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 (имя файла); } public void createPdf (String FileName) {// Шаг 1 документ документ = новый документ (pagesize.a4); // Шаг 2 try {pdfwriter.getInstance (document, new FileOutputStream (fileName)); document.addtitle ("id.net"); document.addauthor ("Dotuian"); document.AddSubject («Это предмет файла PDF».); document.addkeywords («Это ключевое слово файла PDF». »); // Шаг 3 Document.open (); // Шаг 4 pdfptable table = createletable1 (); document.add (таблица); Таблица = Createletable2 (); table.setspacingbefore (5); table.setspacingafter (5); document.add (таблица); Table = Createletable3 (); document.add (таблица); Таблица = Createletable4 (); table.setspacingbefore (5); table.setspacingafter (5); document.add (таблица); Таблица = Createletable5 (); document.add (таблица); Table = Createletable6 (); document.add (таблица); } catch (filenotFoundException e) {e.printstackTrace (); } catch (documentException e) {e.printstacktrace (); } наконец {// Шаг 5 Document.Close (); }} /*** Создает таблицу; Ширины устанавливаются с помощью Setwidths (). * * @return a pdfptable * @Throws DocumentException */ public static pdfptable createletable1 () Throws DocumentException {pdfptable table = new pdfptable (3); Table.SetWidthPercentage (288 / 5.23f); table.setwidths (new int [] {2, 1, 1}); Ячейка PDFPCELL; cell = new pdfpcell (новая фраза ("Таблица 1")); cell.setcolspan (3); Таблица. Адцелл (ячейка); cell = new pdfpcell (новая фраза («ячейка с Rowspan 2»)); cell.setrowspan (2); Таблица. Адцелл (ячейка); Таблица. Таблица. Таблица. Таблица. возвратный таблица; } /*** Создает таблицу; Ширины устанавливаются с помощью Setwidths (). * * @return a pdfptable * @Throws DocumentException */ public static pdfptable createbeable2 () Throws DocumentException {pdfptable table = new pdfptable (3); Table.SetTotalWidth (288); table.setlockedwidth (true); table.setWidths (новый float [] {2, 1, 1}); Ячейка PDFPCELL; cell = new pdfpcell (новая фраза ("Таблица 2")); cell.setcolspan (3); Таблица. Адцелл (ячейка); cell = new pdfpcell (новая фраза («ячейка с Rowspan 2»)); cell.setrowspan (2); Таблица. Адцелл (ячейка); Таблица. Таблица. Таблица. Таблица. возвратный таблица; } /*** Создает таблицу; Ширина установлена в конструкторе. * * @return a pdfptable * @Throws DocumentException */ public static pdfptable createbeable3 () Throws DocumentException {pdfptable table = new pdfptable (new float [] {2, 1, 1}); Table.SetWidthPercentage (55.067f); Ячейка PDFPCELL; cell = new pdfpcell (новая фраза ("Таблица 3")); cell.setcolspan (3); Таблица. Адцелл (ячейка); cell = new pdfpcell (новая фраза («ячейка с Rowspan 2»)); cell.setrowspan (2); Таблица. Адцелл (ячейка); Таблица. Таблица. Таблица. Таблица. возвратный таблица; } /*** Создает таблицу; Ширины устанавливаются с помощью специального метода SetWidthPercentage (). * * @return a pdfptable * @Throws DocumentException */ public Static pdfptable createbeable4 () Throws DocumentException {pdfptable table = new pdfptable (3); Прямоугольник прямоугольник = новый прямоугольник (523, 770); table.setWidthPercentage (New Float [] {144, 72, 72}, rect); Ячейка PDFPCELL; cell = new pdfpcell (новая фраза ("Таблица 4")); cell.setcolspan (3); Таблица. Адцелл (ячейка); cell = new pdfpcell (новая фраза («ячейка с Rowspan 2»)); cell.setrowspan (2); Таблица. Адцелл (ячейка); Таблица. Таблица. Таблица. Таблица. возвратный таблица; } /*** Создает таблицу; Ширина установлена с помощью CetTotalWidth (). * * @return a pdfptable * @Throws DocumentException */ public Static pdfptable createblebe5 () Throws DocumentException {pdfptable table = new pdfptable (3); table.settotalWidth (новый float [] {144, 72, 72}); table.setlockedwidth (true); Ячейка PDFPCELL; cell = new pdfpcell (новая фраза ("Таблица 5")); cell.setcolspan (3); Таблица. Адцелл (ячейка); cell = new pdfpcell (новая фраза («ячейка с Rowspan 2»)); cell.setrowspan (2); Таблица. Адцелл (ячейка); Таблица. Таблица. Таблица. Таблица. возвратный таблица; } public static pdfptable createletable6 () Throws DocumentException {pdfptable table = new pdfptable (10); Table.SetTotalWidth (595); //table.setlockedwidth(true); Ячейка PDFPCELL; cell = new pdfpcell (новая фраза ("Таблица 6")); cell.setcolspan (10); Таблица. Адцелл (ячейка); for (int i = 1; i <100; i ++) {cell = new pdfpcell (новая фраза (string.valueof (i))); cell.setbackgroundcolor (basecolor.light_gray); Таблица. Адцелл (ячейка); } // cell = new pdfpcell (новая фраза («ячейка с Rowspan 2»)); // cell.setRowspan (2); // table.addcell (cell); // table.addcell ("ряд 1; ячейка 1 "); // table.addcell (" ряд 1; ячейка 2 "); // table.addcell (" ряд 2; ячейка 1 "); // table.addcell (" ряд 2; ячейка 2 "); возвратный таблица; }}
3. 在 PDF 文件中添加图片 , 并且指定文本位置
Console Package.pdf; Import java.io.filenotFoundException; import java.io.fileOutputStream; import com.itextpdf.text.document; import com.itextpdf.text.documentexcept .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 founalimageofpdf {public static void main (string [] args) {founalImageOfPdf p001 = new FounalImageOfPdf (); String fileName = "p003.pdf"; p001.createpdf (имя файла); } public void createPdf (String FileName) {// Шаг 1 документ документ = новый документ (pagesize.a4.rotate (), 0,0,0,0); // Шаг 2 try {pdfwriter pdfwriter = pdfwriter.getinstance (document, new FileOutputStream (имя файла)); document.addtitle ("id.net"); document.addauthor ("Dotuian"); document.AddSubject («Это предмет файла PDF».); document.addkeywords («Это ключевое слово файла PDF». »); // Шаг 3 Document.open (); // Шаг 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 (); } наконец {// Шаг 5 Document.Close (); }}}