0

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

Community
  • 1
  • 1
nadafafif
  • 561
  • 6
  • 10
  • You might want to read Bruno's whitepaper on digital signatures in PDFs focusing on iText. – mkl Oct 07 '14 at 08:08
  • 1
    No - I don't think he needs to read it since he is (probably) talking about placing an image on an existing pdf document. This image contains (accidentally) some scribbling which looks like a signature... – Lonzak Oct 08 '14 at 09:10
  • @Lonzak Most likely you are right. – mkl Oct 09 '14 at 12:07

2 Answers2

3

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();
    }
}
}
Maveňツ
  • 1
  • 12
  • 50
  • 89
  • this will not allow me move signature image , or resize it dynamically. i used this code but this doesnt add image on all pdf area. Some portion is not editable may be. – nadafafif Oct 07 '14 at 09:05
  • 3
    Ok nadafafif - the code looks good - of course it is not finished. You need to make it dynamic and set the coordinates which you will get from android (touch gestures etc.)... Don't expect us to do your homework – Lonzak Oct 08 '14 at 09:13
  • @Lonzak Sir.. i did my task. and i was just mentioning my problem. I don't want someone to do my work i just wanted solution if you have. Thanks for time and reply – nadafafif Oct 09 '14 at 09:52
  • please post the solution if having. Thanks in advance – karthik Mar 21 '16 at 13:04
0
                 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.

nadafafif
  • 561
  • 6
  • 10
  • To ask a new question, use the "Ask Question" button at the top of the page. You can link to this question if it helps provide context. – elixenide Jun 12 '16 at 01:17