Hi Guys
I am done a proof of concept using itext and because it's not completely open
source I am not allowed to use it. I basically want to open a pdf, find
positions of textboxes that contain the label Signature0; Signature1;
Signature2 etc, remove the textboxes and draw a signature area over the textbox
position. Now my question is can this be done with pdfbox or can you guys
advise or point me in the right direction.
See sample code attached of the itext app.
Your assistance would be greatly appreciated.
Best Regards
Sheldon Anthony
To read FirstRand Bank's Disclaimer for this email click on the following
address or copy into your Internet browser:
https://www.fnb.co.za/disclaimer.html
If you are unable to access the Disclaimer, send a blank e-mail to
[email protected] and we will send you a copy of the Disclaimer.
package za.co.fnb.ecm.digitalsignature;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.AcroFields.FieldPosition;
import com.itextpdf.text.pdf.AcroFields.Item;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfAppearance;
import com.itextpdf.text.pdf.PdfBorderArray;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
public class Test{
private String src = "C:\\Users\\f3488764\\FNBCRDQUOTEDECLPRGRT.pdf";
private String dest = "C:\\pdfOutput\\FNBCRDQUOTEDECLPRGRT.pdf";
public static void main(String args[]) throws IOException,
DocumentException{
Test t = new Test();
t.run();
}
public void run() throws IOException, DocumentException {
//Removed the line below to improve memory performance
//PdfReader reader = new PdfReader(src);
//Reads a PDF document.
PdfReader reader = new PdfReader(new
RandomAccessFileOrArray(src), null);
/**
* PdfStamper
* Applies extra content to the pages of a PDF document.
* This extra content can be all the objects allowed in
PdfContentByte
* including pages from other Pdfs. The original PDF will keep
all the
* interactive elements including bookmarks, links and form
fields
*/
PdfStamper stamper = new PdfStamper(reader, new
FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
HashMap<String,AcroFields.Item> fields = (HashMap<String,
Item>) form.getFields();
Set<Entry<String, Item>> entrySet = fields.entrySet();
for(Entry<String, Item> entry : entrySet) {
String key = entry.getKey();
System.out.println(key);
}
List<FieldPosition> lfp = form.getFieldPositions("Signature1");
List<FieldPosition> lfp2 = form.getFieldPositions("Signature2");
List<FieldPosition> lfp3 = form.getFieldPositions("Signature3");
form.removeField("Signature1");
form.removeField("Signature2");
form.removeField("Signature3");
for(int i = 0; i < lfp.size(); ++i) {
modifyPDF(stamper, lfp.get(i).position,
lfp.get((i)).page, 1);
form.removeFieldsFromPage(lfp.get((i)).page);
}
for(int i = 0; i < lfp2.size(); ++i) {
modifyPDF(stamper, lfp2.get(i).position,
lfp2.get((i)).page, 2);
form.removeFieldsFromPage(lfp2.get((i)).page);
}
for(int i = 0; i < lfp3.size(); ++i) {
modifyPDF(stamper, lfp3.get(i).position,
lfp3.get((i)).page, 3);
form.removeFieldsFromPage(lfp3.get((i)).page);
}
stamper.close();
reader.close();
}
public void modifyPDF(PdfStamper pdfStamper, Rectangle rect, int page,
int sigNumber) throws IOException, DocumentException {
System.out.println("Original Top: "+
rect.getTop());
rect.setTop(rect.getTop()+20);
System.out.println("Modified Right: "+
rect.getTop());
PdfWriter pdfStamperImp =
pdfStamper.getWriter();
PdfFormField signature =
PdfFormField.createSignature(pdfStamperImp);
signature.setWidget(rect,
PdfAnnotation.HIGHLIGHT_INVERT);
signature.setFieldName("Signature"+ sigNumber);
signature.setFlags(PdfAnnotation.FLAGS_PRINT);
signature.setPage(page);
signature.setMKBorderColor(BaseColor.BLACK);
signature.setMKBackgroundColor(BaseColor.WHITE);
signature.setBorder(new PdfBorderArray(2, 2,
2));
PdfAppearance tp =
PdfAppearance.createAppearance(pdfStamperImp, 72, 48);
tp.rectangle(rect);
tp.stroke();
signature.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
pdfStamper.addAnnotation(signature, page);
}
}