iTextG导致应用程序崩溃(安卓)应用程序、iTextG

2023-09-03 23:09:01 作者:月下独舞

我的目标是叠加PDF格式的内容到另一个PDF。

My objective is to superimpose contents of a pdf onto another PDF.

这是我已经使用了code:

this is the code that I have used:

AssetManager assetManager = getAssets();
    InputStream istr = null;
    PdfReader reader = null;
    String str = null;
    int n = 0;
    try
    {                       
        istr =(InputStream) assetManager.open("Chemistry.pdf");

        reader=new PdfReader(istr);     




        Document document = new Document();
      PdfWriter writer=  PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory()+ "/newhelloPrayToGod.pdf"));


        document.open();

        document.add(new Paragraph("Hello World"));
        document.newPage();
        PdfContentByte canvas = writer.getDirectContent();
        PdfImportedPage page;

        page = writer.getImportedPage(reader, 23);
        canvas.addTemplate(page, 1, 0, 0, 0, 0, 0);



        document.close();

        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();
    }

我已经使用itextG 5.4.3。 我甚至增加了的.jar的副本在我的库文件夹中。 在code编译,但在运行,它只是崩溃。 这是错误日志:

I have used itextG 5.4.3. I have even added a copy of the .jar in my libs folder. The code compiles, but on running, it just crashes. This is the error log:

03-28 16:27:36.693:E / AndroidRuntime(1037):致命异常:主要   03-28 16:27:36.693:E / AndroidRuntime(1037):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.teest / com.example.teest.MainActivity}:java.lang.ClassCastException:机器人.content.res.AssetManager $ AssetInputStream不能转换为com.itextpdf.text.pdf。codec.Base64 $的InputStream   03-28 16:27:36.693:E / AndroidRuntime(1037):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)   03-28 16:27:36.693:E / AndroidRuntime(1037):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)   03-28 16:27:36.693:E / AndroidRuntime(1037):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)   03-28 16:27:36.693:E / AndroidRuntime(1037):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)   03-28 16:27:36.693:E / AndroidRuntime(1037):在android.os.Handler.dispatchMessage(Handler.java:99)   03-28 16:27:36.693:E / AndroidRuntime(1037):在android.os.Looper.loop(Looper.java:137)   03-28 16:27:36.693:E / AndroidRuntime(1037):在android.app.ActivityThread.main(ActivityThread.java:5039)   03-28 16:27:36.693:E / AndroidRuntime(1037):在java.lang.reflect.Method.invokeNative(本机方法)   03-28 16:27:36.693:E / AndroidRuntime(1037):在java.lang.reflect.Method.invoke(Method.java:511)   03-28 16:27:36.693:E / AndroidRuntime(1037):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)   03-28 16:27:36.693:E / AndroidRuntime(1037):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)   03-28 16:27:36.693:E / AndroidRuntime(1037):在dalvik.system.NativeStart.main(本机方法)   03-28 16:27:36.693:E / AndroidRuntime(1037):java.lang.ClassCastException:产生的原因android.content.res.AssetManager $ AssetInputStream不能转换为com.itextpdf.text.pdf codeC。 .Base64 $的InputStream   03-28 16:27:36.693:E / AndroidRuntime(1037):在com.example.teest.MainActivity.onCreate(MainActivity.java:36)   03-28 16:27:36.693:E / AndroidRuntime(1037):在android.app.Activity.performCreate(Activity.java:5104)   03-28 16:27:36.693:E / AndroidRuntime(1037):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)   03-28 16:27:36.693:E / AndroidRuntime(1037):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

03-28 16:27:36.693: E/AndroidRuntime(1037): FATAL EXCEPTION: main 03-28 16:27:36.693: E/AndroidRuntime(1037): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.teest/com.example.teest.MainActivity}: java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream cannot be cast to com.itextpdf.text.pdf.codec.Base64$InputStream 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread.access$600(ActivityThread.java:141) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.os.Handler.dispatchMessage(Handler.java:99) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.os.Looper.loop(Looper.java:137) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread.main(ActivityThread.java:5039) 03-28 16:27:36.693: E/AndroidRuntime(1037): at java.lang.reflect.Method.invokeNative(Native Method) 03-28 16:27:36.693: E/AndroidRuntime(1037): at java.lang.reflect.Method.invoke(Method.java:511) 03-28 16:27:36.693: E/AndroidRuntime(1037): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 03-28 16:27:36.693: E/AndroidRuntime(1037): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-28 16:27:36.693: E/AndroidRuntime(1037): at dalvik.system.NativeStart.main(Native Method) 03-28 16:27:36.693: E/AndroidRuntime(1037): Caused by: java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream cannot be cast to com.itextpdf.text.pdf.codec.Base64$InputStream 03-28 16:27:36.693: E/AndroidRuntime(1037): at com.example.teest.MainActivity.onCreate(MainActivity.java:36) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.Activity.performCreate(Activity.java:5104) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

P.S。 我没有使用过任何授权文件之类的(的.xml)或任何东西。 上述code正是我都习惯了。

P.S. I have not used any license file(.xml) or anything of that sort. the above code is JUST what I have used.

我试着使用iText的罐子(不iTextG)了。 在此,在code的工作,但如果我在code使用这一行

I tried using the iText jar (not iTextG) too. In that, the code worked, but if I used this line in the code

canvas.addTemplate(page, 1, 0, 0, 0, 0, 0);

它不会与此错误编译

It would not compile with this error

类型java.awt.geom.AffineTransform中无法得到解决。这是间接地从必需的.class文件中引用

The type java.awt.geom.AffineTransform cannot be resolved. It is indirectly referenced from required .class files

我该怎么做才能在画布上,addtemplate()在Android上运行? 我要叠加在另一个PDF文件。

What do I do to get the canvas,addtemplate() running on android? I want to superimpose pdfs on another.

推荐答案

我想在这里出现异常:

istr =(InputStream) assetManager.open("Chemistry.pdf");

的异常

java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream cannot be cast to com.itextpdf.text.pdf.codec.Base64$InputStream

表示的InputStream 在code线是不是预期的 java.io 变种。

indicates that the InputStream in that code line is not the expected java.io variant.

最有可能,你有像

import com.itextpdf.text.pdf.codec.Base64.InputStream

而你其实想

import java.io.InputStream