Draw the contents of a PDF file to the canvas


multimedia

My Android app is using PdfDocument to programmatically generate multipage PDF files:

public byte[] buildPDF() {
    int pageWidth = Math.round(8.5f * 72f);
    int pageHeight = Math.round(11.0f * 72f);
    int margin = Math.round(.5f * 72f);
    int fontSize = 12;

    TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setColor(Color.BLACK);
    textPaint.setTextSize(fontSize);
    textPaint.setTypeface(Typeface.DEFAULT_BOLD);

    PdfDocument document = new PdfDocument();
    PdfDocument.Page page = null;
    Canvas canvas = null;

    for (int i=0; i<10; i++) {
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(pageWidth, pageHeight, document.getPages().size()).create();
        page = document.startPage(pageInfo);
        canvas = page.getCanvas();

        String content = "Content for page " + (i + 1);
        canvas.drawText(content, margin, margin + fontSize, textPaint);
        document.finishPage(page);
    }

    byte[] output = new byte[0];
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        document.writeTo(outputStream);
    } catch (Exception e) {
        Log.d("buildPDF", "error writing PDF output");
        return output;
    }
    output = outputStream.toByteArray();
    document.close();

    return output;
}

In some cases, I want to put an existing PDF file on my local device into this series of pages. The easiest workflow is to somehow open the PDF file as data and then write it to the canvas. Is there any way in the Android SDK to do this?

It's easy to use CGPDFDocumentRefand in iOS CGContextDrawPDFPage, but I haven't found an Android equivalent.

If there is no built-in method, the package in Apache PDFBox seems to work fine. However, I need the data in memory as described above, so I figured this would require writing the resulting data to a temp file, performing an operation on the temp file, and then reading the data back into memory.multipdf

Most used software

My Android app is using PdfDocument to programmatically generate multipage PDF files:

Note that PdfDocumentit is designed for printing, not for creating arbitrary PDF files.

The easiest workflow is to somehow open the PDF file as data and then write it to the canvas. Is there any way in the Android SDK to do this?

Not sorry

Related


Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

Draw rectangle with loaded pdf file in canvas using pdf.js

Sharma Vikram I am trying to draw rectangles on a pdf file. When I draw rectangles in pdf, the rectangles are not drawn correctly. I want to draw only one rectangle at a time, when I draw the new one, the old one should be deleted, but this is not happening. H

draw line with canvas in pdf dart

tuner I am using the plugin dart-pdf to generate pdf documents. I need to add a checkbox, but there is no checkbox widget, so I've been trying to draw it with canvas (like this would be perfect). However, I can't even draw a line. The following code does not d

How to draw path on pdf canvas using itext pdf?

User 123456 I am implementing a pdf editing application using itextpdf. I added a text to the pdf and used the canvas.drawPath() method to draw a name on the android system canvas like I want to draw a name on the pdfCanvas? I tried some examples of drawing cu

How to draw path on pdf canvas using itext pdf?

User 123456 I am implementing a pdf editing application using itextpdf. I added a text to the pdf and used the canvas.drawPath() method to draw a name on the android system canvas, just like I want to draw a name on the pdfCanvas? I tried some examples of draw

Programmatically draw svg file on html canvas

honkskillet I'm having some trouble drawing an svg to a canvas with javascript. I want this to work... HTML <canvas class="buttonCanvas" id="handCanvas" height="60px" width="60px" /> Java script function drawHandCanvas(){ var canvas = document.getElementB

javascript: upload image file and draw it into canvas

Aloso I am working in a web application for drawing images. I use CANVAS element and javascript to draw on it, but I have a question: how can I load the image from the user's PC and draw it on the canvas? I don't want to save it on the server, only on the web

How to draw bitmap file on HTML canvas?

username How to draw .bmp file using canvas tag in html. I found a way to draw png and jpeg instead of bmp files. Can someone suggest me these methods and are bmp files supported by every browser and platform? please help. hyphn knight Drawing .bmp files are n

javascript: upload image file and draw it into canvas

Aloso I am working in a web application for drawing images. I use CANVAS element and javascript to draw on it, but I have a question: how can I load the image from the user's PC and draw it on the canvas? I don't want to save it on the server, only on the web

How to draw bitmap file on HTML canvas?

username How to draw .bmp file using canvas tag in HTML. I found a way to draw png and jpeg instead of bmp files. Can someone suggest me these methods and are bmp files supported by every browser and platform? please help. hyphn knight Drawing .bmp files are n

How to draw bitmap file on HTML canvas?

username How to draw .bmp file using canvas tag in HTML. I found a way to draw png and jpeg instead of bmp files. Can someone suggest me these methods and are bmp files supported by every browser and platform? please help. hyphn knight Drawing .bmp files are n

javascript: upload image file and draw it into canvas

Aloso I am working in a web application for drawing images. I use CANVAS element and javascript to draw on it, but I have a question: how can I load the image from the user's PC and draw it on the canvas? I don't want to save it on the server, only on the web

perl, extract table of contents from PDF file

Mike Clamine I've checked through CAM::PDF and other PDF related modules, but can't figure out if there is a way to extract the table of contents from a clear PDF file. Any ideas would be greatly appreciated! Harvey I haven't been able to find a library that c

Save users in listbox contents as PDF file

Linux Pingu Below is the macro assigned to my "Generate Report" command button to save the active sheet as a pdf file. I am trying to use this macro to save the contents of a userform listbox as a PDF. Is this achievable? Sub PDFActiveSheet() Dim ws As Workshe

perl, extract table of contents from PDF file

Mike Clamine I've checked through CAM::PDF and other PDF related modules, but can't figure out if there is a way to extract the table of contents from a clear PDF file. Any ideas would be greatly appreciated! Harvey I haven't been able to find a library that c

Save users in listbox contents as PDF file

Linux Pingu Below is the macro assigned to my "Generate Report" command button to save the active sheet as a pdf file. I am trying to use this macro to save the contents of a userform listbox as a PDF. Is this achievable? Sub PDFActiveSheet() Dim ws As Workshe

How to draw table in PDF file in iOS.

Reksh Tumar Currently, I am developing an expense management application. In this application I want to retrieve data from a local database and create a pdf file for it in tabular format. How to do this requires guidance. thanks. Nitesh Borad You can create a

How to draw image from HTML5 File API on Canvas?

Jonas: I want to draw on canvas an image opened using HTML5 File API. In that handleFiles(e)method, I can use to access the file, e.target.files[0]but not to draw that image directly drawImage. How to draw image from File API on HTML5 canvas? Here is the code

How to draw image from HTML5 File API on Canvas?

Jonas: I want to draw on canvas an image opened using HTML5 File API. In that handleFiles(e)method, I can use to access the file, e.target.files[0]but not to draw that image directly drawImage. How to draw image from File API on HTML5 canvas? Here is the code

OOM: draw 3MB .png file on canvas

red star I'm trying to draw a bitmap (as a background) on a SurfaceView's canvas I tried: Drawable bg = ContextCompat.getDrawable(surfaceView.getContext(), R.drawable.bg); bg.draw(canvas); Then I try: BitmapFactory.Options options = new BitmapFactory.Options(