Android: Filling a PDF Form template located in raw resource directory

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Merger for Java (v8)  /  Android: Filling a PDF Form template located in raw resource directory

DynamicPDF Merger for Java (v8) Forum

Hi,

I'm evaluating DynamicPDF these days and I'm facing a problem. I'm writing an app for Android (>= 4.3) and I want to fill a PDF Form based on info coming from the user.

To do so, I have a PDF Form template that I store in "res/raw" folder. The idea is to open it then fill the form and save the modified version to another file.

The thing is that PdfDocument can't read a "file" coming from the raw directory. It is an instance of AssetInputStream and it doesn't seems to be handled. When I try, it throws an exception about the missing "size" method.

I told to myself, no problem, let read this template and create a real file on the device. To achieve this, I read the AssetInputStream and loop through it to feed a FileOutputStream. When it's done, I use new PdfDocument on the file path of the newly created PDF.

My issue is that PdfDocument throws an exception telling me the PDF is corrupted "Java exception raised: com.cete.dynamicpdf.merger.MergerException: Invalid PDF File. Cross-reference table not found."

The byte size of the original template and the copy are the same and I don't understand how I can corrupt it. I'm only iterating through the original stream to copy it byte by byte to a new stream. My code look like something like http://stackoverflow.com/a/10931005

What is the best way to achieve this in your opinion ?

To sum up I just want to open a PDF form template available in res/raw, then fill it and save it to another location.

Thank you.
Posted by a ceTe Software moderator
Hello,

The Merger exception you are getting is a typical exception thrown when the PDF is defective. It could be that the original PDF is defective or it might have gotten corrupt during conversion from AssetInputStream to FileOutputStream. Please try the following suggestions to try and narrow down the issue:

1. Create a simple Windows console application in Eclipse or NetBeans and read and save the original PDF using Merger as shown below. If you get the same error, the original PDF is defective.

PdfDocument pdf = new PdfDocument("original.pdf");
MergeDocument document = new MergeDocument(pdf);
document.draw("Output.pdf");

2. Instead of saving the PDF to file system and then opening it again with PdfDocument object, try one of the other PdfDocument overloads  that take the PDF file as byte array or ByteArrayInputStream or FileInputStream.

All that said, please keep in mind that our product is not officially supported on Andorid platform. Some of our customers were able to use it on Android under limited circumstances. However, we have not tested our java library on Android platform except for a few basic examples and there may be many other scenarios or features that do not work. The documentation link on compatibility has a list of platforms that our product officially supports.

Thanks,

ceTe Software Support Team.

All times are US Eastern Standard time. The time now is 10:00 PM.