Friday 11 December 2020

Adding Text/Annotations into existing PDF file and View/Rendering the output in android

I am working on a pdf editor.

I have made my changes on pdf files with OpenPDF core that is based on iText

And I am viewing the Pdf file with AndroidPdfViewer

My problems are:

  1. Adding new annotations like text or tags or icons into an existing pdf file. ( SOLVED )

  2. Show new changes right after annotations added into pdf file. ( SOLVED )

  3. Convert user click into Pdf file coordinates to add new annotation on user clicked location.

  4. Get click event on added annotations and read meta data that added into that annotation , for ex: read tag hash id that sets on icon annotation.

  5. Remove added annotation from PDF File.

Any help appreciated

UPDATE

========================================================================

Solution 1

  • Here is my code snippet for adding icon annotation into existing pdf file.

public static void addWatermark(Context context, String filePath) throws FileNotFoundException, IOException {

        // get file and FileOutputStream
        if (filePath == null || filePath.isEmpty())
            throw new FileNotFoundException();

        File file = new File(filePath);

        if (!file.exists())
            throw new FileNotFoundException();

        try {

            // inout stream from file
            InputStream inputStream = new FileInputStream(file);

            // we create a reader for a certain document
            PdfReader reader = new PdfReader(inputStream);

            // get page file number count
            int pageNumbers = reader.getNumberOfPages();

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

            // adding content to each page
            int i = 0;
            PdfContentByte under;

            // get watermark icon
            Image img = Image.getInstance(PublicFunction.getByteFromDrawable(context, R.drawable.ic_chat));
            img.setAnnotation(new Annotation("tag", "gd871394bh2c3r", 0, 0, 0, 0));
            img.setAbsolutePosition(230, 190);
            img.scaleAbsolute(50, 50);

            while (i < pageNumbers) {
                i++;
                // watermark under the existing page
                under = stamp.getUnderContent(i);
                under.addImage(img);
            }

            // closing PdfStamper will generate the new PDF file
            stamp.close();

        } catch (Exception de) {
            de.printStackTrace();
        }

    }
}

Solution 2

  • Here is my code snippet for refreshing the view after adding annotation, I have added this into AndroidPdfViewer core classes.
public void refresh(int currPage) {

            currentPage = currPage;

            if (!hasSize) {
                waitingDocumentConfigurator = this;
                return;
            }
            PDFView.this.recycle();
            PDFView.this.callbacks.setOnLoadComplete(onLoadCompleteListener);
            PDFView.this.callbacks.setOnError(onErrorListener);
            PDFView.this.callbacks.setOnDraw(onDrawListener);
            PDFView.this.callbacks.setOnDrawAll(onDrawAllListener);
            PDFView.this.callbacks.setOnPageChange(onPageChangeListener);
            PDFView.this.callbacks.setOnPageScroll(onPageScrollListener);
            PDFView.this.callbacks.setOnRender(onRenderListener);
            PDFView.this.callbacks.setOnTap(onTapListener);
            PDFView.this.callbacks.setOnLongPress(onLongPressListener);
            PDFView.this.callbacks.setOnPageError(onPageErrorListener);
            PDFView.this.callbacks.setLinkHandler(linkHandler);

            if (pageNumbers != null) {
                PDFView.this.load(documentSource, password, pageNumbers);
            } else {
                PDFView.this.load(documentSource, password);
            }
        }

I will add other new solutions into my question, as update parts.



from Adding Text/Annotations into existing PDF file and View/Rendering the output in android

No comments:

Post a Comment