PdfBox 2.0.0 write text at given postion in a page

て烟熏妆下的殇ゞ 提交于 2019-12-06 13:16:14

问题


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


回答1:


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!



回答2:


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"));


来源:https://stackoverflow.com/questions/36449776/pdfbox-2-0-0-write-text-at-given-postion-in-a-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!