I am desperately trying to insert an image into an existing pdf with droidtext.
The original version of this project was made with iText. So the code already exists and was modified to fit for Android.
What I do is I take an existing PDF as background. Insert text and crosses at specified positions within this pdf. Like filling out a form. This works quite well so far without changing the code drastically.
Now I want to set an image to the bottom of the page to sign the form. I used my original code and adapted it a little. Which doesn't work at all. I tried to set the image at a specific position. Maybe that was the error.
So i tried to do it the "official" way. The Pengiuns.jpg image is located on the sd-card.
try {
Document document = new Document();
File f=new File(Environment.getExternalStorageDirectory(), "SimpleImages.pdf");
PdfWriter.getInstance(document,new FileOutputStream(f));
document.open();
document.add(new Paragraph("Simple Image"));
String path = Environment.getExternalStorageDirectory()+"/Penguins.jpg";
if (new File(path).exists()) {
System.out.println("filesize: " + path + " = " + new File(path).length());
}
Image image =Image.getInstance(path);
document.add(image);
document.close();
} catch (Exception ex) {
System.out.println("narf");
}
But still no image at all. What I get is an PDF with the words "Simple Image" on it and nothing else. I can access the picture. I get the correct filesize by the if(). No exceptions are thrown.
So my questions are, how do I get an Image located on the SD-Card into my pdf? What is the mistake here? But most importantly how do I set the image to a specific location with size within the pdf? In my original code i use setAbsolutePosition( x, y ) for that. Eclipse is not complaining when I use it in the code but is it really working?
Try following code:
The reason why you are getting the "Simple Image" is because you have it in
Paragraph
. In order to add an image, use:If you want to have it as a footer in the last page you can use the following code but it works with text. You can try to manipulate it for image:
Here is sample code. Here I have uploaded an image to Imageview and then added to pdf. Hope this helps.