Skip to content

Instantly share code, notes, and snippets.

@mbgarcia
Created November 10, 2020 01:48
Show Gist options
  • Save mbgarcia/be8302335b480f0a631c710375404f8f to your computer and use it in GitHub Desktop.
Save mbgarcia/be8302335b480f0a631c710375404f8f to your computer and use it in GitHub Desktop.
Generate PDF with iText
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class Teste {
public static void main(String[] args) {
try {
File file = new File("itext-test.pdf");
FileOutputStream fileout = new FileOutputStream(file);
Document document = new Document();
PdfWriter.getInstance(document, fileout);
document.addTitle("My iText Test");
document.open();
Paragraph paragraph = new Paragraph();
paragraph.add("Hello World");
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment