написать на существующую форму-PDF с pdfbox - PullRequest
0 голосов
/ 29 мая 2018

Я относительно новичок в Java и хочу заменить существующий Javascript на базе iText на pdfbox.(Java 2.0) У меня есть PDF-форма (но на этом листе нет записей Acroform), и я хочу заполнить ее информацией (имя, дата рождения и т. Д.).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 голосов
/ 29 мая 2018

Источник работает.Может быть, ваш content.newLineAtOffset(100, 400); слишком велик - вне пределов - для вашей маленькой карточки.

Кстати, вам нужно setLeading(float), чтобы использовать newLine() значащим образом.

...