使用PDFBOX为PDF添加注释,然后在第2行覆盖另一个PDF

时间:2017-11-09 12:59:39

标签: java apache pdf pdfbox

我必须在PDFBOX的现有PDF上创建一行注释。它必须位于现有PDF的右上角。大多数情况下,有一些空间可以在现有PDF中附加该行。有时在右上角有一个标记,禁止我在那里放置注释。我能做的最好的事情就是在顶部写一个带有注释的新页面,然后将两个PDF合并在一起。注释代码:

import java.io.IOException;  
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage; 
import org.apache.pdfbox.pdmodel.PDPageContentStream; 
import org.apache.pdfbox.pdmodel.font.PDType1Font;  
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
public class Document_Creation {

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

  //Creating PDF document object 
  PDDocument document = new PDDocument();
  PDPage page = new PDPage();
  document.addPage(page);
  //Retrieving the pages of the document 
  PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true,true);

  //Begin the Content stream 
  contentStream.beginText(); 

  //Setting the font to the Content stream  
  contentStream.setFont(PDType1Font.TIMES_BOLD, 8);

  //Setting the position for the line 
  contentStream.newLineAtOffset(100, 775);

  String text = args[1];

  //Adding text in the form of string 
  contentStream.showText(text);      

  //Ending the content stream
  contentStream.endText();

  System.out.println("Content added best friend");

  //Closing the content stream
  contentStream.close();

  //Saving the document
  document.save(args[0]);

  //Closing the document  
  document.close();

0 个答案:

没有答案