下面是编程之家 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
/**
* @author
*
* @param inputStream Input PDF file
* @param outputStream Output PDF file
* @param fromPage start page from input PDF file
* @param toPage end page from input PDF file
*/
public static void splitPDF(InputStream inputStream,OutputStream outputStream,int fromPage,int toPage) {
Document document = new Document();
try {
PdfReader inputPDF = new PdfReader(inputStream);
int totalPages = inputPDF.getNumberOfPages();
//make fromPage equals to toPage if it is greater
if(fromPage > toPage ) {
fromPage = toPage;
}
if(toPage > totalPages) {
toPage = totalPages;
}
// Create a writer for the outputstream
PdfWriter writer = PdfWriter.getInstance(document,outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data
PdfImportedPage page;
while(fromPage <= toPage) {
document.newPage();
page = writer.getImportedPage(inputPDF,fromPage);
cb.addTemplate(page,0);
fromPage++;
}
outputStream.flush();
document.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document.isOpen())
document.close();
try {
if (outputStream != null)
outputStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
MergePDF.splitPDF(new FileInputStream("C:\\input.pdf"),new FileOutputStream("C:\\output1.pdf"),1,12);
MergePDF.splitPDF(new FileInputStream("C:\\input.pdf"),new FileOutputStream("C:\\output2.pdf"),13,20);
} catch (Exception e) {
e.printStackTrace();
}
}
以上是编程之家()为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
相关文章
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250