资源简介
把大的PDF文件拆分成指定大小文件,但是因为每页的文件大小不一定,就不能通过固定页数来拆分文件,这样子的话就需要我们通过计算来将文件拆分这指定大小的文件
![](http://www.nz998.com/pic/71341.jpg)
代码片段和文件信息
package com.mysoft.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
/**
* 文件拆分
* @author 86185
*
*/
public class SplitFileUtil {
/**
* pdf文件拆分
* @param path 文档路径
* @param pdfFileName 文件名
* @param filePageSize·每个文件的最大页数
*/
public static void splitPdfFile(String pathString pdfFileNameint filePageSize) {
// 文档路径
// String path = “C:\\Users\\86185\\Desktop\\readFile\\“;
// 待拆分文件名
// String pdfFileName = “684331701425410048.pdf“;
// 每个文件最大页数
// int filePageSize = 25;
// 待拆分文件的总页数
int totalPage;
// 拆分后的文件数量
int splitFileNum;
int pageIndex = 1;
PdfReader reader = null;
try {
String orignName = pdfFileName.split(“\\.“)[0];
reader = new PdfReader(path + pdfFileName);
totalPage = reader.getNumberOfPages();
splitFileNum = totalPage % filePageSize == 0 ? totalPage / filePageSize : totalPage / filePageSize + 1;
for (int i = 0; i < splitFileNum; i++) {
String newFileName = path + orignName + “_“ + (i + 1) + “.pdf“;
// 新建一个PDF文件
Document document = null;
PdfWriter writer = null;
try {
document = new Document();
writer = PdfWriter.getInstance(document new FileOutputStream(newFileName));
document.open();
PdfContentByte pdfContentByte = writer.getDirectContent();
for (int j = 0; j < filePageSize; j++) {
document.newPage();
pdfContentByte.addTemplate(writer.getImportedPage(reader pageIndex) 0 0);
pageIndex ++;
if (pageIndex > totalPage)
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
//这个地方要特别注意资源关闭的顺序
if (document != null)
document.close();
if (writer != null)
writer.close();
}
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if(reader!=null) reader.close();
}
}
/**
* pdf文件拆分
* @param file 拆分的文件
* @param filePageSize·每个文件的最大页数
* @return
*/
public static List splitPdfFile(File fileint filePageSize) {
List list = new ArrayList();//拆分的文件
String absolutePath = file.getAbsolutePath();//文档绝对路径
String pdfFileName = file.getName();// 待拆分文件名
String path = absolutePath.substring(0 absolutePath.lastIndexOf(pdfFileName));// 文档路径
// 待拆分文件的总页数
int totalPage;
// 拆分后的文件数量
int splitFileNum;
int pageIndex = 1;
PdfReader reader = null;
try {
String orignName = pdfFileName.split(“\\.“)[0];
reader = new PdfReader(path + pdfFileName);
totalPage = reader.getNumberOfPages();
splitFile
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 9360 2020-05-21 10:33 使用itextpdf将PDF大文件拆分成若干份指定大小文件\SplitFileUtil.java
文件 2164148 2020-03-14 22:44 使用itextpdf将PDF大文件拆分成若干份指定大小文件\itextpdf-5.5.5.jar
目录 0 2020-08-14 09:03 使用itextpdf将PDF大文件拆分成若干份指定大小文件\
- 上一篇:中国最新详细地图shp文件
- 下一篇:jiajiahuifu_BAH.rar
相关资源
- SpringBoot+H2+mybatis-plus59130
- 登录注册界面.zip48872
- HAP_Advanced_PDF_Password_Recovery 5.05
- Rtx51_tiny_RTOS中文版.pdf
- porting.RTEMS移植指南.双语.V20131224.pdf
- 数字华容道
- SSM+Shiro+redis实现单点登陆
- jstl-api-1.2和jstl-impl-1.2
- 基于MVC模式的会员管理系统
- 国内一家大型软件公司内部的正规软
- 仿windows记事本
- GUI银行管理系统
- 超市收银系统eclipse access大学课程设计
- 模拟ATM柜员机系统--连接数据库
- 硬件测试面试常见题PDF
- A*算法的2D演示(带源码)
- 代码审查表和代码审查实例
- 仿126 网易 163 邮箱 界面
- Tomcat6.x
- 简单的行编辑器
- 扫雷(MVC架构)
- 302 Found
- 图文手把手教你一步步用VC 2010编写通
- 图文手把手教你一步步用VC 2010编写通
- window ping命令加时间并记录日志
- Vulkan Cookbook 无水印转化版pdf
- PDF格式的白话孙子兵法
- APUE第三版pdf
- SDINBDG4-64GB_datasheet generic final v1.pdf
- pdf.jsamp;pdf;.worker.js
评论
共有 条评论