可以使用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
踩
分享到:
-01-04 15:03
浏览 19169
分类:非技术
评论
4 楼
chenli3238
-01-07
同求啊,,。
3 楼
jlhjlh521
-04-17
exe 写道
xbcoil 写道
如果pdf文件很大的话,会报内存溢出错误.不知道您有什么解决方法不?
同问
2 楼
exe
-04-19
xbcoil 写道
如果pdf文件很大的话,会报内存溢出错误.不知道您有什么解决方法不?
同问
1 楼
xbcoil
-04-06
如果pdf文件很大的话,会报内存溢出错误.不知道您有什么解决方法不?