public void CreatePdf(String filename) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filename)); Document doc = new Document(pdfDoc); doc.Add(new Paragraph("Hello World!")); // Create a signature form field PdfFormField field = PdfFormField.CreateSignature(pdfDoc, new Rectangle(72, 632, 200, 100)); field.SetFieldName(SIGNAME); field.SetPage(1); // Set the widget properties field.GetWidgets()[0].SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT).SetFlags(PdfAnnotation.PRINT); PdfDictionary mkDictionary = field.GetWidgets()[0].GetAppearanceCharacteristics(); if (null == mkDictionary) { mkDictionary = new PdfDictionary(); } PdfArray black = new PdfArray(); black.Add(new PdfNumber(ColorConstants.BLACK.GetColorValue()[0])); black.Add(new PdfNumber(ColorConstants.BLACK.GetColorValue()[1])); black.Add(new PdfNumber(ColorConstants.BLACK.GetColorValue()[2])); mkDictionary.Put(PdfName.BC, black); PdfArray white = new PdfArray(); white.Add(new PdfNumber(ColorConstants.WHITE.GetColorValue()[0])); white.Add(new PdfNumber(ColorConstants.WHITE.GetColorValue()[1])); white.Add(new PdfNumber(ColorConstants.WHITE.GetColorValue()[2])); mkDictionary.Put(PdfName.BG, white); field.GetWidgets()[0].SetAppearanceCharacteristics(mkDictionary); PdfAcroForm.GetAcroForm(pdfDoc, true).AddField(field); Rectangle rect = new Rectangle(0, 0, 200, 100); PdfFormXObject xObject = new PdfFormXObject(rect); PdfCanvas canvas = new PdfCanvas(xObject, pdfDoc); canvas .SetStrokeColor(ColorConstants.BLUE) .SetFillColor(ColorConstants.LIGHT_GRAY) .Rectangle(0 + 0.5, 0 + 0.5, 200 - 0.5, 100 - 0.5) .FillStroke() .SetFillColor(ColorConstants.BLUE); new Canvas(canvas, rect).ShowTextAligned("SIGN HERE", 100, 50, TextAlignment.CENTER, (float)(Math.PI / 180) * 25); // Note that Acrobat doesn't show normal appearance in the highlight mode. field.GetWidgets()[0].SetNormalAppearance(xObject.GetPdfObject()); doc.Close(); }
public void CreatePdf(String filename) { // step 1: Create a Document Document document = new Document(); // step 2: Create a PdfWriter PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create)); // step 3: Open the Document document.Open(); // step 4: Add content document.Add(new Paragraph("Hello World!")); // create a signature form field PdfFormField field = PdfFormField.CreateSignature(writer); field.FieldName = SIGNAME; // set the widget properties field.SetPage(); field.SetWidget(new Rectangle(72, 732, 144, 780), PdfAnnotation.HIGHLIGHT_INVERT); field.Flags = PdfAnnotation.FLAGS_PRINT; // add it as an annotation writer.AddAnnotation(field); // maybe you want to define an appearance PdfAppearance tp = PdfAppearance.CreateAppearance(writer, 72, 48); tp.SetColorStroke(BaseColor.BLUE); tp.SetColorFill(BaseColor.LIGHT_GRAY); tp.Rectangle(0.5f, 0.5f, 71.5f, 47.5f); tp.FillStroke(); tp.SetColorFill(BaseColor.BLUE); ColumnText.ShowTextAligned(tp, Element.ALIGN_CENTER, new Phrase("SIGN HERE"), 36, 24, 25); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); // step 5: Close the Document document.Close(); }
public override void Draw(DrawContext drawContext) { base.Draw(drawContext); PdfFormField field = PdfFormField.CreateSignature(drawContext.GetDocument(), GetOccupiedAreaBBox()); field.SetFieldName(name); field.GetWidgets()[0].SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT); field.GetWidgets()[0].SetFlags(PdfAnnotation.PRINT); PdfAcroForm.GetAcroForm(drawContext.GetDocument(), true).AddField(field); }
protected PdfPCell CreateSignatureFieldCell(PdfWriter writer, String name) { PdfPCell cell = new PdfPCell(); cell.MinimumHeight = 50; PdfFormField field = PdfFormField.CreateSignature(writer); field.FieldName = name; field.Flags = PdfAnnotation.FLAGS_PRINT; cell.CellEvent = new MySignatureFieldEvent(field); return(cell); }
private static PdfFormField CreateField(PdfWriter writer, string fieldName, iTextSharp.text.Rectangle placeRect, int page, int flags) { PdfFormField field = PdfFormField.CreateSignature(writer); field.Flags = flags; field.SetWidget(placeRect, null); field.FieldName = fieldName; field.Page = page; field.MKBorderColor = iTextSharp.text.Color.BLACK; field.MKBackgroundColor = iTextSharp.text.Color.WHITE; return(field); }
public override void Draw(DrawContext drawContext) { base.Draw(drawContext); PdfFormField field = PdfFormField.CreateSignature(drawContext.GetDocument(), GetOccupiedAreaBBox()); field.SetFieldName(name); if (Lock != null) { field.Put(PdfName.Lock, this.Lock.MakeIndirect(drawContext.GetDocument()).GetPdfObject()); } field.GetWidgets()[0].SetFlag(PdfAnnotation.PRINT); field.GetWidgets()[0].SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT); PdfAcroForm.GetAcroForm(drawContext.GetDocument(), true).AddField(field); }
public void AddField(String src, String dest) { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)); // create a signature form field PdfFormField field = PdfFormField.CreateSignature(stamper.Writer); field.FieldName = SIGNAME; // set the widget properties field.SetWidget(new Rectangle(72, 732, 144, 780), PdfAnnotation.HIGHLIGHT_OUTLINE); field.Flags = PdfAnnotation.FLAGS_PRINT; // add the annotation stamper.AddAnnotation(field, 1); // close the stamper stamper.Close(); }
private void CreateSignatureField(PdfReader reader, PdfStamper stamper, string signingBlock) { if (signingBlock == null) { return; } if (!DoesSignatureFieldExist(reader, signingBlock)) { PdfFormField signatureField = PdfFormField.CreateSignature(stamper.Writer); signatureField.SetWidget(new Rectangle(100, 100, 200, 200), null); signatureField.Flags = PdfAnnotation.FLAGS_PRINT; signatureField.FieldName = signingBlock; signatureField.Page = 1; stamper.AddAnnotation(signatureField, 1); } }
protected PdfPCell CreateSignatureFieldCell(PdfWriter writer, String name, PdfDictionary Lock) { PdfPCell cell = new PdfPCell(); cell.MinimumHeight = 50; PdfFormField field = PdfFormField.CreateSignature(writer); field.FieldName = name; if (Lock != null) { field.Put(PdfName.LOCK, writer.AddToBody(Lock).IndirectReference); } field.Flags = PdfAnnotation.FLAGS_PRINT; cell.CellEvent = new MySignatureFieldEvent(field); return(cell); }
public void AddField(String src, String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); // Create a signature form field PdfSignatureFormField field = PdfFormField.CreateSignature(pdfDoc, new Rectangle(72, 632, 200, 100)); field.SetFieldName(SIGNAME); field.GetWidgets()[0].SetHighlightMode(PdfAnnotation.HIGHLIGHT_OUTLINE).SetFlags(PdfAnnotation.PRINT); PdfAcroForm.GetAcroForm(pdfDoc, true).AddField(field); pdfDoc.Close(); }
/// <summary> /// Adds a blank signature field at the specified location. /// </summary> /// <param name="pdf">The PDF.</param> /// <param name="signatureRect">The signature location.</param> /// <param name="signaturePage">the page on which the signature appears</param> /// <returns>The new PDF.</returns> private static byte[] AddBlankSignatureField(byte[] pdf, Rectangle signatureRect, int signaturePage) { var pdfReader = new PdfReader(pdf); using (var ms = new MemoryStream()) { var pdfStamper = new PdfStamper(pdfReader, ms); var signatureField = PdfFormField.CreateSignature(pdfStamper.Writer); signatureField.SetWidget(signatureRect, null); signatureField.Flags = PdfAnnotation.FLAGS_PRINT; signatureField.Put(PdfName.DA, new PdfString("/Helv 0 Tf 0 g")); signatureField.FieldName = "ClientSignature"; signatureField.Page = signaturePage; pdfStamper.AddAnnotation(signatureField, signaturePage); pdfStamper.Close(); return(ms.ToArray()); } }
public PdfFormField formField(PdfStamper stamper, int page, DesignSign design) { PdfWriter writer = stamper.Writer; PdfFormField sig = PdfFormField.CreateSignature(writer); sig.SetWidget(design.getRect(), null); sig.SetHighlighting(PdfAnnotation.HIGHLIGHT_NONE); sig.Color = new BaseColor(design.getColor().r, design.getColor().g, design.getColor().b); // sig.SetFlags(PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_PRINT); //sig.SetFieldName("sig3"); //sig.SetPage(page); // stamper.addAnnotation(sig, 1); stamper.AcroFields.SetFieldProperty("sign-kma", "textfont", design.getFont(), null); sig.SetFieldFlags(PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_PRINT); return(sig); }
public virtual void PdfA1DocWithPdfA1SignatureFieldTest() { String name = "pdfA1DocWithPdfA1SignatureField"; String fileName = destinationFolder + name + ".pdf"; String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1SignatureField.pdf"; PdfFont fontFreeSans = PdfFontFactory.CreateFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true); fontFreeSans.SetSubset(false); Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read); PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B; PdfADocument pdfDoc = new PdfADocument(new PdfWriter(fileName), conformanceLevel, new PdfOutputIntent("Custom" , "", "http://www.color.org", "sRGB IEC61966-2.1", @is)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); form.AddField(PdfFormField.CreateSignature(pdfDoc, conformanceLevel).SetFieldName("signature").SetFont(fontFreeSans ).SetFontSize(20)); pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(fileName, cmp, destinationFolder)); NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(fileName)); }
/// <summary>This is the first method to be called when using external signatures.</summary> /// <remarks> /// This is the first method to be called when using external signatures. The general sequence is: /// preClose(), getDocumentBytes() and close(). /// <p> /// <CODE>exclusionSizes</CODE> must contain at least /// the <CODE>PdfName.CONTENTS</CODE> key with the size that it will take in the /// document. Note that due to the hex string coding this size should be byte_size*2+2. /// </remarks> /// <param name="exclusionSizes"> /// Map with names and sizes to be excluded in the signature /// calculation. The key is a PdfName and the value an Integer. At least the /Contents must be present /// </param> /// <exception cref="System.IO.IOException">on error</exception> protected internal virtual void PreClose(IDictionary <PdfName, int?> exclusionSizes) { if (preClosed) { throw new PdfException(PdfException.DocumentAlreadyPreClosed); } // TODO: add mergeVerification functionality preClosed = true; PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, true); SignatureUtil sgnUtil = new SignatureUtil(document); String name = GetFieldName(); bool fieldExist = sgnUtil.DoesSignatureFieldExist(name); acroForm.SetSignatureFlags(PdfAcroForm.SIGNATURE_EXIST | PdfAcroForm.APPEND_ONLY); PdfSigFieldLockDictionary fieldLock = null; if (cryptoDictionary == null) { throw new PdfException(PdfException.NoCryptoDictionaryDefined); } cryptoDictionary.GetPdfObject().MakeIndirect(document); if (fieldExist) { PdfSignatureFormField sigField = (PdfSignatureFormField)acroForm.GetField(fieldName); sigField.Put(PdfName.V, cryptoDictionary.GetPdfObject()); fieldLock = sigField.GetSigFieldLockDictionary(); if (fieldLock == null && this.fieldLock != null) { this.fieldLock.GetPdfObject().MakeIndirect(document); sigField.Put(PdfName.Lock, this.fieldLock.GetPdfObject()); fieldLock = this.fieldLock; } sigField.Put(PdfName.P, document.GetPage(appearance.GetPageNumber()).GetPdfObject()); sigField.Put(PdfName.V, cryptoDictionary.GetPdfObject()); PdfObject obj = sigField.GetPdfObject().Get(PdfName.F); int flags = 0; if (obj != null && obj.IsNumber()) { flags = ((PdfNumber)obj).IntValue(); } flags |= PdfAnnotation.LOCKED; sigField.Put(PdfName.F, new PdfNumber(flags)); PdfDictionary ap = new PdfDictionary(); ap.Put(PdfName.N, appearance.GetAppearance().GetPdfObject()); sigField.Put(PdfName.AP, ap); sigField.SetModified(); } else { PdfWidgetAnnotation widget = new PdfWidgetAnnotation(appearance.GetPageRect()); widget.SetFlags(PdfAnnotation.PRINT | PdfAnnotation.LOCKED); PdfSignatureFormField sigField = PdfFormField.CreateSignature(document); sigField.SetFieldName(name); sigField.Put(PdfName.V, cryptoDictionary.GetPdfObject()); sigField.AddKid(widget); if (this.fieldLock != null) { this.fieldLock.GetPdfObject().MakeIndirect(document); sigField.Put(PdfName.Lock, this.fieldLock.GetPdfObject()); fieldLock = this.fieldLock; } int pagen = appearance.GetPageNumber(); widget.SetPage(document.GetPage(pagen)); PdfDictionary ap = widget.GetAppearanceDictionary(); if (ap == null) { ap = new PdfDictionary(); widget.Put(PdfName.AP, ap); } ap.Put(PdfName.N, appearance.GetAppearance().GetPdfObject()); acroForm.AddField(sigField, document.GetPage(pagen)); if (acroForm.GetPdfObject().IsIndirect()) { acroForm.SetModified(); } else { //Acroform dictionary is a Direct dictionary, //for proper flushing, catalog needs to be marked as modified document.GetCatalog().SetModified(); } } exclusionLocations = new Dictionary <PdfName, PdfLiteral>(); PdfLiteral lit = new PdfLiteral(80); exclusionLocations[PdfName.ByteRange] = lit; cryptoDictionary.Put(PdfName.ByteRange, lit); foreach (KeyValuePair <PdfName, int?> entry in exclusionSizes) { PdfName key = entry.Key; lit = new PdfLiteral((int)entry.Value); exclusionLocations[key] = lit; cryptoDictionary.Put(key, lit); } if (certificationLevel > 0) { AddDocMDP(cryptoDictionary); } if (fieldLock != null) { AddFieldMDP(cryptoDictionary, fieldLock); } if (signatureEvent != null) { signatureEvent.GetSignatureDictionary(cryptoDictionary); } if (certificationLevel > 0) { // add DocMDP entry to root PdfDictionary docmdp = new PdfDictionary(); docmdp.Put(PdfName.DocMDP, cryptoDictionary.GetPdfObject()); document.GetCatalog().Put(PdfName.Perms, docmdp); document.GetCatalog().SetModified(); } cryptoDictionary.GetPdfObject().Flush(false); document.Close(); range = new long[exclusionLocations.Count * 2]; long byteRangePosition = exclusionLocations.Get(PdfName.ByteRange).GetPosition(); exclusionLocations.JRemove(PdfName.ByteRange); int idx = 1; foreach (PdfLiteral lit1 in exclusionLocations.Values) { long n = lit1.GetPosition(); range[idx++] = n; range[idx++] = lit1.GetBytesCount() + n; } iText.IO.Util.JavaUtil.Sort(range, 1, range.Length - 1); for (int k = 3; k < range.Length - 2; k += 2) { range[k] -= range[k - 1]; } if (tempFile == null) { bout = temporaryOS.ToArray(); range[range.Length - 1] = bout.Length - range[range.Length - 2]; MemoryStream bos = new MemoryStream(); PdfOutputStream os = new PdfOutputStream(bos); os.Write('['); for (int k_1 = 0; k_1 < range.Length; ++k_1) { os.WriteLong(range[k_1]).Write(' '); } os.Write(']'); System.Array.Copy(bos.ToArray(), 0, bout, (int)byteRangePosition, bos.Length); } else { try { raf = FileUtil.GetRandomAccessFile(tempFile); long len = raf.Length; range[range.Length - 1] = len - range[range.Length - 2]; MemoryStream bos = new MemoryStream(); PdfOutputStream os = new PdfOutputStream(bos); os.Write('['); for (int k_1 = 0; k_1 < range.Length; ++k_1) { os.WriteLong(range[k_1]).Write(' '); } os.Write(']'); raf.Seek(byteRangePosition); raf.Write(bos.ToArray(), 0, (int)bos.Length); } catch (System.IO.IOException e) { try { raf.Close(); } catch (Exception) { } try { tempFile.Delete(); } catch (Exception) { } throw; } } }