Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PdfBox 2.0.0 write text at given postion in a page

Tags:

java

pdfbox

I have just passed from PdfBox 1.8 to 2.0.0 and there are quite significant differences. Before to write a text on an existing pdf page I used drawString. In 2.0.0 draw string is deprecated but showText does not work in a block text.

My code in 1.8:

 contentStream.beginText()
 contentStream.moveTextPositionByAmount(250, 665)
 contentStream.drawString("1  2 3 4 5 6    7  8  9   1 0")
 contentStream.endText()

My code in 2.0

  PDDocument newPdf=null
  newPdf=PDDocument.load(sourcePdfFile)
  PDPage firstPage=newPdf.getPage(0)
  PDPageContentStream contentStream = new PDPageContentStream(newPdf, firstPage, PDPageContentStream.AppendMode.APPEND,true,true)
  contentStream.setFont(pdfFont, fontSize)
  contentStream.beginText()
  contentStream.lineTo(200,685)
  contentStream.showText("John")
  contentStream.endText()

But it does not working...

Anyone has any idea about how can I write text as in 1.8

like image 400
Amrida D Avatar asked Apr 06 '16 11:04

Amrida D


People also ask

Is PDFBox free for commercial use?

Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative ...


2 Answers

LineTo is to draw a line. What you want is newLineAtOffset (the deprecation notice of moveTextPositionByAmount says so), so your code is like this:

  PDDocument newPdf = PDDocument.load(sourcePdfFile);
  PDPage firstPage=newPdf.getPage(0);
  PDFont pdfFont= PDType1Font.HELVETICA_BOLD;
  int fontSize = 14;
  PDPageContentStream contentStream = new PDPageContentStream(newPdf, firstPage, PDPageContentStream.AppendMode.APPEND,true,true);
  contentStream.setFont(pdfFont, fontSize);
  contentStream.beginText();
  contentStream.newLineAtOffset(200,685);
  contentStream.showText("John");
  contentStream.endText();
  contentStream.close(); // don't forget that one!
like image 121
Tilman Hausherr Avatar answered Oct 03 '22 22:10

Tilman Hausherr


If you are looking for code to add multiple line statement in PDF at desired position
like this

this is line first
this is line second
this is line third

then you need use

    //to load PDF where you 
    PDDocument pdDocument = PDDocument.load(new File(resourceBundle.getString("F:\PDF\loki.pdf")));
    //get the page where you want to write code,for first page you need to use 0
    PDPage firstPage = pdDocument.getPage(0);

    //you can load new font if required, by using `ttf` file for that font
    PDFont pdfFont = PDType0Font.load(pdDocument, 
new File(resourceBundle.getString("F:\PDF\data\verdana.ttf")));

    int fontSize = 9;
    //PDPageContentStream.AppendMode.APPEND this part is must if you want just add new data in exsitnig one
    PDPageContentStream contentStream = new PDPageContentStream(pdDocument, pdDocument.getPage(0),
            PDPageContentStream.AppendMode.APPEND, true, true);

    contentStream.setFont(PDType0Font.load(pdDocument, new File(resourceBundle.getString("pdfFont"))), 9);

    //for first Line
    contentStream.beginText();
    //For adjusting location of text on page you need to adjust this two values
    contentStream.newLineAtOffset(200,685);
    contentStream.showText("this is line first");
    contentStream.endText();

    //for second line
    contentStream.beginText();
    contentStream.newLineAtOffset(200,685);
    contentStream.showText("this is line second");
    contentStream.endText();

    //for  third line
    contentStream.beginText();
    contentStream.newLineAtOffset(200,685);
    contentStream.showText("this is line third");
    contentStream.endText();
    //and so on.

    //at last you need to close the document to save data
    contentStream.close(); 
   //this is for saving your PDF you can save with new name 
   //or you can replace existing one by giving same name 
    pdDocument.save(resourceBundle.getString("F:\PDF\lokiTheKing.pdf"));
like image 44
v8-E Avatar answered Oct 03 '22 20:10

v8-E