You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Tilman Hausherr (JIRA)" <ji...@apache.org> on 2019/05/30 09:45:00 UTC

[jira] [Comment Edited] (PDFBOX-4556) how the visual representation of the signature is achieved

    [ https://issues.apache.org/jira/browse/PDFBOX-4556?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16851533#comment-16851533 ] 

Tilman Hausherr edited comment on PDFBOX-4556 at 5/30/19 9:44 AM:
------------------------------------------------------------------

Hi tilman,

can you please guide how the stamping on PDF is achieved using the pdfbox? i want to show some image or text on the PDF when it is signed. i am using below code to show the visual representation of the signature.i am reading the  the inputstream returned from the method and set it to signatureoptions.setVisualSignature to read it. Also  attaching the stacktrace  please let me know where i am doing wrong?

getting null pointer exception 

stacktrace :
{noformat}
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSStream obj)
 at org.apache.pdfbox.cos.COSStream.accept(ICOSVisitor visitor)
 at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSBase obj)
 at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSDocument doc)
 at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSDocument doc)
 at org.apache.pdfbox.cos.COSDocument.accept(ICOSVisitor visitor)
 at org.apache.pdfbox.pdfwriter.COSWriter.write(PDDocument doc)
 at org.apache.pdfbox.pdmodel.PDDocument.save(OutputStream output)
 at PdfBox.ResponseDigest.createVisualSignatureTemplate(PDDocument signedDoc, Int32 stampingPage, String tickImagePath, String name, String location, String reason, Int32 xCo_ordinates, Int32 yCo_ordinates, Int32 signatureHeight, Int32 signatureWidth) in D:\balasaheb\Esign 2.1Try\PdfBoxTest\PdfBox\PdfBox\ResponseDigest.aspx.cs:line 466
{noformat}

-----------------------------------------------------------------------------------------------------------
{code}
SignatureOptions signatureoptions = new SignatureOptions();

signatureoptions.setVisualSignature(createVisualSignatureTemplate(signedDocument, pageNo, tickImagePath, name, location, reason, xCo_ordinates, yCo_ordinates, signatureHeight, signatureWidth));

please find the method 

