1

我正在尝试加密 pdf,在我成功生成带有水印的 pdf 之前,但是当我使用水印时,当我加密 pdf 时,它会给出错误:

11-08 09:03:13.579: W/System.err(29720): java.lang.IllegalArgumentException: No valid encryption mode
11-08 09:03:13.579: W/System.err(29720):    at com.itextpdf.text.pdf.PdfEncryption.setCryptoMode(PdfEncryption.java:198)
11-08 09:03:13.579: W/System.err(29720):    at com.itextpdf.text.pdf.PdfWriter.setEncryption(PdfWriter.java:2042)
11-08 09:03:13.579: W/System.err(29720):    at com.itextpdf.text.pdf.PdfStamper.setEncryption(PdfStamper.java:293)
11-08 09:03:13.579: W/System.err(29720):    at com.example.demo_pdfgenerate.WaterMarkAndPdfGenerate.WaterMarkEffect(WaterMarkAndPdfGenerate.java:135)
11-08 09:03:13.588: W/System.err(29720):    at com.example.demo_pdfgenerate.WaterMarkAndPdfGenerate.onCreate(WaterMarkAndPdfGenerate.java:81)
11-08 09:03:13.588: W/System.err(29720):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-08 09:03:13.588: W/System.err(29720):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-08 09:03:13.588: W/System.err(29720):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-08 09:03:13.588: W/System.err(29720):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-08 09:03:13.588: W/System.err(29720):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-08 09:03:13.588: W/System.err(29720):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 09:03:13.588: W/System.err(29720):    at android.os.Looper.loop(Looper.java:123)
11-08 09:03:13.588: W/System.err(29720):    at android.app.ActivityThread.main(ActivityThread.java:4627)
11-08 09:03:13.588: W/System.err(29720):    at java.lang.reflect.Method.invokeNative(Native Method)
11-08 09:03:13.588: W/System.err(29720):    at java.lang.reflect.Method.invoke(Method.java:521)
11-08 09:03:13.599: W/System.err(29720):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-08 09:03:13.599: W/System.err(29720):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-08 09:03:13.599: W/System.err(29720):    at dalvik.system.NativeStart.main(Native Method)

如果我在加水印之前加密它的工作正常,但在加水印时再次出现错误first decry-pt your pdf

那么加密的最佳方法是什么,如果我做得对,那么我缺乏的地方。

代码@

pdf生成#

        try {
        document = new Document();

        PdfWriter.getInstance(document, new FileOutputStream(FILE));
        document.open();
        addImage();
        addTitlePage(document);
        createTable();
        document.close();
       //watermarking...
       WaterMarkEffect();

水印#

内部水印我正在做加密。

    public void WaterMarkEffect() {
    try {
        reader = new PdfReader(FILE);
        n = reader.getNumberOfPages();

        // Create a stamper that will copy the document to a new file
        stamp = new PdfStamper(reader, new FileOutputStream(Des));

               //pdf encryption
        stamp.setEncryption(OWNER_PASS.getBytes(),
                USER_PASS.getBytes(), PdfWriter.ENCRYPTION_AES_256| PdfWriter.DO_NOT_ENCRYPT_METADATA, PdfWriter.ALLOW_PRINTING);


        // img = Image.getInstance("watermark.jpg");
        Drawable myImage = getResources().getDrawable(
                R.drawable.back_watermarker_dull_small);
        Bitmap bitmap = ((BitmapDrawable) myImage).getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] bitmapdata = stream.toByteArray();
        img = Image.getInstance(bitmapdata);

        bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,
                BaseFont.EMBEDDED);
        img.setAbsolutePosition(150, 0);
        // img.scalePercent(SCALE_PER);

        // while (i < n) {
        // Watermark under the existing page
        under = stamp.getUnderContent(i);
        under.addImage(img);

        // Text over the existing page
        over = stamp.getOverContent(i);
        over.beginText();
        over.setFontAndSize(bf, 18);
        over.showText("page " + i);
        over.endText();

        // i++;
        // }


        stamp.close();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

你的建议是可观的。

4

1 回答 1

3

替换您的加密代码@

采用#

        stamp.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
                PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128
                        | PdfWriter.DO_NOT_ENCRYPT_METADATA);

反而#

stamp.setEncryption(OWNER_PASS.getBytes(),
                USER_PASS.getBytes(), PdfWriter.ENCRYPTION_AES_256| PdfWriter.DO_NOT_ENCRYPT_METADATA, PdfWriter.ALLOW_PRINTING);
于 2012-11-08T11:06:14.087 回答