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博客

相关推荐
又是被bug折磨的一天26 分钟前
发票形式是eml批量下载发票pdf
pdf
hmz8562 小时前
亲测有效,完全免费PDF转换工具,支持PDF压缩、PDF转图片、PDF删除和排序 、PDF转Word、PDF转文本、图片转PDF【附安装教程】
pdf
至善迎风3 小时前
用 Codex / Claude Code Skill 自动完成「文献 PDF → 文献汇报 PPT」:从论文精读到 10–12 页学术汇报
人工智能·pdf·powerpoint
俊哥工具4 小时前
不用打开文件也能预览!支持压缩包、PDF、音视频
python·智能手机·django·pdf·计算机外设·virtualenv
雨田哥1 天前
Qt Ironclad Reader (授权/加密/OFD签章/OFD验章/PDF/导出)
pdf·ofd·签章·验章·qt ofd·qt pdf·授权加密
狠学嵌入式1 天前
耗时一个月整理了3款实用免费PDF处理网站
pdf·pdf转word·pdf添加水印·pdf处理·免费工具·免费网站·清页pdf
2501_930707783 天前
使用C#代码替换 PDF 文档中的文本
pdf
周末也要写八哥3 天前
Visual C++6.0下载安装流程及PDF学习手册资源
c++·学习·pdf
优化控制仿真模型3 天前
2026初中英语考纲词汇表(1600词)PDF电子版
经验分享·pdf
2401_876964133 天前
27考研优路|肖睿|唐辛|师大集训营|大牙|B站橙啦101公共课PDF
考研·pdf