当前位置: 首页 > 知识库问答 >
问题:

如何在java pdfbox中按结果拆分pdf文件

丌官绍元
2023-03-14
import java.io.*;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.util.*;
import java.util.regex.*;

public class PDFtest1 {
public static void main(String[] args){
PDDocument pd;
try {

     File input = new File("G:\\Sales.pdf");

     // StringBuilder to store the extracted text
     StringBuilder sb = new StringBuilder();
     pd = PDDocument.load(input);
     PDFTextStripper stripper = new PDFTextStripper();

     // Add text to the StringBuilder from the PDF
     sb.append(stripper.getText(pd));


     Pattern p = Pattern.compile("Invoice No.\\s\\w\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d");

     // Matcher refers to the actual text where the pattern will be found
     Matcher m = p.matcher(sb);

     while (m.find()){
         // group() method refers to the next number that follows the pattern we have specified.
         System.out.println(m.group());
     }

     if (pd != null) {
         pd.close();
     }
   } catch (Exception e){
     e.printStackTrace();
    }
 }
 }  
run:
Invoice No. D0000003010
Invoice No. D0000003011
Invoice No. D0000003011
Invoice No. D0000003011
Invoice No. D0000003011
Invoice No. D0000003012
Invoice No. D0000003012
Invoice No. D0000003012
Invoice No. D0000003013
Invoice No. D0000003013
Invoice No. D0000003014
Invoice No. D0000003014
Invoice No. D0000003015
Invoice No. D0000003016

我需要根据发票编号拆分pdf。例如发票号D0000003011,所有pdf页面应合并为单个pdf,依此类推。我怎样才能做到。..

共有1个答案

薛博赡
2023-03-14
public static void main(String[] args) throws IOException, COSVisitorException
{
    File input = new File("G:\\Sales.pdf");

    PDDocument outputDocument = null;
    PDDocument inputDocument = PDDocument.loadNonSeq(input, null);
    PDFTextStripper stripper = new PDFTextStripper();
    String currentNo = null;
    for (int page = 1; page <= inputDocument.getNumberOfPages(); ++page)
    {
        stripper.setStartPage(page);
        stripper.setEndPage(page);
        String text = stripper.getText(inputDocument);
        Pattern p = Pattern.compile("Invoice No.(\\s\\w\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d)");

        // Matcher refers to the actual text where the pattern will be found
        Matcher m = p.matcher(text);
        String no = null;
        if (m.find())
        {
            no = m.group(1);
        }
        System.out.println("page: " + page + ", value: " + no);

        PDPage pdPage = (PDPage) inputDocument.getDocumentCatalog().getAllPages().get(page - 1);

        if (no != null && !no.equals(currentNo))
        {
            saveCloseCurrent(currentNo, outputDocument);
            // create new document
            outputDocument = new PDDocument();
            currentNo = no;
        }
        if (no == null && currentNo == null)
        {
            System.out.println ("header page ??? " + page + " skipped");
            continue;
        }
        // append page to current document
        outputDocument.importPage(pdPage);
    }
    saveCloseCurrent(currentNo, outputDocument);
    inputDocument.close();
}

private static void saveCloseCurrent(String currentNo, PDDocument outputDocument)
        throws IOException, COSVisitorException
{
    // save to new output file
    if (currentNo != null)
    {
        // save document into file
        File f = new File(currentNo + ".pdf");
        if (f.exists())
        {
            System.err.println("File " + f + " exists?!");
            System.exit(-1);
        }
        outputDocument.save(f);
        outputDocument.close();
    }
}

当心:

  • 尚未使用您的文件进行测试(因为我没有它);
  • 代码假设相同的发票号码总是在一起;
  • 正则表达式略有更改;
  • 确保第一个和最后一个PDF文件正确,并随机检查几个文件,如果可用,则使用不同的查看器;
  • 验证文件总数是否与预期相同;
  • 所有文件的总大小将大于源文件,这是由于字体资源的原因;
  • 请使用1.8.10版本。不要同时使用PDFBox 0.7.3.jar!
  • 错误处理是非常基本的,您需要更改它;

2015年8月19日更新:

    null
 类似资料:
  • 我需要把一个文件分成几个小文件。例如,如果文档有7页,我需要生成7个PDF。 在iTextSharp中,我使用了以下代码,运行得非常好。然而,在iText 7中,不可能以同样的方式进行。 第一个问题 我发现有一个,它可以将我的pdf文件拆分成小的pdf文件。然而,即使是我的测试pdf也有7页,甚至返回数字7,被拆分的文档的数量只是一个。 在这个链接文档中,以某种方式展示了如何拆分文档。然而,我不知

  • 在上一章中,我们已经了解了如何将JavaScript添加到PDF文档中。 现在让我们学习如何将给定的PDF文档拆分成多个文档。 拆分PDF文档中的页面 您可以使用名为Splitter的类将给定的PDF文档拆分为多个PDF文档。 此类用于将给定的PDF文档拆分为多个其他文档。 以下是拆分现有PDF文档的步骤 第1步:加载现有PDF文档 使用PDDocument类的静态方法load()加载现有PDF文

  • 对于以下代码, > 那么为什么“b”,“and”,“1”后面没有空字符串呢? 输出为: 8 "b" " " ": and: f" " "" "1" "

  • 问题内容: 我正在使用Apache PDFBox处理Java应用程序中的PDF文件。我想在每个页面上分割一个PDF文档。 是否有可能做到这一点Apache PDFBox?如果是这样,怎么办? 问题答案: 可以使用来实现。 这是一个示例代码,它将在每个页面上拆分文档: 您可以使用来控制每个拆分的PDF的页数。

  • 这是代码: 我还尝试了以下存储新文件的代码: 但是这段代码创建的文件缺少一些内容,并且大小与原始文件相同。 THX!