我试图拖动并添加图像到pdf文件并使用qoppa库保存此pdf签名,

时间:2014-12-27 08:13:06

标签: android

我正在尝试拖动图片并将其添加到pdf文件中,并使用qoppa库保存此PDF文件, 我的功能正常运行但保存我的应用程序后崩溃了。 我的代码是

    StandardFontTF.mAssetMgr = getAssets();

    String val = "/sdcard/TEST.pdf";
    Log.e("pdffilename",""+val);

    mStampImage = BitmapFactory.decodeFile("/sdcard/122.jpg");
    //mStampImage = BitmapFactory.decodeFile("/mnt/sdcard/imagex/"
            //+ signatureName);

    mStampButton = new Button(this);
    Log.e("sing","sing"+mStampButton);
    mSaveDocButton = new Button(this);
    mStampButton.setText("Sign");
    mSaveDocButton.setText("Confirm");
    mStampButton.setHeight(40);
    mStampButton.setOnClickListener(this);
    mSaveDocButton.setOnClickListener(this);

    mNotesView = new QPDFNotesView(this);
    Log.e("sing","sing"+mNotesView);
    mNotesView.setActivity(this);

    // ///////////////
    // mNotesView.loadDocument("/sdcard/Attachment.pdf");
    mNotesView.loadDocument(val);
    // /////////////////////
    // removing unwanted buttons
    if (isOnlyView == true) {

        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbNewDoc());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbOpen());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolInk());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolNote());

        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolTypewriter());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolUnderline());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolUnderline());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolFreetext());

        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolFreeHighlight());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolStrikethrough());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbShowSearch());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolHighlight());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolLine());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolSquare());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolCircle());

    } else {
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbNewDoc());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbOpen());

        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolNote());
        mNotesView.getToolbar().removeView(

                mNotesView.getToolbar().getIbToolTypewriter());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolUnderline());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolUnderline());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolFreetext());

        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolFreeHighlight());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolStrikethrough());`enter code here`
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbShowSearch());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolHighlight());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolLine());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolSquare());
        mNotesView.getToolbar().removeView(
                mNotesView.getToolbar().getIbToolCircle());

        mNotesView.getToolbar().addView(mStampButton);
        mNotesView.getToolbar().addView(mSaveDocButton);
        mSaveDocButton.setClickable(false);
    }
    setContentView(mNotesView);

}

class ImageDrawingTool extends AnnotDrawingTool {
    private PointF m_CurrAnnotCenter;
    private Matrix drawingMatrix = new Matrix();

    public ImageDrawingTool(Context arg0) {
        super(arg0);
    }

    @Override
    protected Annotation initAnnot(PointF point) {

        point = convScreenToPdf(point);


        stamp = AnnotationFactory.createRubberStamp(mStampImage);
        // center rectangle around given point
        stamp.setRectangle(new RectF(point.x - mStampImage.getWidth() / 2,
                point.y - mStampImage.getHeight() / 2, point.x
                        + mStampImage.getWidth() / 2, point.y
                        + mStampImage.getHeight() / 2));

        m_CurrAnnotCenter = new PointF(stamp.getRectangle().centerX(),
                stamp.getRectangle().centerY());

        drawingMatrix.preScale(getViewer().getCurrentScale(), getViewer()
                .getCurrentScale());

        return stamp;
    }


    @Override
    public void dragged(MotionEvent e) {
        // reposition the Stamp according the the user dragging

        PointF draggedPoint = convScreenToPdf(e.getX(), e.getY());

        float deltaX = draggedPoint.x - m_CurrAnnotCenter.x;
        float deltaY = draggedPoint.y - m_CurrAnnotCenter.y;

        m_CurrAnnotCenter = draggedPoint;

        RectF currRect = m_Annot.getRectangle();
        currRect.offset(deltaX, deltaY);
        m_Annot.setRectangle(currRect);

        requestLayout();
        postInvalidate();
    }

    protected void finishDrawing() {
        super.finishDrawing();


        mNotesView.getPageView(0).addAnnot(
            (com.qoppa.android.pdf.annotations.b.b) stamp);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mStampImage, drawingMatrix, null);
    }

    @Override
    public boolean supportsFillColor() {
        // TODO Auto-generated method stub
        return false;
    }
}

