Created
August 29, 2018 11:55
-
-
Save abuabdul/3cb54e745d0008b2a4017bea6f68fc33 to your computer and use it in GitHub Desktop.
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 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