I have .pdf file and multiple forms are there.
I want to open my .pdf file, fill the forms and save it from Android development.
Is there any API for Android Rendering.
I found iText but I just manage to create new pdf and than i can fill form. means which .pdf file i created that will be filled out. I need to fill my form in my own .pdf.
Thanks in Advance...any help will be appreciated...
DynamicPDF Merger for Java allows you to do just that. You can take an existing PDF document, fill out the form field values and then output that newly filled PDF.
There was a recent blog post on dynamicpdf.com on setting up DynamicPDF for Java in an Android application and creating a simple PDF from it, http://www.dynamicpdf.com/Blog/post/2012/06/15/Generating-PDFs-Dynamically-on-Android.aspx.
You can easily take that example one step further and use it to accomplish your task of form filling. The following (untested) code is an example of what it would take to form fill an existing PDF on an Android device using DynamicPDF Merger for Java:
InputStream inputStream = this.getAssets().open("PDFToFill.pdf");
long avail = inputStream.available();
byte[] samplePDF = new byte[(int) avail];
inputStream.read(samplePDF , 0, (int) avail);
inputStream.close();
PdfDocument objPDF = new PdfDocument(samplePDF);
MergeDocument document = new MergeDocument(objPDF);
document.getForm().getFields().getFormField("FormField1").setValue("My Text");
document.draw("[PhysicalPath]/FilledPDF.pdf");
The native PDF support on current Android platforms (including Android P) doesn't expose any controls for filling forms. 3rd-party PDF SDKs such as PSPDFKit fill this gap and allow programmatic PDF form filling:
List<FormField> formFields = document.getFormProvider().getFormFields();
for (FormField formField : formFields) {
if (formField.getType() == FormType.TEXT) {
TextFormElement textFormElement = (TextFormElement) formField.getFormElement();
textFormElement.setText("Test " + textFormElement.getName());
} else if (formField.getType() == FormType.CHECKBOX) {
CheckBoxFormElement checkBoxFormElement = (CheckBoxFormElement)formField.getFormElement();
checkBoxFormElement.toggleSelection();
}
}
(If you click on above link there's also a Kotlin PDF form filling example.)
Note that most SDKs on the market focus on PDF AcroForms, and not the XFA specification, which has been deprecated in the PDF 2.0 spec.
Related
Context
I'm using a third-party library called PsPdfKit to edit PDF files, adding annotations - like PNG images, and text decorations - on top of the PDF document. But there's a limitation of this library where I'm not able to use this annotation feature on password-protected PDF files. I can open and see the documents, but I'm unable to actually drop these annotations in.
What I'm trying to do now is to figure out whether could be possible to create an editable copy of the PDF file.
Question
Is there any way on Android to create an editable copy of a password protected PDF file? Again, these password protected PDF files are only preventing writing any changes on top of the PDF, you don't actually need a password to see the PDF content.
My idea is to create an editable copy of the PDF file and then pass that copy to the PsPdfKit library.
Okay, so I've finally figured out a workaround for this issue. What I did was to instantiate the password protected PDF file via PsPdfKit and then create a writeable copy of the file using the page bitmaps of the password protected file. A bit of a hacky solution, but it did allow me to use the annotation feature.
// We open the password-protected file.
val readOnlyPdfDocument = PdfDocumentLoader.openDocument(applicationContext, readOnlyFile.toUri())
// Use the PsPdfKit API to create a copy of the pages of this document
val task = PdfProcessorTask.newPage(NewPage.fromPage(readOnlyPdfDocument, 0).backgroundColor(Color.RED).build())
for (i in 1 until readOnlyPdfDocument.pageCount) {
task.addNewPage(NewPage.fromPage(readOnlyPdfDocument, i).backgroundColor(Color.RED).build(), i)
}
// Finally store the writeable PDF document in a new file
val writeableFile = File(applicationContext.cacheDir, "Writeable.pdf")
PdfProcessor.processDocument(task, writeableFile)
Using the Android sample in the WebViewer folder I have an application in Android Studio which when run on my device works and displays the xod file given as expected. However I've tried to change the lines:
String documentPath = getFilesDir().getPath() + "/GettingStarted.xod";
InputStream in = getAssets().open("xod/GettingStarted.xod");
to
String documentPath = getFilesDir().getPath() + "/sample.pdf";
InputStream in = getAssets().open("xod/sample.pdf");
And have also changed the endsWith(".xod") to endsWith(".pdf") but I only get a grey screen. For this to work does the file have to be a .xod? As it works for xod files but not pdf files.
Thanks for your time.
Yes, for mobile viewing you need to convert your files to the web-optimized XOD format.
The PDF backend for WebViewer is not available for mobile browsers. This is due to limitations of the hardware and the mobile browsers.
For desktop browsers, to switch from XOD backend to PDF backend, you need to follow the steps. See here for more details, especially if you run into any errors.
var myWebViewer = new PDFTron.WebViewer({
path: "lib",
type: "html5",
documentType: "pdf",
initialDoc: "GettingStarted.pdf"
}, viewerElement);
Notice the documentType parameter is set to pdf.
My server spits out the source of a PDF. With XHR2 I request that code using xhr.responseType = 'blob'; For iOS I then just use
blob = new Blob([this.response], {type: "application/pdf"});
and then save it using Phonegap's Filewriter. When I then open the saved file, I have a perfect PDF. I simply can't get anything similar to work on Android. My last attempt was:
if (!window.BlobBuilder && window.WebKitBlobBuilder){
window.BlobBuilder = window.WebKitBlobBuilder;
var bb = new window.BlobBuilder();
bb.append(this.response);
blob = bb.getBlob("application/pdf");}
since Android coughed over just plain 'BlobBuilder'. That duly creates a PDF which FileWriter then saves. The files structure is presumably OK as a PDF since PDF readers open it without complaint. Unfortunately, the PDF is completely blank.
I just don't seem to be able to get my elderly head around whatever is required by Android.
I am working on Android and using docx4j to view the docx,pptx and xlsx files into my application.
I am unable to view the ppt files. I am getting compile time error at SvgExporter class. which is not there in docx4j library.
How can I get the SvgExporter class library and build my application and get the Svghtml to load on webview for ppt files? My code is as follows:
String inputfilepath = System.getProperty("user.dir") + "/sample-docs/pptx/pptx-basic.xml";
// Where to save images
SvgExporter.setImageDirPath(System.getProperty("user.dir") + "/sample-docs/pptx/");
PresentationMLPackage presentationMLPackage =
(PresentationMLPackage)PresentationMLPackage.load(new java.io.File(inputfilepath));
// TODO - render slides in document order!
Iterator partIterator = presentationMLPackage.getParts().getParts().entrySet().iterator();
while (partIterator.hasNext()) {
Map.Entry pairs = (Map.Entry)partIterator.next();
Part p = (Part)pairs.getValue();
if (p instanceof SlidePart) {
System.out.println(
SvgExporter.svg(presentationMLPackage, (SlidePart)p)
);
}
}
// NB: file suffix must end with .xhtml in order to see the SVG in a browser
}
SvgExporter uses XSLT and Xalan extension functions to do its thing.
IIRC, there were problems getting Xalan working on Android (you should verify this yourself).
If that remains the case, then you'll need to write a version of SvgExporter which does the traversal in Java code, as opposed to relying on Xalan to do this.
That should be quite feasible; there are "NonXSLT" examples in the docx4j code base.
I'm having a problem with iText. Other people say that iText is for PDF Creation only? and it can not read or extract text from a PDF. is that true?
If it is true then what are other options i can choose to EXTRACT text from PDF File and Save it on a Variable or Display it in Android device?
If iText is capable of Extracting text from PDF, then HOW?
iText can extract text from PDFs. While it is true that it originated as a tool to create new and manipulate existing PDFs, it in the recent years also has become better and better at extracting text. This obviously implies that you should use a current iText version (5.3.x) for text extraction.
The book "iText in Action, second edition" by the main iText developer, Bruno Lowagie, explains basic iText text extraction in chapter 15, and the samples from that chapter are available in the iText Sourceforge SVN repository, cf. Samples for chapter 15. A good starting point is ExtractPageContentSorted2 which extracts the text of a whole page.
If you have special requirements, you may use ExtractPageContentSorted1 as a starting point which explicitly defines a text extraction strategy; depending on your requirements you will need your own startegy. If you want the text from a specific region only, look at ExtractPageContentArea.
To really fine tune the text extraction capabilities of iText, you should have a look at the itext-question mailing list archive (e.g. at nabble.com) as recently the iText text extraction API was extended to serve additional use cases.
Use below code to extract text from pdf :
String pat = data.getData().getPath();
File f = new File(pat);
//f is file path of pdf file
read = new PdfReader(new FileInputStream(f));
parser = new PdfReaderContentParser(read);
strw = new StringWriter();
stretegy = parser.processContent(j, new SimpleTextExtractionStrategy());
strw.write(stretegy.getResultantText());
String da = strw.toString();
//set extracted text from pdf file
//to Edit-text
edt1.setText(da);