因为使用免费的jar切割之后会有水印
public static void main(String[] args) {
String pdfPath="D:/xx/xxx.pdf";//路径
/**
* com.spire.pdf
* 使用免费的切割文件会存在水印
*/
//创建一个PdfDocument对象
PdfDocument doc = new PdfDocument();
//加载PDF文件
doc.loadFromFile(pdfPath);
//将 PDF 拆分为单页 PDF
doc.split( "output-{0}.pdf", 1);
}
所以就用了下面的方法
public void splitPdf(String pdfPath) throws IOException, DocumentException {
// 根据页码拆分pdf,有多少页就拆成多少张pdf
PdfReader reader = new PdfReader(pdfPath);
int n = reader.getNumberOfPages();
for (int i = 1; i <= n; i++) {
String outFile = pdfPath.substring(0, pdfPath.indexOf(".pdf")) + "-" + String.format("%d", i ) + ".pdf";
Document document = new Document(reader.getPageSizeWithRotation(1));
PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
document.open();
PdfImportedPage page = writer.getImportedPage(reader, i);
writer.addPage(page);
document.close();
writer.close();
}
/**
* 如果要删除原文件
* 需要关闭
*/
reader.close();
}