Java 通过页码范围提取新的文档(docx、pdf)

客户有一个新的需求。控制用户下载文档的页数。比如。我设置只开放10页。那就要我们去做截取文档。pdf自带有实现的方式。docx暂时没找到开源的。我这边处理的docx。是通过转换成pdf。通过截取pdf后再转成docx去实现的

工具类如下:

复制代码
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfSmartCopy;

/**
 * PDF工具类
 */
public class PdfUtils {
    /**
     * 抽取PDF文件
     *
     * @param sourceFile        源PDF文件路径
     * @param targetFile        目标PDF文件路径
     * @param extractedPageNums 需要抽取的页码)
     */
    public static void extract(String sourceFile, String targetFile, List<Integer> extractedPageNums) {
        Objects.requireNonNull(sourceFile);
        Objects.requireNonNull(targetFile);
        PdfReader reader = null;
        Document document = null;
        FileOutputStream outputStream = null;
        PdfCopy pdfCopy = null;
        try {
            // 读取源文件
            reader = new PdfReader(sourceFile);
            // 创建新的文档
            document = new Document();
            // 创建目标PDF文件
            outputStream = new FileOutputStream(targetFile);
            pdfCopy = new PdfSmartCopy(document, outputStream);
            // 获取源文件的页数
            int pages = reader.getNumberOfPages();
            document.open();
            // 注意此处的页码是从1开始
            for (int page = 1; page <= pages; page++) {
                // 如果是指定的页码,则进行复制
                if (extractedPageNums.contains(page)) {
                    pdfCopy.addPage(pdfCopy.getImportedPage(reader, page));
                }
            }
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (document != null) {
                document.close();
            }
            if (outputStream != null) {
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(pdfCopy != null){
                pdfCopy.flush();
                pdfCopy.close();
            }
        }
    }
}

如果想实现docx和pdf互转格式不变。请参考另一篇文章

java 实现docx和pdf互转并保留格式_王威振的csdn的博客-CSDN博客

相关推荐
阿幸软件杂货间1 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
reembarkation1 天前
使用pdfjs-dist 预览pdf,并添加文本层的实现
前端·javascript·pdf
reembarkation1 天前
vue-pdf 实现blob数据的预览
javascript·vue.js·pdf
Light602 天前
领码方案|Linux 下 PLT → PDF 转换服务超级完整版:异步、权限、进度(一气呵成)
linux·spring boot·pdf·gpcl6/ghostpcl·s3/oss·权限与审计·异步与进度
伟贤AI之路2 天前
【分享】中小学教材课本 PDF 资源获取指南
人工智能·pdf
东风西巷2 天前
PDFgear:免费全能的PDF处理工具
前端·pdf·软件需求
Sunny_yiyi3 天前
Java根据模版导出PDF文件
java·开发语言·pdf
小*-^-*九3 天前
php 使用html 生成pdf word wkhtmltopdf 系列2
pdf·html·php
千册3 天前
pyside6 的pdf显示测试 -- 01
开发语言·python·pdf
qq_172805594 天前
Go 语言 PDF 生成库综合比较与实践指南
开发语言·golang·pdf