Reputation: 671
i am using pdfviewer library to show pdf. Now i want add signature image in pdf on the position where user taps. Signature image can be resize and move along the pdf page. can i do that with iText jar ? Or there is any other way to do it.
kindly provide me the solution if you have.
i am using this way to show pdf
Example of code to implement a PDF reader
Upvotes: 0
Views: 6428
Reputation: 671
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = Bitmap.createScaledBitmap(pdfBtm, (int)(pdfBtm.getWidth()), (int)(pdfBtm.getHeight()), true);
bmp.compress(CompressFormat.PNG, 0, stream);
byte[] byteArray = stream.toByteArray();
Image img = Image.getInstance(byteArray);
String pdffile = sharedPref.getString(com.appealsoft.i_file_me.Config.PdffileName, "");
int pageNumber = sharedPref.getInt(com.appealsoft.i_file_me.Config.PdfpageNumber, 0);
PdfReader reader = new PdfReader(pdffile);
String filename = pdffile.substring(pdffile.lastIndexOf("/")+1, pdffile.length());
System.out.println("file name is :" + filename);
OutputStream newfile = new FileOutputStream(new File("/sdcard/"+filename));
Document newDocs = new Document();
PdfWriter writer = PdfWriter.getInstance(newDocs, newfile);
newDocs.open();
for(int i = 1 ;i<=reader.getNumberOfPages();i++)
{
if(i == pageNumber)
{
Image img2 = Image.getInstance(byteArray);
newDocs.add(img2);
System.out.println(" i was inside...");
}else{
Image img2 = Image.getInstance(writer.getImportedPage(reader, i));
newDocs.add(img2);
}
}
newDocs.close();
this is solution by which i did it. I am just facing a problem now. my created pdf is not getting all the contents of image or bitmap if its wide. Other Images are shifting to right side.
Anyone has any idea why this is happening.?
thanks for your time.
Upvotes: 0
Reputation: 1
There is text added for the page number as the bottom right corner. Here is the source code ...
package com.samplecode;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.FileOutputStream;
import java.io.IOException;
public class PdfStamperExample {
public static void main(String[] args) {
try {
PdfReader pdfReader = new PdfReader("data/FormW4.pdf");
PdfStamper pdfStamper = new PdfStamper(pdfReader,
new FileOutputStream("data/FormW4-Stamped.pdf"));
Image image = Image.getInstance("data/Approved.png");
for(int i=1; i<= pdfReader.getNumberOfPages(); i++){
//put content under
PdfContentByte content = pdfStamper.getUnderContent(i);
image.setAbsolutePosition(100f, 150f);
content.addImage(image);
//put content over
content = pdfStamper.getOverContent(i);
image.setAbsolutePosition(300f, 150f);
content.addImage(image);
//Text over the existing page
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.WINANSI, BaseFont.EMBEDDED);
content.beginText();
content.setFontAndSize(bf, 18);
content.showTextAligned(PdfContentByte.ALIGN_LEFT,"Page No: " + i,430,15,0);
content.endText();
}
pdfStamper.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
Upvotes: 3