private InputStream createVisualSignatureTemplate(PDDocument signedDoc, int stampingPage, string tickImagePath, string name, string location, string reason, int xCo_ordinates, int yCo_ordinates, int signatureHeight, int signatureWidth)
 {
 java.io.File imageFile = new java.io.File(tickImagePath);
 PDPage page = (PDPage)signedDoc.getDocumentCatalog().getAllPages().get(stampingPage - 1);
 float x = (float)xCo_ordinates;
 float y = (float)yCo_ordinates;
 float sigWidth = (float)signatureWidth;
 float sigheight = (float)signatureHeight;


 ByteArrayOutputStream baos = new ByteArrayOutputStream();

try
 {

 Rectangle2D humanRectangle = new Rectangle2D.Float(xCo_ordinates, yCo_ordinates, signatureWidth, signatureHeight);
 // create signature rectangle
 PDRectangle pdRectangle = null;
 if (pdRectangle == null)
 {
 pdRectangle = createSignatureRectangle(signedDoc, humanRectangle, stampingPage);
 }

 PDAcroForm acroForm = new PDAcroForm(signedDoc);
 signedDoc.getDocumentCatalog().setAcroForm(acroForm);
 signedDoc.getDocumentCatalog().getCOSObject().setNeedToBeUpdate(true);

 PDSignatureField signatureField = new PDSignatureField(acroForm);
 List<PDField> acroFormFields = new List<PDField>();
 acroForm.getCOSObject().setDirect(true);
 acroForm.getCOSObject().setNeedToBeUpdate(true);
 acroFormFields.Add(signatureField);
 signatureField.getCOSObject().setNeedToBeUpdate(true);

 PDAnnotationWidget Widget = new PDAnnotationWidget();
 //PDAnnotation Widget = PDAnnotation.createAnnotation(signedDoc.getDocumentCatalog().getCOSObject());
 Widget.getCOSObject().setNeedToBeUpdate(true);
 Widget.setRectangle(pdRectangle);
 Widget.setPage(page);

 PDStream pdfstream = new PDStream(signedDoc);
 pdfstream.getCOSObject().setNeedToBeUpdate(true);
 PDXObjectForm form = new PDXObjectForm(pdfstream);
 form.getCOSObject().setNeedToBeUpdate(true);
 // set resources to xobj
 PDResources resources = new PDResources();
 resources.getCOSObject().setNeedToBeUpdate(true);
 PDFont font = PDType1Font.HELVETICA;

 form.setResources(resources);
 form.setFormType(1);
 PDRectangle bbox = new PDRectangle(pdRectangle.getWidth(), pdRectangle.getHeight());
 float height = bbox.getHeight();
 form.setBBox(pdRectangle);
PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary(signedDoc.getDocumentCatalog().getCOSDictionary());
 appearanceDictionary.getCOSObject().setDirect(true);
 appearanceDictionary.getCOSObject().setNeedToBeUpdate(true);
 PDAppearanceStream appearanceStream = new PDAppearanceStream(form.getCOSStream());
 appearanceStream.getCOSObject().setNeedToBeUpdate(true);
appearanceDictionary.setNormalAppearance(appearanceStream);
 Widget.setAppearance(appearanceDictionary);
 try
 {
 PDPageContentStream pagecontentStream = new PDPageContentStream(signedDoc, page, true, false);
 pagecontentStream.setNonStrokingColor(java.awt.Color.black);
 pagecontentStream.addRect(x, y, sigWidth, sigheight);
 int windingRule = PathIterator.WIND_NON_ZERO;
 pagecontentStream.fill(windingRule);
PDXObjectImage imgobj = null;
 imgobj = new PDJpeg(signedDoc, new FileInputStream(tickImagePath));
 //imgobj = createImageobject(tickImagePath, signedDoc, stampingPage);
 // draw xobject with coordinates

 pagecontentStream.drawImage(imgobj, x, y);
 pagecontentStream.saveGraphicsState();

 pagecontentStream.restoreGraphicsState();

float fontSize = 8;
 float leading = fontSize * 1.0f;
 pagecontentStream.beginText();

if (!string.IsNullOrEmpty(name))
 {
 pagecontentStream.drawString("signed By:");
 pagecontentStream.drawString("Name:" + name);
 }
 if (!string.IsNullOrEmpty(location))
 pagecontentStream.drawString("Location:" + location);
 if (!string.IsNullOrEmpty(reason))
 pagecontentStream.drawString("Reason:" + reason);
 pagecontentStream.endText();
 pagecontentStream.close();
 }
 catch (System.Exception ex)
 {
 ex.StackTrace.ToString();
 using (StreamWriter sw = new StreamWriter(@"D:\balasaheb\Esign 2.1Try\Test" + "\\" + "Log.txt"))
 {
 sw.Write(ex.StackTrace.ToString());
 sw.Close();
 }
 }

signedDoc.save(baos);
return new ByteArrayInputStream(baos.toByteArray());
 }
{code}



was (Author: bal@123):
Hi tilman,

can you please guide how the stamping on PDF is achieved using the pdfbox? i want to show some image or text on the PDF when it is signed. i am using below code to show the visual representation of the signature.i am reading the  the inputstream returned from the method and set it to signatureoptions.setVisualSignature to read it. Also  attaching the stacktrace  please let me know where i am doing wrong?

getting null pointer exception 

stacktrace :

at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSStream obj)
 at org.apache.pdfbox.cos.COSStream.accept(ICOSVisitor visitor)
 at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSBase obj)
 at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSDocument doc)
 at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSDocument doc)
 at org.apache.pdfbox.cos.COSDocument.accept(ICOSVisitor visitor)
 at org.apache.pdfbox.pdfwriter.COSWriter.write(PDDocument doc)
 at org.apache.pdfbox.pdmodel.PDDocument.save(OutputStream output)
 at PdfBox.ResponseDigest.createVisualSignatureTemplate(PDDocument signedDoc, Int32 stampingPage, String tickImagePath, String name, String location, String reason, Int32 xCo_ordinates, Int32 yCo_ordinates, Int32 signatureHeight, Int32 signatureWidth) in D:\balasaheb\Esign 2.1Try\PdfBoxTest\PdfBox\PdfBox\ResponseDigest.aspx.cs:line 466

-----------------------------------------------------------------------------------------------------------

SignatureOptions signatureoptions = new SignatureOptions();

signatureoptions.setVisualSignature(createVisualSignatureTemplate(signedDocument, pageNo, tickImagePath, name, location, reason, xCo_ordinates, yCo_ordinates, signatureHeight, signatureWidth));

please find the method 

