Java generates instance code for PDF files

  • 2020-04-01 01:52:55
  • OfStack


package com.qhdstar.java.pdf;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;

public class GeneratePDF {

 public static void main(String[] args) {

  //The first method is called to generate a file named ITextTest. PDF to disk C
  try {
   writeSimplePdf();
  } 
  catch (Exception e) { e.printStackTrace(); }

  
  //The second method is called to add a section to the file named ITextTest. PDF on disk C.
  try {
   writeCharpter();
  } 
  catch (Exception e) { e.printStackTrace(); }

  
 }
 
 public static void writeSimplePdf() throws Exception {

  //1. Create a new document object
  //The first parameter is the page size. The next parameters are left, right, top, and bottom margins.
  Document document = new Document(PageSize.A4, 50, 50, 50, 50);

  //2. Create a Writer to associate with the document object, through which documents can be written to disk.
  //The first parameter to create a PdfWriter object is a reference to the document object, and the second parameter is the actual name of the file, in which the output path is also given.
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\ITextTest.pdf"));

  //Open the document
  document.open();

  //4. Add content to the document
  //Through the com. Lowagie. Text. Com.lowagie.text.paragraph to add text. You can create a default paragraph with the text and its default font, color, size, and so on
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("Some more text on the  first page with different color and font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200))));

  //Close the document
  document.close();
 }

 
 public static void writeCharpter() throws Exception {

  //  new document object The first parameter is the page size. The next parameters are left, right, top, and bottom margins.
  Document document = new Document(PageSize.A4, 20, 20, 20, 20);

  //Creates a Writer associated with the document object that writes documents to disk.
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\ITextTest.pdf"));

  //Open the file
  document.open();

  //The title
  document.addTitle("Hello mingri example");

  //The author
  document.addAuthor("wolf");

  //The theme
  document.addSubject("This example explains how to add metadata.");
  document.addKeywords("iText, Hello mingri");
  document.addCreator("My program using iText");

  // document.newPage();
  //Add content to the document
  document.add(new Paragraph("n"));
  document.add(new Paragraph("n"));
  document.add(new Paragraph("n"));
  document.add(new Paragraph("n"));
  document.add(new Paragraph("n"));
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD, new Color(0, 0, 0))));
  Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));

  //A new chapter
  Chapter chapter1 = new Chapter(title1, 1);
  chapter1.setNumberDepth(0);
  Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
  Section section1 = chapter1.addSection(title11);
  Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
  section1.add(someSectionText);
  someSectionText = new Paragraph("Following is a 3 X 2 table.");
  section1.add(someSectionText);
  document.add(chapter1);

  //Close the document
  document.close();
 }
 
} 


Related articles: