Monday, November 6, 2017

Writing to PDf file by using PDFBox Java

package MyPackage;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class PDFWrite
{

public static void main(String[] args) throws Exception
       {

PDDocument doc=new PDDocument();

PDPage page=new PDPage();
doc.addPage(page);

int numberOfPages=doc.getNumberOfPages();
System.out.println(numberOfPages);

float version=doc.getVersion();

System.out.println(version);

PDDocumentInformation info=new PDDocumentInformation();

String title="Mutturaj Hulagabal";

info.setTitle(title);

System.out.println(info.getTitle());

String author = "Mutturaj Hulagabal";

info.setAuthor(author);

System.out.println(info.getAuthor());

PDPageContentStream stream=new PDPageContentStream(doc, page);

stream.beginText();

stream.setFont(PDType1Font.TIMES_BOLD, 20);

stream.newLineAtOffset(25,750);

stream.showText("Mutturaj Hulagabal");

stream.endText();

stream.close();

doc.save(filepath);

doc.close();

}

}

No comments:

Post a Comment