-
-
Notifications
You must be signed in to change notification settings - Fork 615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get/set Annot or Widget from pdf #39
Comments
Hello @ssleptsov. This is certainly possible, as Here's how to do it: const pdfBytes = /* A Uint8Array containing the PDF you wish to modify */
const pdfDoc = PDFDocumentFactory.load(pdfBytes);
const [FontHelvetica] = pdfDoc.embedStandardFont('Helvetica');
// Define the signature dictionary (Detailed in Table 252 of PDF specification)
// Note that the dict below isn't actually valid. I just made up its `ByteRange`
// and `Contents` entries. It's just an example.
const signatureDict = PDFDictionary.from({
Type: PDFName.from('Sig'),
Filter: PDFName.from('Adobe.PPKLite'),
SubFilter: PDFName.from('adbe.pkcs7.detached'),
ByteRange: PDFArray.fromArray([
PDFNumber.fromNumber(0),
PDFNumber.fromNumber(0),
PDFNumber.fromNumber(0),
PDFNumber.fromNumber(0),
], pdfDoc.index),
Contents: PDFHexString.fromString('00000000000'),
Reason: PDFString.fromString('We need your signature for reasons...'),
// M: PDFString.fromString('D:YYYYMMDDHHmmSSOHH'mm')
// See section "7.9.4 Dates" of the PDF specification for details on the format of date strings
}, pdfDoc.index);
// Define a content stream that defines how the signature field should appear
// on the PDF.
//
// Note: This isn't necessary if you define the signature widget's "Rect" entry
// as [0, 0, 0, 0] like in the snippet you provided. But I've implemented it
// just in case you (or others) might find it useful.
const sigAppearanceStream = PDFContentStream.of(
PDFDictionary.from({
Type: PDFName.from('XObject'),
Subtype: PDFName.from('Form'),
BBox: PDFArray.fromArray([
PDFNumber.fromNumber(0),
PDFNumber.fromNumber(0),
PDFNumber.fromNumber(200),
PDFNumber.fromNumber(50),
], pdfDoc.index),
Resources: PDFDictionary.from({
Font: PDFDictionary.from({
Helvetica: FontHelvetica
}, pdfDoc.index)
}, pdfDoc.index),
}, pdfDoc.index),
drawRectangle({
x: 0,
y: 0,
width: 200,
height: 50,
colorRgb: [0.95, 0.95, 0.95],
borderWidth: 3,
borderColorRgb: [0, 0, 0],
}),
drawText('Sign Here', {
x: 10,
y: 15,
font: 'Helvetica',
size: 30,
colorRgb: [0.5, 0.5, 0.5],
}),
drawRectangle({
x: 4,
y: 4,
width: 192,
height: 2,
colorRgb: [0.5, 0.5, 0.5],
}),
);
const sigAppearanceStreamRef = pdfDoc.register(sigAppearanceStream);
// Define the signature widget annotation
const widgetDict = PDFDictionary.from({
Type: PDFName.from('Annot'),
Subtype: PDFName.from('Widget'),
FT: PDFName.from('Sig'),
Rect: PDFArray.fromArray([
PDFNumber.fromNumber(50),
PDFNumber.fromNumber(50),
PDFNumber.fromNumber(300),
PDFNumber.fromNumber(100),
], pdfDoc.index),
// Uncomment this if you've created a valid signatureDict
// V: signatureDict,
T: PDFString.fromString('Signature1'),
F: PDFNumber.fromNumber(4),
P: (pdfDoc.catalog.Pages.get('Kids') as PDFArray).get(0),
AP: PDFDictionary.from({
N: sigAppearanceStreamRef,
}, pdfDoc.index),
}, pdfDoc.index);
const widgetDictRef = pdfDoc.register(widgetDict);
// Add our signature widget to the first page
const pages = pdfDoc.getPages();
pages[0].set(
'Annots',
PDFArray.fromArray([widgetDictRef], pdfDoc.index),
);
// Create an AcroForm object containing our signature widget
const formDict = PDFDictionary.from({
SigFlags: PDFNumber.fromNumber(3),
Fields: PDFArray.fromArray([widgetDictRef], pdfDoc.index),
}, pdfDoc.index);
pdfDoc.catalog.set('AcroForm', formDict);
const modifiedPdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc); Here's a sample PDF that I modified using the script above (notice the "Sign Here" field at the bottom of the first page): pdf-with-signature-field-unsigned.pdf. And here's the original pdf. Please let me know if this is what you're looking for, or if you need any additional help with |
wow, that's awesome! Thanks for your help and the great library! |
You bet! (I updated the |
Does this work for filling form fields? I think I do not know PDFs well enough yet. |
Yes, it should be able to fill form fields. If you can share a document that you'd like to fill out, I can provide some sample code that does so. If not, I can provide an example of filling out a form field with one of the repo's test PDFs, if you let me know which types of form fields you're working with. |
@pspeter3
Sorry I haven’t had a chance to tackle this the past couple of days. I’ll
try to write up an example script this evening. If you’re able to submit a
PR, that’d be great!
- Andrew
…On Sat, Nov 10, 2018 at 12:43 PM Phips Peter ***@***.***> wrote:
Thanks! As an example, thinking about how to form fill these
http://dnd.wizards.com/articles/features/character_sheets. I would be
happy to submit a PR with an example of a form fillable document as a thank
you for your help. This project is great, thank you for building it.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#39 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AJ301KMDCv1QQMgDXaBYZAsXIryyNOe4ks5utx5TgaJpZM4Xx1vf>
.
|
No, worries. I think this project is great!
…On Tue, Nov 13, 2018, 5:57 AM Andrew Dillon ***@***.*** wrote:
Sorry I haven’t had a chance to tackle this the past couple of days. I’ll
try to write up an example script this evening. If you’re able to submit a
PR, that’d be great!
- Andrew
On Sat, Nov 10, 2018 at 12:43 PM Phips Peter ***@***.***>
wrote:
> Thanks! As an example, thinking about how to form fill these
> http://dnd.wizards.com/articles/features/character_sheets. I would be
> happy to submit a PR with an example of a form fillable document as a
thank
> you for your help. This project is great, thank you for building it.
>
> —
> You are receiving this because you modified the open/close state.
>
>
> Reply to this email directly, view it on GitHub
> <#39 (comment)>,
or mute
> the thread
> <
https://github.com/notifications/unsubscribe-auth/AJ301KMDCv1QQMgDXaBYZAsXIryyNOe4ks5utx5TgaJpZM4Xx1vf
>
> .
>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#39 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAlF1fAgu_gGvcE9JX6piABrLfZJk4A3ks5uus_cgaJpZM4Xx1vf>
.
|
Hey ! I am trying to signed my existing pdf file which i have generated using filling form with hummusjs. But i was not able to set default ByteRange for signature placeholder. Could not find ByteRange placeholder: /ByteRange [0 /********** /********** /**********] Can you please help me get this done. Thanks in advance. |
Hello @john-attrium-204! I need more information in order to help you with this. Can you please provide a code sample and a PDF that I can use to reproduce the issue? |
HI, I have been trying to put a signature mark on the pdf, but using this library I modify the PDF with the
According to the example you proposed, you occupy functions that do not exist in
And you talk about a 252 table of the PDF specification that I can't find. |
@therpobinski I am pretty sure the original snippet in this issue that @ssleptsov posted is a piece of code from the node-signpdf package. So he was trying to do the same thing you are. I think @john-attrium-204 was dealing with the same thing. Sorry @Hopding. Just saw we are commenting on a closed issue. |
It will be great if both libraries join hands - both pdf-lib and node-signpdf libraries would offer signing of new and existing pdfs. @Hopding and @vbuch - adding a pdflibAddPlaceholder in addition to pdfkit and plan one would surely resolve a major missing feature request. |
Hi, Hello, I have the same problem and I think I know what it is, I am reviewing it and I see that the placeholder has never been embedded in the PDF, so signing the document does not find the ByteRange and the error occurs. |
Hi Andrew. |
@therpobinski @brasycad @vemundeldegard Please see #112 (comment). I've updated my example for version |
Hey, is it possible to get/set Annot or Widget for existing pdf file?
I'm trying to replace this pdfkit code with your library
Thanks!
The text was updated successfully, but these errors were encountered: