使用pdfbox

时间:2018-05-29 10:35:18

标签: java load pdfbox fill

我是Java的新手,我想用pdfbox替换现有的基于iText的Javascript。 (Java 2.0) 我有一个pdf-Formsheet(但这张表没有Acroform条目),我想用信息填充它(Name,Birthdate等)。 pdf采用矩形特殊尺寸(如联系卡)。

到目前为止我的代码:

  File file = new File("ToBeFilled.pdf"); 

  PDDocument document = PDDocument.load(file); 

  System.out.println("PDF loaded"); 

 //Retrieving the page
  PDPage page = (PDPage)document.getPages().get( 0 );

  PDFont font = PDType1Font.HELVETICA_BOLD;
  PDPageContentStream content = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);

  content.beginText();      
  //Setting the font to the Content stream  
  content.setFont(font, 30);  

  //Setting the position for the line (float x, float y), (0,0) = lower left corner
  content.newLineAtOffset(100, 400);
  String text = "This is the sample document and we are adding content to it.";
  String text1 = "This is an example of adding text to a page in the pdf document. we can add as many lines";
  String text2 = "as we want like this using the ShowText()  method of the ContentStream class";
  //Adding text in the form of string 
  content.showText(text); 
  //Adding text in the form of string
  content.newLine();
  content.showText(text1);
  content.newLine();
  content.showText(text2);

  //Ending the content stream
  content.endText();
  System.out.println("Text added"); 

  content.close();
  //Saving the document 
  document.save("newPrint.pdf");

  //Closing the document  
  document.close(); 

文字没有显示。我在这里错过了什么?我想用正确的文字位置,我可以简单地写在pdf上?

1 个答案:

答案 0 :(得分:0)

来源正在运作。 也许你的tcpdump -v -s0 udp 太大了 - 超出范围 - 为你的小卡片。

顺便说一下,您必须content.newLineAtOffset(100, 400);使用setLeading(float)含义。

相关问题