@SuppressLint("SdCardPath")
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == mStampButton) {
        mSaveDocButton.setClickable(true);
         mNotesView.startDrawingTool(new ImageDrawingTool(this));
        int pgNo = mNotesView.getCurrentPageNumber();
        Log.e("Pgno","sing"+pgNo);
        //PDFPage page = mNotesView.getDocument().getPage(pgNo - 1);
        //Log.e("page","sing"+page);
        RubberStamp stamp = AnnotationFactory
                .createRubberStamp(mStampImage);
        Log.e("stamp","sing"+stamp);
        stamp.setBorderWidth(2);
        stamp.setRectangle(new RectF(20, 80, 100, 100));
        //mNotesView.startDrawingTool(new ImageDrawingTool(this));  
        mNotesView.startDrawingTool(new ImageDrawingTool(this));    
    }

    if (v == mSaveDocButton) {

        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        Log.e("sing1","sing"+ adb);
        adb.setTitle("Are you Sure?");
        adb.setIcon(R.drawable.apollo_emr_logo);
        adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                Toast.makeText(getApplicationContext(), "file save", Toast.LENGTH_SHORT).show();
                PDFDocument doc = mNotesView.getDocument();
                Log.e("sing11","sing"+doc);
                int totalPg = doc.getPageCount();
                Log.e("Total pg count",""+totalPg);
                for (int i = 0; i < totalPg; i++) {
                    PDFPage page = doc.getPage(i);
                    Log.e("sing111","sing"+ page);

                    try {
                        page.flattenAnnotations(true);
                        Intent intent = new Intent(PDFViewer.this,
                                PDFViewer.class);

                startActivity(intent);
                    } catch (PDFException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

                mNotesView.save("/sdcard/time.pdf");

                        Intent intent = new Intent(PDFViewer.this,
                                PDFViewer.class);

                startActivity(intent);
                // mNotesView.save("/mnt/sdcard/" + pdfFilename);
                dialog.cancel();


                //new uploadFilesTask().execute(pdfDataStr);
            }
        });
        adb.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.cancel();
                    }
                });
        adb.show();

    }
}

}

日志文件是

12-26 18:56:32.460: E/Pgno(19019): sing1
12-26 18:56:32.468: E/stamp(19019): singcom.qoppa.android.pdf.annotations.b.g@42a4ddb8
12-26 18:56:33.252: E/Pgno(19019): sing1
12-26 18:56:33.256: E/stamp(19019): singcom.qoppa.android.pdf.annotations.b.g@42a63710
12-26 18:56:34.835: E/sing1(19019): singandroid.app.AlertDialog$Builder@42a6ac28
12-26 18:56:36.084: E/sing11(19019): singcom.qoppa.android.pdfProcess.PDFDocument@429cd510
12-26 18:56:36.084: E/Total pg count(19019): 1
12-26 18:56:36.085: E/sing111(19019): singcom.qoppa.android.pdfProcess.PDFPage@42a14ed0
12-26 18:56:36.353: E/AndroidRuntime(19019): FATAL EXCEPTION: main
12-26 18:56:36.353: E/AndroidRuntime(19019): Process: com.example.qpdfdemo, PID: 19019
12-26 18:56:36.353: E/AndroidRuntime(19019): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=19019, uid=10147
12-26 18:56:36.353: E/AndroidRuntime(19019):    at android.os.Parcel.readException(Parcel.java:1465)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at android.os.Parcel.readException(Parcel.java:1419)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2406)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1256)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:365)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at com.qoppa.viewer.c.b$_b.onPostExecute(Unknown Source)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at com.qoppa.viewer.c.b$_b.onPostExecute(Unknown Source)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at android.os.AsyncTask.finish(AsyncTask.java:632)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at android.os.Looper.loop(Looper.java:136)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at android.app.ActivityThread.main(ActivityThread.java:5097)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at java.lang.reflect.Method.invokeNative(Native Method)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at java.lang.reflect.Method.invoke(Method.java:515)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-26 18:56:36.353: E/AndroidRuntime(19019):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您必须在按钮

上的侦听器之前实例化NotesView