PDFBox学习总结

学习地址https://www.yiibai.com/pdfbox/pdfbox_overview.html

官网下载jarhttps://pdfbox.apache.org/
或使用maven

<build>
      <sourceDirectory>src</sourceDirectory>
      <plugins>
         <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
               <source>1.8</source>
               <target>1.8</target>
            </configuration> 
         </plugin>
      </plugins> 
   </build> 

   <dependencies>  
      <dependency> 
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>pdfbox</artifactId> 
         <version>2.0.1</version> 
      </dependency>   

      <dependency> 
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>fontbox</artifactId> 
         <version>2.0.0</version> 
      </dependency>

      <dependency>  
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>jempbox</artifactId> 
         <version>1.8.11</version> 
      </dependency> 

      <dependency>
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>xmpbox</artifactId> 
         <version>2.0.0</version> 
      </dependency> 

      <dependency> 
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>preflight</artifactId> 
         <version>2.0.0</version> 
      </dependency> 

      <dependency> 
         <groupId>org.apache.pdfbox</groupId> 
         <artifactId>pdfbox-tools</artifactId> 
         <version>2.0.0</version> 
      </dependency>

   </dependencies>

创建空的PDF文档

PDDocument document = new PDDocument();

进行保存

document.save("Path");

关闭文档

document.close();

添加页面到PDF文档

PDPage my_page = new PDPage();
document.addPage(my_page);

加载现有的PDF文档

File file = new File("path of the document") 
PDDocument.load(file);

从现有文档中删除页面,传递页面索引

document.removePage(2); // 删除第三页

获取总页数

document.getNumberOfPages();

文档的属性

编号	属性	描述
1	File	该属性保存文件的名称。
2	Title	使用此属性,可以设置文档的标题。
3	Author	使用此属性,可以设置文档的作者姓名。
4	Subject	使用此属性,可以指定PDF文档的主题。
5	Keywords	使用此属性,列出可以搜索文档的关键字。
6	Created	使用此属性,可以设置为文档修改的日期
7	Application	使用此属性,可以设置文档的应用程序。

设置文档的属性

	PDDocumentInformation pdd = document.getDocumentInformation();
pdd.setAuthor("Yiibai.com");

      // Setting the title of the document
      pdd.setTitle("一个简单的文档标题"); 

      //Setting the creator of the document 
      pdd.setCreator("PDF Examples"); 

      //Setting the subject of the document 
      pdd.setSubject("文档标题"); 

      //Setting the created date of the document 
      Calendar date = new GregorianCalendar();
      date.set(2017, 11, 5); 
      pdd.setCreationDate(date);
      //Setting the modified date of the document 
      date.set(2018, 10, 5); 
      pdd.setModificationDate(date); 

      //Setting keywords for the document 
      pdd.setKeywords("pdfbox, first example, my pdf"); 

将文本添加到现有的PDF文档

PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.beginText(); 
// 其它代码 .....
code to add text content 
contentStream.setFont( font_type, font_size );
// 其它代码 .....
contentStream.newLineAtOffset(25, 700);
contentStream.showText(text);
contentStream.endText();

插入多行文本

contentStream.setLeading(14.5f);
contentStream.newLine(); 

提取文本

	File file = new File("F:\\worksp\\pdfbox\\new-mul-doc.pdf");
      PDDocument document = PDDocument.load(file);

      //Instantiate PDFTextStripper class
      PDFTextStripper pdfStripper = new PDFTextStripper();

      //Retrieving text from PDF document
      String text = pdfStripper.getText(document);
      System.out.println(text);

      //Closing the document
      document.close();

插入图像

File file = new File("F:/worksp/pdfbox/sample.pdf");
      PDDocument doc = PDDocument.load(file);

      //Retrieving the page
      PDPage page = doc.getPage(0);

      //Creating PDImageXObject object
      PDImageXObject pdImage = PDImageXObject.createFromFile("F:/worksp/pdfbox/logo.png",doc);

      //creating the PDPageContentStream object
      PDPageContentStream contents = new PDPageContentStream(doc, page);

      //Drawing the image in the PDF document
      contents.drawImage(pdImage, 70, 250);

      System.out.println("Image inserted");

      //Closing the PDPageContentStream object
      contents.close();        

      //Saving the document
      doc.save("F:/worksp/pdfbox/sample-image.pdf");

      //Closing the document
      doc.close();

其他待整理,包括加密pdf文档,分隔合并pdf文档,提取图像,添加矩形等。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄宝康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值