Dear all,

I have been writing a Cordova plugin and looking for quite a while to add 
some text on a specific position in a pdf template. Has someone a working 
example for me?

What I have done so far

1. Read the resource file from the raw folder R.raw.template - works

   File file = new 
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
 + "/target.pdf");
   InputStream is = 
cordova.getActivity().getResources().openRawResource(R.raw.template);
   OutputStream os = new FileOutputStream(file);
   byte[] fileData = new byte[is.available()];
   is.read(fileData);
   os.write(fileData);
   is.close();
   os.close();


2. Use the PdfRenderer to get single pages from the file - works

   File templateFile = new 
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
 + "/target.pdf")
   PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, 
ParcelFileDescriptor.MODE_READ_WRITE));

   Page page = renderer.openPage(0);


3. Try to add information to the page - does not work


    public Bitmap drawText(String text, int textWidth, int textSize) {
        // Get text dimensions
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG
                | Paint.LINEAR_TEXT_FLAG);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(textSize);
        StaticLayout mTextLayout = new StaticLayout(text, textPaint,
                textWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

        // Create bitmap and canvas to draw to
        Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), 
Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);

        // Draw background
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG
                | Paint.LINEAR_TEXT_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        c.drawPaint(paint);

        // Draw text
        c.save();
        c.translate(0, 0);
        mTextLayout.draw(c);
        c.restore();

        return b;
    }


    Bitmap b = drawText("foobar", *100*, *10*);

    page.render(b, new Rect(0, 0, b.getWidth(), b.getHeight()), new Matrix(), 
PdfRenderer.Page.RENDER_MODE_FOR_PRINT);

    page.close();
    renderer.close();


But I only see the original template file. Any hints or example on this. 
External libraries should not be a solution.


-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/21692900-68a1-42d1-8b04-aae55aa4ccd6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to