Created
November 4, 2022 09:12
-
-
Save migueldoctor/83f4fb39f8d066606b65c1cfa2f6240f to your computer and use it in GitHub Desktop.
Sample 4 header and footer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kesizo.java.pdf.sample; | |
import java.awt.*; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import com.lowagie.text.*; | |
import com.lowagie.text.Font; | |
import com.lowagie.text.Rectangle; | |
import com.lowagie.text.pdf.PdfContentByte; | |
import com.lowagie.text.pdf.PdfPageEventHelper; | |
import com.lowagie.text.pdf.PdfWriter; | |
public class Main { | |
public static void main(String[] args) { | |
//1) Configure the layout of the document by adding | |
// parameters to the constructor (size and margins) | |
Document myPDFDoc = new Document(PageSize.A4, | |
40f, // left | |
40f, // right | |
200f, // top | |
150f); // down | |
//2) Create an object for the footer that will display | |
//. a rectangle in color black | |
Rectangle footer = new Rectangle(30f, 30f, PageSize.A4.getRight(30f), 140f); | |
footer.setBorder(Rectangle.BOX); | |
footer.setBorderColor(Color.BLACK); | |
footer.setBorderWidth(2f); | |
// 3) Create another object for header (rectangle in color blue) | |
Rectangle header = new Rectangle(30f, 30f, PageSize.A4.getRight(30f), 140f); | |
header.setBorder(Rectangle.BOX); | |
header.setBorderColor(Color.BLUE); | |
header.setBorderWidth(1f); | |
header.setTop(PageSize.A4.getTop(30f)); | |
header.setBottom(PageSize.A4.getTop(180f)); | |
// Define a string as title | |
String title = "Learning OpenPDF with Java"; | |
// Define a paragraph | |
String lorenIpsum1 = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo"+ | |
" ligula eget dolor. Aenean massa.Cum sociis natoque penatibus et magnis dis" + | |
" parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec," + | |
" pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim"+ | |
" Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu."+ | |
" In enim justo, rhoncus ut, imperdiet a, venenatis vitae,"+ | |
" justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt."+ | |
" Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus."; | |
try { | |
FileOutputStream pdfOutputFile = new FileOutputStream("./sample1.pdf"); | |
final PdfWriter pdfWriter = PdfWriter.getInstance(myPDFDoc, pdfOutputFile); | |
// 4) Use an object expression to configure and register page event | |
// in charge of adding the header and the footer | |
// create and register page event to add the rectangles | |
pdfWriter.setPageEvent(new PdfPageEventHelper() { | |
@Override | |
public void onEndPage(PdfWriter writer, Document document) { | |
PdfContentByte cb = writer.getDirectContent(); | |
cb.rectangle(header); | |
cb.rectangle(footer); | |
} | |
}); | |
myPDFDoc.open(); // Open the Document | |
/* Here we add some metadata to the generated pdf */ | |
myPDFDoc.addTitle("This is a simple PDF example"); | |
myPDFDoc.addSubject("This is a tutorial explaining how to use openPDF"); | |
myPDFDoc.addKeywords("Java, OpenPDF, Basic sample"); | |
myPDFDoc.addCreator("Miguel and Kesizo.com"); | |
myPDFDoc.addAuthor("Miguel Doctor"); | |
/* End of the adding metadata section */ | |
// Create a Font object | |
Font titleFont = new Font(Font.COURIER, 20f, Font.BOLDITALIC, Color.BLUE); | |
// Create a paragraph with the new font | |
Paragraph paragraph = new Paragraph(title,titleFont); | |
// Element class provides properties to align | |
// Content elements within the document | |
paragraph.setAlignment(Element.ALIGN_CENTER); | |
myPDFDoc.add(paragraph); | |
// Adding an empty line | |
myPDFDoc.add(new Paragraph(Chunk.NEWLINE)); | |
// Include the text as content of the pdf | |
myPDFDoc.add(new Paragraph(lorenIpsum1)); | |
myPDFDoc.close(); | |
pdfWriter.close(); | |
} catch (IOException de) { | |
System.err.println(de.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment