Commit 828ac26d authored by luzhuang's avatar luzhuang

cutPdf工具修改

parent 1147eebc
......@@ -5,60 +5,83 @@ import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by yangzhaojun on 2018/2/6.
*/
@Component
@Slf4j
public class CutPdfUtil {
public static void main(String[] args) throws Exception{
public static void main(String[] args) {
try {
List<String> pdfList = toCutPdf("F:\\桌面整理\\文件归纳\\QQ下载\\xx文字+图片-100页.pdf", 30);
log.debug(pdfList.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 切割pdf 返回list
*
* @param filePath 源文件路径
* @param page 每个pdf页数
* @return
* @throws Exception
*/
public static List<String> toCutPdf(String filePath, int page) throws Exception {
List<String> pdfList = new ArrayList<>();
Date dd = new Date();
long l1 = dd.getTime();
int i = 0;
int j = 1;
boolean finish = false;
String filePath = "E:\\pdfTest\\a9c9c7916e424c3aa44255564c31bbe9.pdf";
PdfReader reader = new PdfReader(filePath);
int pageNum = reader.getNumberOfPages();
do{
String newFile = "E:\\pdfTest\\cutPdf\\a9c9c7916e424c3aa44255564c31bbe9"+j+".pdf";
int end = i+30;
if(end>=pageNum){
do {
String newFilePath = filePath.substring(0, filePath.lastIndexOf(".")) + "-" + j + ".pdf";
int end = i + page;
if (end >= pageNum) {
end = pageNum;
finish = true;
}
partitionPdfFile(reader, newFile, i+1,end);
System.out.println("第"+j+"个pdf转换完成");
i+=30;
partitionPdfFile(reader, newFilePath, i + 1, end);
log.debug("第" + j + "个pdf转换完成");
i += page;
j++;
}while (!finish);
pdfList.add(newFilePath);
} while (!finish);
Date dd2 = new Date();
long l2 = dd2.getTime();
System.out.println("耗时:"+(l2 - l1));
log.debug("耗时:" + (l2 - l1));
return pdfList;
}
/**
* 截取pdfFile的第from页至第end页,组成一个新的文件名
*
* @param reader
* @param newFile
* @param newFilePath
* @param from
* @param end
*/
public static void partitionPdfFile(PdfReader reader,
String newFile, int from, int end) {
String newFilePath, int from, int end) {
Document document = null;
PdfCopy copy = null;
try {
document = new Document(reader.getPageSize(1));
copy = new PdfCopy(document, new FileOutputStream(newFile));
copy = new PdfCopy(document, new FileOutputStream(newFilePath));
document.open();
for(int j=from; j<=end; j++) {
for (int j = from; j <= end; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
......@@ -67,7 +90,7 @@ public class CutPdfUtil {
} catch (IOException e) {
e.printStackTrace();
} catch(DocumentException e) {
} catch (DocumentException e) {
e.printStackTrace();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment