Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save documentprocessing/7b4dd0530fddb070e59b061862cb36ab to your computer and use it in GitHub Desktop.
Save documentprocessing/7b4dd0530fddb070e59b061862cb36ab to your computer and use it in GitHub Desktop.
Create PDF using PDF Clown API for Java
// Import PDF Clown core classes
import org.pdfclown.documents.Document;
import org.pdfclown.documents.Page;
import org.pdfclown.documents.contents.composition.PrimitiveComposer;
import org.pdfclown.documents.contents.fonts.StandardType1Font;
import org.pdfclown.files.File;
import java.awt.geom.Point2D;
public class CreateBasicPDF {
public static void main(String[] args) {
try {
// 1. Instantiate a new PDF file
File pdfFile = new File();
Document document = pdfFile.getDocument();
// 2. Add a page to the document
Page page = new Page(document);
document.getPages().add(page);
// 3. Create a composer to write content
PrimitiveComposer composer = new PrimitiveComposer(page);
// 4. Set font and write text
composer.setFont(new StandardType1Font(document, StandardType1Font.FamilyEnum.TIMES_ROMAN, true, false), 24);
composer.showText("Welcome to PDF Clown!", new Point2D.Double(50, 700));
composer.setFont(new StandardType1Font(document, StandardType1Font.FamilyEnum.TIMES_ROMAN, false, false), 12);
composer.showText("This PDF was generated programmatically using:", new Point2D.Double(50, 650));
composer.showText("- Java " + System.getProperty("java.version"), new Point2D.Double(70, 630));
composer.showText("- PDF Clown " + pdfFile.getVersion(), new Point2D.Double(70, 610));
// 5. Flush the composer and save the file
composer.flush();
pdfFile.save("output.pdf");
System.out.println("PDF created successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment