The purpose of this code is to add mailing addresses to a newsletter, but could to modified to customize any type of document, making it both personal and professional.
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
public class labels {
public static void main(String[] args) throws InvalidPasswordException, IOException {
File file = new File("C:\Template.pdf");
PDDocument document = PDDocument.load(file);
PDPage page = document.getPage(0);
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true);
contentStream.setFont(PDType1Font.TIMES_ROMAN, 20);
contentStream.beginText();
contentStream.newLineAtOffset(280, 630);
String text1 = args[0];
contentStream.showText(text1);
contentStream.endText();
contentStream.beginText();
contentStream.newLineAtOffset(280, 600);
String text2 = args[1];
contentStream.showText(text2);
contentStream.endText();
contentStream.beginText();
contentStream.newLineAtOffset(280, 570);
String text3 = args[2];
contentStream.showText(text3);
contentStream.endText();
contentStream.close();
String outputfile = "C:\Page-" + args[3] + ".pdf";
document.save(new File(outputfile));
document.close();
}
}
No comments:
Post a Comment