ITEXT 下载页面 : http://sourceforge.net/projects/itext/files/
1. 创建简单的 PDF 文件
Paketkonsole.pdf; importieren java.io.filenotfoundException; import Java.io.filoutputStream; importieren com.itextpdf.text.document; com.itextpdf.text.documentException; importieren .text.paragraph; import com.itextpdf.text.pdf.pdfwriter;/** * 使用 itext 生成 pdf 文件 */public createpdf {public static void main (String [] args) {createpdf p001 = new CreatePdf (); String Dateiname = "p001.pdf"; p001.Createpdf (Dateiname); } public void createpdf (String -Dateiname) {// Schritt 1 Dokument Dokument = neues Dokument (pageSize.a4); // Schritt 2 Try {pdfwriter.getInstance (Dokument, neuer FileOutputStream (Dateiname)); document.addtitle ("id.net"); document.addAuthor ("dotuian"); document.addsubject ("Dies ist Gegenstand der PDF -Datei."); document.addKeywords ("Dies ist das Schlüsselwort der PDF -Datei."); // Schritt 3 document.open (); // Schritt 4 document.add (neuer Absatz ("Hallo Welt!")); } catch (FilenotFoundException e) {e.printstacktrace (); } catch (documentException e) {e.printstacktrace (); } endlich {// Schritt 5 document.close (); }}}
2. 在 PDF 文件中添加 Tabelle
Paketkonsole.pdf; importieren java.io.filenotfoundException; import Java.io.filoutputStream; importieren com.itextpdf.text.basecolor; com.itextpdf.text.document; com.itextpdf.text.documentException; com.itextpdf .Text.pageSize; importieren com.itextpdf.text.phrase; import com.itextpdf.text.rectangle; import com.itextpdf.text.pdf.pdfpcell; import com.itextpdf.text.pdf.pdfptable; com.itextpdf. text.pdf.pdfwriter;/** * 使用 itext 生成 pdf 文件 * 在 pdf 文件中创建表格 */public class tableofpdf {public static void main (String [] args) {tableofpdf p001 = new tableofpdf (); String Dateiname = "p002.pdf"; p001.Createpdf (Dateiname); } public void createpdf (String -Dateiname) {// Schritt 1 Dokument Dokument = neues Dokument (pageSize.a4); // Schritt 2 Try {pdfwriter.getInstance (Dokument, neuer FileOutputStream (Dateiname)); document.addtitle ("id.net"); document.addAuthor ("dotuian"); document.addsubject ("Dies ist Gegenstand der PDF -Datei."); document.addKeywords ("Dies ist das Schlüsselwort der PDF -Datei."); // Schritt 3 document.open (); // Schritt 4 pdfptable table = createTable1 (); document.add (Tabelle); Tabelle = createTable2 (); Tabelle.SetSpacingBefore (5); Tabelle.SetSpacingAfter (5); document.add (Tabelle); Tabelle = createTable3 (); document.add (Tabelle); Tabelle = createTable4 (); Tabelle.SetSpacingBefore (5); Tabelle.SetSpacingAfter (5); document.add (Tabelle); Tabelle = createTable5 (); document.add (Tabelle); Tabelle = createTable6 (); document.add (Tabelle); } catch (FilenotFoundException e) {e.printstacktrace (); } catch (documentException e) {e.printstacktrace (); } endlich {// Schritt 5 document.close (); }} /*** erstellt eine Tabelle; Breiten werden mit Setwidths () eingestellt. * * @return a pdfptable * @throws documentException */ public static pdfptable createTable1 () löst documentException {pdfptable table = new pdfptable (3) aus; table.setwidtHThpercentage (288 / 5.23f); table.setwidths (neu int [] {2, 1, 1}); PDFPCell -Zelle; cell = neuer pdfpcell (neuer Phrase ("Tabelle 1")); cell.setColspan (3); table.addcell (Zelle); cell = neuer pdfpcell (neue Phrase ("Cell with rowspan 2")); cell.setrowspan (2); table.addcell (Zelle); Tabelle.Addcell ("Zeile 1; Zelle 1"); table.addcell ("Zeile 1; Zelle 2"); table.addcell ("Zeile 2; Zelle 1"); table.addcell ("Zeile 2; Zelle 2"); Rückkehrtabelle; } /*** erstellt eine Tabelle; Breiten werden mit Setwidths () eingestellt. * * @return a pdfptable * @throws documentException */ public static pdfptable createTable2 () löst documentException {pdfptable table = new pdfptable (3) aus; Tabelle.Settotalwidth (288); table.setLockedwidth (true); table.setwidths (neuer float [] {2, 1, 1}); PDFPCell -Zelle; cell = neuer pdfpcell (neuer Phrase ("Tabelle 2")); cell.setColspan (3); table.addcell (Zelle); cell = neuer pdfpcell (neue Phrase ("Cell with rowspan 2")); cell.setrowspan (2); table.addcell (Zelle); Tabelle.Addcell ("Zeile 1; Zelle 1"); table.addcell ("Zeile 1; Zelle 2"); table.addcell ("Zeile 2; Zelle 1"); table.addcell ("Zeile 2; Zelle 2"); Rückkehrtabelle; } /*** erstellt eine Tabelle; Breiten werden im Konstruktor eingestellt. * * @return a pdfptable * @throws documentException */ public static pdfptable createTable3 () löst documentException {pdfptable table = new pdfptable (new float [] {2, 1, 1}); table.setwidtHThpercentage (55.067f); PDFPCell -Zelle; cell = neuer pdfpcell (neuer Phrase ("Tabelle 3")); cell.setColspan (3); table.addcell (Zelle); cell = neuer pdfpcell (neue Phrase ("Cell with rowspan 2")); cell.setrowspan (2); table.addcell (Zelle); Tabelle.Addcell ("Zeile 1; Zelle 1"); table.addcell ("Zeile 1; Zelle 2"); table.addcell ("Zeile 2; Zelle 1"); table.addcell ("Zeile 2; Zelle 2"); Rückkehrtabelle; } /*** erstellt eine Tabelle; Die Breiten werden mit einer speziellen Methode für SetwidtHThpercentage () festgelegt. * * @return a pdfptable * @throws documentException */ public static pdfptable createStable4 () löst documentException {pdfptable table = new pdfptable (3) aus; Rechteck rechteck = neues Rechteck (523, 770); table.setwidthpercentage (neuer float [] {144, 72, 72}, rect); PDFPCell -Zelle; cell = neuer pdfpcell (neuer Phrase ("Tabelle 4")); cell.setColspan (3); table.addcell (Zelle); cell = neuer pdfpcell (neue Phrase ("Cell with rowspan 2")); cell.setrowspan (2); table.addcell (Zelle); Tabelle.Addcell ("Zeile 1; Zelle 1"); table.addcell ("Zeile 1; Zelle 2"); table.addcell ("Zeile 2; Zelle 1"); table.addcell ("Zeile 2; Zelle 2"); Rückkehrtabelle; } /*** erstellt eine Tabelle; Breiten werden mit settotalerbreite () eingestellt. * * @return a pdfptable * @throws documentException */ public static pdfptable createTable5 () löst documentException {pdfptable table = new pdfptable (3) aus; table.settotalwidth (neuer float [] {144, 72, 72}); table.setLockedwidth (true); PDFPCell -Zelle; cell = neuer pdfpcell (neuer Phrase ("Tabelle 5")); cell.setColspan (3); table.addcell (Zelle); cell = neuer pdfpcell (neue Phrase ("Cell with rowspan 2")); cell.setrowspan (2); table.addcell (Zelle); Tabelle.Addcell ("Zeile 1; Zelle 1"); table.addcell ("Zeile 1; Zelle 2"); table.addcell ("Zeile 2; Zelle 1"); table.addcell ("Zeile 2; Zelle 2"); Rückkehrtabelle; } public static pdfptable createTable6 () löst documentException {pdfptable table = new pdfptable (10); Tabelle.Settotalwidth (595); //table.setLockedWidth(True); PDFPCell -Zelle; cell = neuer pdfpcell (neuer Phrase ("Tabelle 6")); cell.setColspan (10); table.addcell (Zelle); für (int i = 1; i <100; i ++) {cell = new pdfpcell (neue Phrase (String.Valueof (i))); cell.setbackgroundColor (Basicolor.light_gray); table.addcell (Zelle); } // cell = new pdfpcell (neue Phrase ("Cell with rowspan 2"); // cell.setrowspan (2); // table.addcell (cell); // table.addcell ("Zeile 1; Cell 1 "); // Table.Addcell (" Zeile 1; Rückkehrtabelle; }}
3. 在 PDF 文件中添加图片 , 并且指定文本位置
Paket console.pdf; import java.io.filenotfoundException; import Java.io.FileOutputStream; importieren com.itextpdf.text.document; com.itextpdf.text.documentException; com.itextpdf.text.image; 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 HintergrundImageofpdf {public static void main (String [] args) {HintergrundImageOfpdf p001 = neuer HintergrundImageOfpdf (); String Dateiname = "p003.pdf"; p001.Createpdf (Dateiname); } public void createpdf (String -Dateiname) {// Schritt 1 Dokument = neues Dokument (pageSize.a4.Rotate (), 0,0,0,0); // Schritt 2 Try {pdfwriter pdfwriter = pdfwriter.getInstance (Dokument, neuer FileOutputStream (Dateiname)); document.addtitle ("id.net"); document.addAuthor ("dotuian"); document.addsubject ("Dies ist Gegenstand der PDF -Datei."); document.addKeywords ("Dies ist das Schlüsselwort der PDF -Datei."); // Schritt 3 document.open (); // Schritt 4 Image Image = Image.getInstance ("Bg.jpg"); document.add (Bild); PdfContentByte pdfContentByte = pdfWriter.getDirectContent (); pdfContentByte.beGinText (); BaseFont Bf = BaseFont.CreateInt (BaseFont.times_roman, BaseFont.winansi, BaseFont.Embedded); pdfContentByte.setFontandSize (BF, 12); für (int i = 0; i <= 842; i = i + 50) {für (int j = 0; j <= 595; j = j + 20) {pdfContentByte.settTextMatrix (i, j); pdfContentByte.showText ("(" + i + ":" + j + ")"); }} pdfContentByte.endText (); } catch (FilenotFoundException e) {e.printstacktrace (); } catch (documentException e) {e.printstacktrace (); } catch (Ausnahme e) {e.printstacktrace (); } endlich {// Schritt 5 document.close (); }}}