我想用IText读取一个~5 MB的文件。我的目标是将此PDF中的许多页面叠加到另一个PDF上。
这是我的代码:
try {
AssetManager assetManager = getActivity().getAssets();
InputStream istr = null;
PdfReader reader = null;
istr =(InputStream) assetManager.open("chem.pdf");
reader=new PdfReader(istr);
Document document = new Document();
PdfWriter writer= PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory()+ "/pdfpdfpdfpdfpdf.pdf"));
document.open();
document.add(new Paragraph(" Generated By Harshvardhan Gupta"));
document.newPage();
PdfContentByte canvas = writer.getDirectContent();
PdfImportedPage page;
//Rectangle r=document.getPageSize();
float pos=720;
float CurPsize=0;
float counter=0;
float oldc=0;
//r.getBottom();
for(int m=1;m<35;m++,counter++) {
page = writer.getImportedPage(reader, m);
Rectangle r=reader.getPageSize(m);
CurPsize= (r.getTop()-r.getBottom());
pos=pos-CurPsize;
Rectangle q=reader.getPageSize(m+1);
int mq=(int) (q.getTop()-q.getBottom());
if(pos<mq+counter*25+50){
document.newPage();
pos=720;
counter=0;
}
canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
canvas.addTemplate(page, 1f, 0, 0, 1,100,pos-70-counter*25);
BaseFont bf= BaseFont.createFont();
canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
canvas.beginText();
canvas.setFontAndSize(bf,14);
canvas.setTextMatrix(75, (float) ((float) (pos-70-counter*25)-10+CurPsize));
canvas.setLineWidth(0.5f);
canvas.showText(m+"."+" ");
canvas.endText();
}
document.close();
writer.freeReader(reader);
writer.close();
reader.close();
//spinner.setVisibility(View.INVISIBLE);
Log.d("OK", "done");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//spinner.setVisibility(View.INVISIBLE);
有相当多的东西在进行,但我认为甚至在从资产中读取PDF之前就会抛出OutOfMemoryError。
这是完整的错误日志:
06-21 19:39:12.090: E/AndroidRuntime(6844): FATAL EXCEPTION: main
06-21 19:39:12.090: E/AndroidRuntime(6844): java.lang.OutOfMemoryError
06-21 19:39:12.090: E/AndroidRuntime(6844): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
06-21 19:39:12.090: E/AndroidRuntime(6844): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.PdfReader.FlateDecode(PdfReader.java:2004)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.PdfReader.FlateDecode(PdfReader.java:1859)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.FilterHandlers$Filter_FLATEDECODE.decode(FilterHandlers.java:107)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.PdfReader.decodeBytes(PdfReader.java:2409)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.PdfReader.decodeBytes(PdfReader.java:2357)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.PdfReader.getStreamBytes(PdfReader.java:2422)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.PdfReader.readObjStm(PdfReader.java:1363)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.PdfReader.readDocObj(PdfReader.java:1305)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:649)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:187)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:328)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:349)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.mainpackage.FragmentOne$1.onClick(FragmentOne.java:88)
06-21 19:39:12.090: E/AndroidRuntime(6844): at android.view.View.performClick(View.java:3511)
06-21 19:39:12.090: E/AndroidRuntime(6844): at android.view.View$PerformClick.run(View.java:14105)
06-21 19:39:12.090: E/AndroidRuntime(6844): at android.os.Handler.handleCallback(Handler.java:605)
06-21 19:39:12.090: E/AndroidRuntime(6844): at android.os.Handler.dispatchMessage(Handler.java:92)
06-21 19:39:12.090: E/AndroidRuntime(6844): at android.os.Looper.loop(Looper.java:137)
06-21 19:39:12.090: E/AndroidRuntime(6844): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-21 19:39:12.090: E/AndroidRuntime(6844): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 19:39:12.090: E/AndroidRuntime(6844): at java.lang.reflect.Method.invoke(Method.java:511)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
06-21 19:39:12.090: E/AndroidRuntime(6844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
06-21 19:39:12.090: E/AndroidRuntime(6844): at dalvik.system.NativeStart.main(Native Method)
请注意,我尝试使用较小的文件(200kb)并且它可以正常工作。这是否意味着我无法使用iText在Android上阅读如此大的文件?
答案 0 :(得分:1)
我的pdf使用钢笔工具进行了大量注释。这导致了这个问题。 我删除了笔痕,pdf加载正常。