Skip to content

Instantly share code, notes, and snippets.

@abuabdul
Created August 29, 2018 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abuabdul/3cb54e745d0008b2a4017bea6f68fc33 to your computer and use it in GitHub Desktop.
Save abuabdul/3cb54e745d0008b2a4017bea6f68fc33 to your computer and use it in GitHub Desktop.
package net.consensys.landstream.pdf;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
public class Test {
public static void main(String[] args) {
String uuid = UUID.randomUUID().toString();
try {
File tmpFile = File.createTempFile(uuid, ".pdf");
PDDocument document = new PDDocument();
PDPage firstPage = new PDPage();
document.addPage(firstPage);
PDPageContentStream contentStream = new PDPageContentStream(document, firstPage);
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, 11);
contentStream.setLeading(15f);
contentStream.newLineAtOffset(25, 770);
for (String field : Arrays.asList("Hello", "This is an example of adding text to a page in the pdf document. we can add as many lines", "Hru", "How", "Hello", "Hello", "Hello", "Hello",
"Hello", "Ok")) {
contentStream.showText(field);
contentStream.newLine();
}
contentStream.endText();
contentStream.close();
document.save(tmpFile.getAbsolutePath());
document.close();
} catch (IOException io) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment