Java Open Source Tool Itext generates a simple instance of PDF _java

Source: Internet
Author: User

Itext download page: http://sourceforge.net/projects/itext/files/
1. Create a simple PDF file

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; /** * using Itext to generate PDF files/public class CreatePDF {public static void main (string[] args) {createpdf p001 = new Cr

    Eatepdf ();
    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. Add table in PDF file

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; /** * using Itext to generate PDF files * Create a form in a PDF file */public class Tableofpdf {public static void main (string[] args) {tableof

    PDF 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 Documente
    xception {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 Documente
    xception {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 Documente
    xception {pdfptable table = new pdfptable (3);
    Rectangle rect = new Rectangle (523, 770);
    Table.setwidthpercentage (new float[] {144, A., 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 Documente
   xception {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 < 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. Add a picture to the PDF file and specify the text position

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; /** * using Itext to generate PDF files * Add a background picture to the PDF file and specify the position of the text in the PDF file * * 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 +) {for (int j = 0; J <= 595; j = j +) {Pdfcontentbyte.set
          Textmatrix (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 (); }
  }
}



 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.