0

So I looked around a little bit to find a good PDF APIs for android and I've discovered PDFBox. I started to play with this library and mainly I'm looking for manipulating specific pdf form.

Now I am loading the file successfuly from my assets folder, but when I try to call

getAcroForm();

It turns me a null object, Thus I can't get into the fields and start to insert some data.

Here is the code where I load the pdf file, copy it to the sdcard and load it to an PDDocument object:

package com.silverfix.pdfpractice;

import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDFieldTreeNode;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity{

    private final String FORM_NAME = "dgc.pdf";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            AssetManager assetManager = getAssets();
            File file = createFileFromInputStream(assetManager.open(FORM_NAME));

            PDDocument form = PDDocument.load(file);
            PDDocumentCatalog catalog = form.getDocumentCatalog();
            catalog.getPages().getCount();

            Toast.makeText(this, "Loaded! " + catalog.getPages().getCount(), Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "Loading has failed", Toast.LENGTH_LONG).show();
        }
    }

    private File createFileFromInputStream(InputStream inputStream) {

        try{
            OutputStream outputStream = new FileOutputStream("/sdcard/PDFPractice/" + FORM_NAME);
            byte buffer[] = new byte[1024];
            int length = 0;

            while((length=inputStream.read(buffer)) > 0) {
                outputStream.write(buffer,0,length);
            }

            outputStream.close();
            inputStream.close();

            File result = new File("/sdcard/PDFPractice/" + FORM_NAME);
            return result;
        }catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

How can I manipulate a pdf file without acroform? Or, can I add an somesort of acroform to this pdf file by myself?

David Lasry
  • 1,407
  • 4
  • 26
  • 43
  • similar question [here](http://stackoverflow.com/questions/16420509/getting-null-when-call-acroform-getfields-using-pdfbox) – Bill Jul 20 '16 at 00:40
  • Have a look at the CreateSimpleForm example in the PDFBox source code download. (Don't know if this will work on PDFBox for android). – Tilman Hausherr Jul 20 '16 at 09:56
  • Does your pdf actually contain an AcroForm form? Or are there drawings of form fields? – mkl Jul 20 '16 at 13:45

1 Answers1

0

Ok so I finally solved this problem. I downloaded Foxit PhantomPDF and it turns out you can define text fields and align them for your needs. I don't know if there is another software that does that but this one did the job for me

David Lasry
  • 1,407
  • 4
  • 26
  • 43