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?