java 合并pdf_用iText分割和合并pdf文件

可以使用iText将多个pdf文件合并成一个pdf文件,也可以使用iText将一个pdf文件分割成多个pdf文件。下面的代码是一个简单的例子。

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.pdf.PdfCopy;

import com.lowagie.text.pdf.PdfImportedPage;

import com.lowagie.text.pdf.PdfReader;

public class pdfOperate

{

private static final int N = 3;

public static void main(String[] args)

{

String[] files = {"C:\\a.pdf", "C:\\b.pdf"};

String savepath = "C:\\temp.pdf";

mergePdfFiles(files, savepath);

partitionPdfFile("C:\\a.pdf");

}

public static void mergePdfFiles(String[] files, String savepath)

{

try

{

Document document = new Document(new PdfReader(files[0]).getPageSize(1));

PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath));

document.open();

for(int i=0; i

{

PdfReader reader = new PdfReader(files[i]);

int n = reader.getNumberOfPages();

for(int j=1; j<=n; j++)

{

document.newPage();

PdfImportedPage page = copy.getImportedPage(reader, j);

copy.addPage(page);

}

}

document.close();

} catch (IOException e) {

e.printStackTrace();

} catch(DocumentException e) {

e.printStackTrace();

}

}

public static void partitionPdfFile(String filepath)

{

Document document = null;

PdfCopy copy = null;

try

{

PdfReader reader = new PdfReader(filepath);

int n = reader.getNumberOfPages();

if(n < N)

{

System.out.println("The document does not have " + N + " pages to partition !");

return;

}

int size = n/N;

String staticpath = filepath.substring(0, filepath.lastIndexOf("\\")+1);

String savepath = null;

ArrayList savepaths = new ArrayList();

for(int i=1; i<=N; i++)

{

if(i < 10)

{

savepath = filepath.substring(filepath.lastIndexOf("\\")+1, filepath.length()-4);

savepath = staticpath + savepath + "0" + i + ".pdf";

savepaths.add(savepath);

}

else

{

savepath = filepath.substring(filepath.lastIndexOf("\\")+1, filepath.length()-4);

savepath = staticpath + savepath + i + ".pdf";

savepaths.add(savepath);

}

}

for(int i=0; i

{

document = new Document(reader.getPageSize(1));

copy = new PdfCopy(document, new FileOutputStream(savepaths.get(i)));

document.open();

for(int j=size*i+1; j<=size*(i+1); j++)

{

document.newPage();

PdfImportedPage page = copy.getImportedPage(reader, j);

copy.addPage(page);

}

document.close();

}

document = new Document(reader.getPageSize(1));

copy = new PdfCopy(document, new FileOutputStream(savepaths.get(N-1)));

document.open();

for(int j=size*(N-1)+1; j<=n; j++)

{

document.newPage();

PdfImportedPage page = copy.getImportedPage(reader, j);

copy.addPage(page);

}

document.close();

} catch (IOException e) {

e.printStackTrace();

} catch(DocumentException e) {

e.printStackTrace();

}

}

}

当然,可以做的复杂一些:可以做成有界面形式的,可以选择对哪些(个)文件进行操作,可以选择保存的文件名等等;也可以用命令行形式,在参数中选择是进行合并操作还是分割操作,并输入要操作的pdf文件名,已经保存的文件名,分割的数量等。还可以做的更复杂,例如,可以选择合并这些文件的哪些页,分割某个文件的哪些页等等。这只需要做一些相应的修改就可以了。

附件为可能会用到的jar包,注意使用iText的不同版本时的问题。

16

1

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2009-01-04 15:03

浏览 19169

分类:非技术

评论

4 楼

chenli3238

2014-01-07

同求啊,,。

3 楼

jlhjlh521

2012-04-17

exe 写道

xbcoil 写道

如果pdf文件很大的话,会报内存溢出错误.不知道您有什么解决方法不?

同问

2 楼

exe

2011-04-19

xbcoil 写道

如果pdf文件很大的话,会报内存溢出错误.不知道您有什么解决方法不?

同问

1 楼

xbcoil

2011-04-06

如果pdf文件很大的话,会报内存溢出错误.不知道您有什么解决方法不?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值