0
点赞
收藏
分享

微信扫一扫

java 用PDFBox 删除 PDF文件中的某一页

小布_cvg 2023-08-04 阅读 53

依赖:


  1. org.apache.pdfbox
  2. pdfbox-app
  3. 1.8.10

java 用PDFBox 删除 PDF文件中的某一页,前n页,后n页,效率低,不推荐使用

  1. package com.everjiankang;

  2. import java.io.File;

  3. import org.apache.pdfbox.pdmodel.PDDocument;

  4. /**运行效率很慢,因为每次删除一页就读取和保存一次文件,初始文件名格式:xxxx0.pdf*/
  5. public class Test {
  6. static String name_pre = "C:\\log\\jvm"; //文件名前缀
  7. static String name_after = ".pdf";//文件名后缀
  8. public static void main(String[] args) {
  9. //1.刪除前n頁
  10. // cutPdfPreNPage(2);
  11. //2.刪除后n頁
  12. cutPdfAfterNPage(5);
  13. //3.刪除第n頁
  14. cutPdf(name_pre + 0 + name_after,name_pre + (0+1) + name_after,7);//删除第n页
  15. }

  16. /**
  17. * 删除前n页
  18. * @param n
  19. */
  20. public static void cutPdfPreNPage(int n) {
  21. for(int i = 0; i < n; i++)
  22. cutPdf(name_pre + i + name_after,name_pre + (i+1) + name_after,0);
  23. }

  24. /**
  25. * 删除后n页
  26. * @param n
  27. */
  28. public static void cutPdfAfterNPage(int n) {
  29. for(int i = 0; i < n; i++)
  30. cutPdf(name_pre + i + name_after,name_pre + (i+1) + name_after,1);
  31. }

  32. /**
  33. *
  34. * @param pdfPath 旧路径
  35. * @param newPdfPath 新路径
  36. * @param flag 0:第一页;1:最后一页 ;else : 要删除的页码
  37. */
  38. public static void cutPdf(String pdfPath,String newPdfPath, int flag)
  39. {
  40. File file = new File(pdfPath);
  41. PDDocument document = new PDDocument();
  42. try{
  43. document = PDDocument.load(file);
  44. }catch(Exception e){
  45. e.printStackTrace();
  46. }
  47. int noOfPages = document.getNumberOfPages();
  48. System.out.println(noOfPages);
  49. if(flag == 0)
  50. document.removePage(0);
  51. else if(flag == 1) {
  52. document.removePage(noOfPages-1);
  53. } else {
  54. document.removePage(flag-1);
  55. }
  56. try{
  57. document.save(newPdfPath);
  58. document.close();
  59. }catch(Exception e){
  60. e.printStackTrace();
  61. }
  62. System.out.println("已经转完了哦");

  63. }
  64. }

抽取任意范围的PDF页作为新的PDF. 效率高

依赖


  1. com.itextpdf
  2. itextpdf
  3. 5.5.13

代码

  1. /**
  2. * 截取pdfFile的第from页至第end页,组成一个新的文件名
  3. * @param pdfFile 需要分割的PDF
  4. * @param savepath 新PDF
  5. * @param from 起始页
  6. * @param end 结束页
  7. */
  8. public static void splitPDFFile(String respdfFile,
  9. String savepath, int from, int end) {
  10. Document document = null;
  11. PdfCopy copy = null;
  12. try {
  13. PdfReader reader = new PdfReader(respdfFile);
  14. int n = reader.getNumberOfPages();
  15. if(end==0){
  16. end = n;
  17. }
  18. ArrayList<String> savepaths = new ArrayList<String>();
  19. String staticpath = respdfFile.substring(0, respdfFile.lastIndexOf("\\")+1);
  20. //String savepath = staticpath+ newFile;
  21. savepaths.add(savepath);
  22. document = new Document(reader.getPageSize(1));
  23. copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0)));
  24. document.open();
  25. for(int j=from; j<=end; j++) {
  26. document.newPage();
  27. PdfImportedPage page = copy.getImportedPage(reader, j);
  28. copy.addPage(page);
  29. }
  30. document.close();

  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. } catch(DocumentException e) {
  34. e.printStackTrace();
  35. }
  36. }
举报

相关推荐

0 条评论