我有一個〜5 MB的文件,我想用IText來閱讀。我的目標是將此PDF中的許多頁面疊加到另一PDF上。閱讀PDF時出現OutOfMemoryError(Android)
這是我的代碼:
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上閱讀這樣一個大文件?
你使用的是什麼版本?你能分享你的PDF嗎? –