Skip to content

Instantly share code, notes, and snippets.

@documentprocessing
Created June 2, 2025 16:08
Show Gist options
  • Select an option

  • Save documentprocessing/770ae178323d93c6cd5646ae3ae1775c to your computer and use it in GitHub Desktop.

Select an option

Save documentprocessing/770ae178323d93c6cd5646ae3ae1775c to your computer and use it in GitHub Desktop.
Create Simple PDF in Java
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
public class SimplePdfCreator {
public static void main(String[] args) {
// Step 1: Create a Document object
Document document = new Document();
try {
// Step 2: Initialize PdfWriter
PdfWriter.getInstance(
document,
new FileOutputStream("simple-document.pdf")
);
// Step 3: Open the Document
document.open();
// Step 4: Add content
Font titleFont = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 18);
Font bodyFont = FontFactory.getFont(FontFactory.HELVETICA, 12);
document.add(new Paragraph("OpenPDF Demo Document", titleFont));
document.add(new Paragraph("This is a sample PDF created with OpenPDF.", bodyFont));
document.add(new Paragraph("Features:", bodyFont));
document.add(new Paragraph("- Text Styling", bodyFont));
document.add(new Paragraph("- Font Embedding", bodyFont));
// Step 5: Close the Document
document.close();
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