private InputStream createVisualSignatureTemplate(PDDocument signedDoc, int stampingPage, string tickImagePath, string name, string location, string reason, int xCo_ordinates, int yCo_ordinates, int signatureHeight, int signatureWidth)
 {
 java.io.File imageFile = new java.io.File(tickImagePath);
 PDPage page = (PDPage)signedDoc.getDocumentCatalog().getAllPages().get(stampingPage - 1);
 float x = (float)xCo_ordinates;
 float y = (float)yCo_ordinates;
 float sigWidth = (float)signatureWidth;
 float sigheight = (float)signatureHeight;


 ByteArrayOutputStream baos = new ByteArrayOutputStream();

try
 {

 Rectangle2D humanRectangle = new Rectangle2D.Float(xCo_ordinates, yCo_ordinates, signatureWidth, signatureHeight);
 // create signature rectangle
 PDRectangle pdRectangle = null;
 if (pdRectangle == null)
 {
 pdRectangle = createSignatureRectangle(signedDoc, humanRectangle, stampingPage);
 }

 PDAcroForm acroForm = new PDAcroForm(signedDoc);
 signedDoc.getDocumentCatalog().setAcroForm(acroForm);
 signedDoc.getDocumentCatalog().getCOSObject().setNeedToBeUpdate(true);

 PDSignatureField signatureField = new PDSignatureField(acroForm);
 List<PDField> acroFormFields = new List<PDField>();
 acroForm.getCOSObject().setDirect(true);
 acroForm.getCOSObject().setNeedToBeUpdate(true);
 acroFormFields.Add(signatureField);
 signatureField.getCOSObject().setNeedToBeUpdate(true);

 PDAnnotationWidget Widget = new PDAnnotationWidget();
 //PDAnnotation Widget = PDAnnotation.createAnnotation(signedDoc.getDocumentCatalog().getCOSObject());
 Widget.getCOSObject().setNeedToBeUpdate(true);
 Widget.setRectangle(pdRectangle);
 Widget.setPage(page);

 PDStream pdfstream = new PDStream(signedDoc);
 pdfstream.getCOSObject().setNeedToBeUpdate(true);
 PDXObjectForm form = new PDXObjectForm(pdfstream);
 form.getCOSObject().setNeedToBeUpdate(true);
 // set resources to xobj
 PDResources resources = new PDResources();
 resources.getCOSObject().setNeedToBeUpdate(true);
 PDFont font = PDType1Font.HELVETICA;

 form.setResources(resources);
 form.setFormType(1);
 PDRectangle bbox = new PDRectangle(pdRectangle.getWidth(), pdRectangle.getHeight());
 float height = bbox.getHeight();
 form.setBBox(pdRectangle);
PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary(signedDoc.getDocumentCatalog().getCOSDictionary());
 appearanceDictionary.getCOSObject().setDirect(true);
 appearanceDictionary.getCOSObject().setNeedToBeUpdate(true);
 PDAppearanceStream appearanceStream = new PDAppearanceStream(form.getCOSStream());
 appearanceStream.getCOSObject().setNeedToBeUpdate(true);
appearanceDictionary.setNormalAppearance(appearanceStream);
 Widget.setAppearance(appearanceDictionary);
 try
 {
 PDPageContentStream pagecontentStream = new PDPageContentStream(signedDoc, page, true, false);
 pagecontentStream.setNonStrokingColor(java.awt.Color.black);
 pagecontentStream.addRect(x, y, sigWidth, sigheight);
 int windingRule = PathIterator.WIND_NON_ZERO;
 pagecontentStream.fill(windingRule);
PDXObjectImage imgobj = null;
 imgobj = new PDJpeg(signedDoc, new FileInputStream(tickImagePath));
 //imgobj = createImageobject(tickImagePath, signedDoc, stampingPage);
 // draw xobject with coordinates

 pagecontentStream.drawImage(imgobj, x, y);
 pagecontentStream.saveGraphicsState();

 pagecontentStream.restoreGraphicsState();

float fontSize = 8;
 float leading = fontSize * 1.0f;
 pagecontentStream.beginText();

if (!string.IsNullOrEmpty(name))
 {
 pagecontentStream.drawString("signed By:");
 pagecontentStream.drawString("Name:" + name);
 }
 if (!string.IsNullOrEmpty(location))
 pagecontentStream.drawString("Location:" + location);
 if (!string.IsNullOrEmpty(reason))
 pagecontentStream.drawString("Reason:" + reason);
 pagecontentStream.endText();
 pagecontentStream.close();
 }
 catch (System.Exception ex)
 {
 ex.StackTrace.ToString();
 using (StreamWriter sw = new StreamWriter(@"D:\balasaheb\Esign 2.1Try\Test" + "\\" + "Log.txt"))
 {
 sw.Write(ex.StackTrace.ToString());
 sw.Close();
 }
 }

signedDoc.save(baos);
return new ByteArrayInputStream(baos.toByteArray());
 }

 

 

 

> how the visual representation of the signature is achieved  
> ------------------------------------------------------------
>
>                 Key: PDFBOX-4556
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-4556
>             Project: PDFBox
>          Issue Type: Wish
>          Components: .NET
>            Reporter: bal
>            Priority: Major
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org