通过 Aspose.PDF 拆分 PDF 以获取 Java

在 Aspose.PDF for Java 工具的帮助下拆分文件。

使用 Aspose.PDF 拆分文件 for Java

只需要大文件的一部分吗?要从文档中分离一个或多个部分,必须拆分原始文件的页面。为了拆分文件,我们将使用 Aspose.PDF for Java API,这是一款适用于 Java 平台的功能丰富、功能强大且易于使用的转换 API。你可以直接从 Maven 下载其最新版本,然后通过在pom.xml中添加以下配置,将其安装在基于Maven的项目中。我们的软件工具还为开发人员提供了将 Word、PDF、HTML、TXT 和 DOCX 文档拆分成几个部分。欲了解更多详情,请学习 文档页面

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

通过 Java 拆分文档


您需要 [Aspose.PDF for Java](https://releases.aspose.com/pdf/java)來嘗試環境中的代碼。

  1. 使用 Document 實例載入 PDF。
  2. 建立一個新的 Document 類別物件來拆分 PDF 頁面。
  3. 將目前頁面新增至文件。
  4. 將目前頁面儲存為單獨的 PDF

如何使用 Java 拆分 PDF

使用 Aspose.PDF for Java 库,你可以拆分大型 PDF 文档。分割 PDF 文档是处理 PDF 文档时的常见用例。它通过将大型文档分解为较小的文件以通过电子邮件发送,从而帮助减小PDF文件的大小。

拆分 PDF-Java

此示例代码显示如何拆分 PDF 文件-Java

    //Read the source PDF file
    doc = new com.aspose.pdf.Document("1.pdf");
    //Instantiate PdfFileEditor object
    pdfEditor = new com.aspose.pdf.facades.PdfFileEditor();

    //Split the PDF file into two halves
    pdfEditor.extract(pathSource, 1, doc.getPages().size() / 2, "pdf_half.pdf");

如何使用 Java 拆分 HTML 文件

HTML 文档可能很大,因为它们可以包含文本、图像、图表等。您可能需要根据不同的要求或用例分开 HTML 文件。Aspose.PDF for Java 库将帮助你完成这项任务。

拆分 HTML-Java

此示例代码显示了如何拆分 HTML 文件-Java

    //Read the source HTML file
    doc = new com.aspose.pdf.Document(pathSource, new com.aspose.pdf.HtmlLoadOptions());

    //save input html to pdf to file
    doc.save("test.pdf", com.aspose.pdf.SaveFormat.Pdf);

    //Instantiate PdfFileEditor object
    pdfEditor = new com.aspose.pdf.facades.PdfFileEditor();

    //slit first page
    pdfEditor.splitFromFirst("test.pdf", 1, "test.pdf");

    //Convert first pdf page to html
    outputDoc = new com.aspose.pdf.Document("test.pdf");
    outputDoc.save("first_page.html", com.aspose.pdf.SaveFormat.Html);

如何使用 Java 拆分 TXT 文件

Aspose.PDF for Java 库允许 Java 开发人员将 TXT 文件拆分为不同的部分。将 TXT 文档拆分成单独文件的功能将帮助您同时为多个用户处理大型文档的不同部分。拆分 TXT 文档将加快和简化您的工作。使用以下代码片段提取 TXT 文件的页面。

拆分文本-Java

此示例代码显示如何拆分 TXT 文件-Java

    //Read the source TXT file to Aspose Document
    doc = new com.aspose.pdf.Document(pathSource, new com.aspose.pdf.TxtLoadOptions());

    //Instantiate PdfFileEditor object
    pdfEditor = new com.aspose.pdf.facades.PdfFileEditor();


    //save input text to pdf to file
    doc.save("test.pdf", com.aspose.pdf.SaveFormat.Pdf);

    //split pdf to pages
    ByteArrayInputStream[] pages = pdfEditor.splitToPages("test.pdf");
    int index = 1;

    //5. save each page to text file
    //extract text from page
    for(var ms : pages) {
        page = new com.aspose.pdf.Document(ms);
        textAbsorber = new com.aspose.pdf.TextAbsorber();
        page.getPages().accept(textAbsorber);
        String extractedText = textAbsorber.getText();
        Files.writeString(Path.of("text_"+ Integer.toString(index)+".txt"), extractedText);
        index++;
    }