protected void ManipulatePdf(String dest) { // CreateForm() method creates a temporary document in the memory, // which then will be used as a source while writing to a real document byte[] content = CreateForm(); IRandomAccessSource source = new RandomAccessSourceFactory().CreateSource(content); PdfDocument pdfDoc = new PdfDocument(new PdfReader(source, new ReaderProperties()), new PdfWriter(dest)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); // Set a flag specifying whether to construct appearance streams and appearance dictionaries // for all widget annotations in the document. form.SetNeedAppearances(true); form.GetField("text1") // Method sets the flag, specifying whether or not the field can be changed. .SetReadOnly(true) .SetValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"); form.GetField("text2") .SetReadOnly(true) .SetValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"); form.GetField("text3") .SetReadOnly(true) .SetValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"); form.GetField("text4") .SetReadOnly(true) .SetValue("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"); pdfDoc.Close(); }
public virtual void CopyAndEditTextFields() { //TODO: update after DEVSIX-2354 String srcFileName = sourceFolder + "/checkPdfFormCopy_Source.pdf"; String destFilename = destinationFolder + "copyAndEditTextFields.pdf"; String cmpFileName = sourceFolder + "/cmp_copyAndEditTextFields.pdf"; PdfDocument srcDoc = new PdfDocument(new PdfReader(srcFileName)); PdfDocument destDoc = new PdfDocument(new PdfWriter(destFilename)); PdfPageFormCopier pdfPageFormCopier = new PdfPageFormCopier(); for (int i = 0; i < 4; i++) { srcDoc.CopyPagesTo(1, 1, destDoc, pdfPageFormCopier); } PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(destDoc, false); acroForm.GetField("text_1").SetValue("text_1"); acroForm.GetField("NumberField_text.2").SetValue("-100.00"); acroForm.GetField("NumberField_text.2_1").SetValue("3.00"); acroForm.GetField("text.3_1<!").SetValue("text.3_1<!"); acroForm.GetField("text.4___#1+1").SetValue("CHANGEDtext"); destDoc.Close(); srcDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destFilename, cmpFileName, destinationFolder , "diff_")); }
/// <summary>Sets the name indicating the field to be signed.</summary> /// <remarks> /// Sets the name indicating the field to be signed. The field can already be presented in the /// document but shall not be signed. If the field is not presented in the document, it will be created. /// </remarks> /// <param name="fieldName">The name indicating the field to be signed.</param> public virtual void SetFieldName(String fieldName) { if (fieldName != null) { if (fieldName.IndexOf('.') >= 0) { throw new ArgumentException(PdfException.FieldNamesCannotContainADot); } PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, true); if (acroForm.GetField(fieldName) != null) { PdfFormField field = acroForm.GetField(fieldName); if (!PdfName.Sig.Equals(field.GetFormType())) { throw new ArgumentException(PdfException.FieldTypeIsNotASignatureFieldType); } if (field.GetValue() != null) { throw new ArgumentException(PdfException.FieldAlreadySigned); } appearance.SetFieldName(fieldName); IList <PdfWidgetAnnotation> widgets = field.GetWidgets(); if (widgets.Count > 0) { PdfWidgetAnnotation widget = widgets[0]; appearance.SetPageRect(GetWidgetRectangle(widget)); appearance.SetPageNumber(GetWidgetPageNumber(widget)); } } this.fieldName = fieldName; } }
public PdfFormField GetMatchingFormField(PdfAcroForm form, PropertyInfo property) { var field = form.GetField(property.Name); if (field != null) { return(field); } var attributes = property.GetCustomAttributes(true); foreach (var attribute in attributes) { if (attribute is PdfFormFieldAttribute pdfAttribute) { field = form.GetField(pdfAttribute.FieldName); if (field != null) { return(field); } } } return(null); }
public void generatePDF() { PdfDocument pdfDocument = new PdfDocument(new PdfReader(srcPdf), new PdfWriter(outPdf)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, false); form.GetField("First").SetValue("First"); form.GetField("Second").SetValue("Second"); pdfDocument.Close(); }
public IActionResult OnPost() { if (!ModelState.IsValid) { return(Page()); } //read src file ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfDocument pdfDoc = new PdfDocument(new PdfReader(Path.GetFullPath(SRC)), new PdfWriter(baos)); //genetate pdf form fields PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); PropertyInfo[] properties = Pdf.GetType().GetProperties(); foreach (PropertyInfo property in properties) { if (property.GetValue(Pdf, null) == null) { continue; } //field name string name = property.Name; //field value string value = ""; string propertyValue = property.GetValue(Pdf, null).ToString(); switch (property.Name) { case "Dob": //date of birth value = DateTime.Parse(propertyValue).ToString("dd/MMM/yyyy"); break; case "Restatus": //relationship status name = RelationshipStatus(propertyValue); value = "yes"; form.GetField(name).SetCheckType(1); break; default: value = propertyValue; break; } //set field name form.GetField(name).SetValue(value); } //flat form form.FlattenFields(); pdfDoc.Close(); return(File(baos.ToArray(), "application/pdf", "result.pdf")); }
public void FillOut(String src, String dest, String name, String value) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest), new StampingProperties().UseAppendMode()); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); form.GetField(name).SetValue(value); form.GetField(name).SetReadOnly(true); pdfDoc.Close(); }
public static void FillPdf(string src, string dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); //here is where the bug occurs, when the razor runtime compliation library is installed //remove the razor runtime compilation package, and the next line will pass form.GetField("First Name").SetValue("Viktor"); form.GetField("Last Name").SetValue("Jakovlev"); pdfDoc.Close(); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); // The second parameter sets how the field's value will be displayed in the resultant pdf. // If the second parameter is null, then actual value will be shown. form.GetField("Name").SetValue("1.0", "100%"); form.GetField("Company").SetValue("1217000.000000", "$1,217,000"); pdfDoc.Close(); }
protected void ManipulatePdf(String dest) { PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC)); PdfAcroForm form = PdfAcroForm.GetAcroForm(srcDoc, false); PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); // Event handler copies content of the source pdf file on every page // of the result pdf file pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, new PaginationEventHandler(srcDoc.GetFirstPage().CopyAsFormXObject(pdfDoc))); srcDoc.Close(); Document doc = new Document(pdfDoc); Rectangle rect = form.GetField("body").GetWidgets()[0].GetRectangle().ToRectangle(); // The renderer will place content in columns specified with the rectangles doc.SetRenderer(new ColumnDocumentRenderer(doc, new Rectangle[] { rect })); Paragraph p = new Paragraph(); // The easiest way to add a Text object to Paragraph p.Add("Hello "); // Use add(Text) if you want to specify some Text characteristics, for example, font size p.Add(new Text("World").SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD))); for (int i = 1; i < 101; i++) { doc.Add(new Paragraph("Hello " + i)); doc.Add(p); } doc.Close(); }
private PdfFormField MergeFieldsWithTheSameName(PdfFormField existingField, PdfFormField newField) { String fieldName = newField.GetFieldName().ToUnicodeString(); existingField.GetPdfObject().Remove(PdfName.T); PdfFormField mergedField = formTo.GetField(fieldName); PdfArray kids = mergedField.GetKids(); if (kids != null && !kids.IsEmpty()) { mergedField.AddKid(existingField); return(mergedField); } newField.GetPdfObject().Remove(PdfName.T); mergedField = PdfFormField.CreateEmptyField(documentTo); formTo.GetFields().Remove(newField.GetPdfObject()); mergedField.Put(PdfName.FT, existingField.GetFormType()).Put(PdfName.T, new PdfString(fieldName)); PdfDictionary parent = existingField.GetParent(); if (parent != null) { mergedField.Put(PdfName.Parent, parent); } kids = existingField.GetKids(); if (kids != null) { mergedField.Put(PdfName.Kids, kids); } mergedField.AddKid(existingField).AddKid(newField); return(mergedField); }
public virtual void CopyFieldsTest04() { String srcFilename = sourceFolder + "srcFile1.pdf"; PdfDocument srcDoc = new PdfDocument(new PdfReader(srcFilename)); PdfDocument destDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream())); PdfPageFormCopier formCopier = new PdfPageFormCopier(); srcDoc.CopyPagesTo(1, srcDoc.GetNumberOfPages(), destDoc, formCopier); srcDoc.CopyPagesTo(1, srcDoc.GetNumberOfPages(), destDoc, formCopier); PdfAcroForm form = PdfAcroForm.GetAcroForm(destDoc, false); NUnit.Framework.Assert.AreEqual(1, form.GetFields().Size()); NUnit.Framework.Assert.IsNotNull(form.GetField("Name1")); NUnit.Framework.Assert.IsNotNull(form.GetField("Name1.1")); destDoc.Close(); }
private static void AssertAppearanceFontSize(String filename, float expectedFontSize) { PdfDocument pdfDocument = new PdfDocument(new PdfReader(filename)); PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(pdfDocument, false); PdfStream stream = acroForm.GetField("Signature1").GetWidgets()[0].GetNormalAppearanceObject().GetAsDictionary (PdfName.Resources).GetAsDictionary(PdfName.XObject).GetAsStream(new PdfName("FRM")).GetAsDictionary(PdfName .Resources).GetAsDictionary(PdfName.XObject).GetAsStream(new PdfName("n2")); String[] streamContents = iText.IO.Util.StringUtil.Split(iText.IO.Util.JavaUtil.GetStringForBytes(stream.GetBytes ()), "\\s"); String fontSize = null; for (int i = 1; i < streamContents.Length; i++) { if ("Tf".Equals(streamContents[i])) { fontSize = streamContents[i - 1]; break; } } float foundFontSize = float.Parse(fontSize, System.Globalization.CultureInfo.InvariantCulture); NUnit.Framework.Assert.IsTrue(Math.Abs(foundFontSize - expectedFontSize) < 0.1 * expectedFontSize, MessageFormatUtil .Format("Font size: exptected {0}, found {1}", expectedFontSize, fontSize)); }
public virtual void ManipulatePdf(String src, String dest) { //Initialize PDF document PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); //Add text annotation PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(400, 795, 0, 0)) .SetOpen(true) .SetTitle(new PdfString("iText")) .SetContents("Please, fill out the form."); pdfDoc.GetFirstPage().AddAnnotation(ann); PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage()); canvas.BeginText().SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 12).MoveText(265, 597 ).ShowText("I agree to the terms and conditions.").EndText(); //Add form field PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); PdfButtonFormField checkField = PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(245, 594, 15, 15), "agreement" , "Off", PdfFormField.TYPE_CHECK); checkField.SetRequired(true); form.AddField(checkField); //Update reset button form.GetField("reset").SetAction(PdfAction.CreateResetForm(new String[] { "name", "language", "experience1" , "experience2", "experience3", "shift", "info", "agreement" }, 0)); pdfDoc.Close(); }
/* (non-Javadoc) * @see com.itextpdf.html2pdf.attach.impl.layout.form.renderer.AbstractFormFieldRenderer#applyAcroField(com.itextpdf.layout.renderer.DrawContext) */ protected internal override void ApplyAcroField(DrawContext drawContext) { PdfDocument doc = drawContext.GetDocument(); PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, true); Rectangle area = flatRenderer.GetOccupiedArea().GetBBox().Clone(); PdfPage page = doc.GetPage(occupiedArea.GetPageNumber()); String groupName = this.GetProperty <String>(Html2PdfProperty.FORM_FIELD_VALUE); PdfButtonFormField radioGroup = (PdfButtonFormField)form.GetField(groupName); bool addNew = false; if (null == radioGroup) { radioGroup = PdfFormField.CreateRadioGroup(doc, groupName, "on"); addNew = true; } if (IsBoxChecked()) { radioGroup.SetValue(GetModelId()); } PdfFormField radio = PdfFormField.CreateRadioButton(doc, area, radioGroup, GetModelId()); radio.SetCheckType(PdfFormField.TYPE_CIRCLE); if (addNew) { form.AddField(radioGroup, page); } else { form.ReplaceField(GetModelId(), radioGroup); } WriteAcroFormFieldLangAttribute(doc); }
public virtual void FontsResourcesHelvFontTest() { String filename = "fontsResourcesHelvFontTest.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "drWithHelv.pdf"), new PdfWriter(destinationFolder + filename)); PdfFont font = PdfFontFactory.CreateFont(sourceFolder + "NotoSans-Regular.ttf", PdfEncodings.IDENTITY_H); font.SetSubset(false); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); form.GetField("description").SetValue(TEXT, font, 12f); pdfDoc.Close(); PdfDocument document = new PdfDocument(new PdfReader(destinationFolder + filename)); PdfDictionary actualDocumentFonts = PdfAcroForm.GetAcroForm(document, false).GetPdfObject().GetAsDictionary (PdfName.DR).GetAsDictionary(PdfName.Font); // Note that we know the structure of the expected pdf file PdfString expectedFieldsDAFont = new PdfString("/F2 12 Tf"); PdfObject actualFieldDAFont = document.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.AcroForm).GetAsArray (PdfName.Fields).GetAsDictionary(0).Get(PdfName.DA); NUnit.Framework.Assert.AreEqual(new PdfName("Helvetica"), actualDocumentFonts.GetAsDictionary(new PdfName( "F1")).Get(PdfName.BaseFont), "There is no Helvetica font within DR key"); NUnit.Framework.Assert.AreEqual(new PdfName("NotoSans"), actualDocumentFonts.GetAsDictionary(new PdfName("F2" )).Get(PdfName.BaseFont), "There is no NotoSans font within DR key."); NUnit.Framework.Assert.AreEqual(expectedFieldsDAFont, actualFieldDAFont, "There is no NotoSans(/F2) font within Fields DA key" ); document.Close(); ExtendedITextTest.PrintOutputPdfNameAndDir(destinationFolder + filename); }
public virtual void XfaExternalConnectionTest() { String inFileName = sourceFolder + "xfaExternalConnection.pdf"; String outFileName = destinationFolder + "outXfaExternalConnection.pdf"; String cmpFileName = sourceFolder + "cmp_outXfaExternalConnection.pdf"; #if NETSTANDARD2_0 using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(inFileName), new PdfWriter(outFileName).SetCompressionLevel (CompressionConstants.NO_COMPRESSION))) { NUnit.Framework.Assert.That(() => { // the line below is expected to produce an exception PdfAcroForm.GetAcroForm(pdfDoc, true); } , NUnit.Framework.Throws.InstanceOf <XmlException>().With.Message.EqualTo("Reference to undeclared entity 'xxe'. Line 124, position 64.")); } #endif #if !NETSTANDARD2_0 using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(inFileName), new PdfWriter(outFileName).SetCompressionLevel (CompressionConstants.NO_COMPRESSION))) { PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); // server fills out a field form.GetField("TestField").SetValue("testvalue"); form.GetXfaForm().Write(pdfDoc); } NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder )); #endif }
public override void Draw(DrawContext drawContext) { base.Draw(drawContext); if (!IsFlatten()) { String value = GetDefaultValue(); String name = GetModelId(); UnitValue fontSize = (UnitValue)this.GetPropertyAsUnitValue(Property.FONT_SIZE); if (!fontSize.IsPointValue()) { fontSize = UnitValue.CreatePointValue(DEFAULT_FONT_SIZE); } PdfDocument doc = drawContext.GetDocument(); Rectangle area = GetOccupiedArea().GetBBox().Clone(); ApplyMargins(area, false); PdfPage page = doc.GetPage(occupiedArea.GetPageNumber()); PdfButtonFormField button = PdfFormField.CreatePushButton(doc, area, name, value, doc.GetDefaultFont(), fontSize .GetValue()); button.GetWidgets()[0].SetHighlightMode(PdfAnnotation.HIGHLIGHT_NONE); button.SetBorderWidth(0); button.SetBackgroundColor(null); TransparentColor color = GetPropertyAsTransparentColor(Property.FONT_COLOR); if (color != null) { button.SetColor(color.GetColor()); } PdfAcroForm forms = PdfAcroForm.GetAcroForm(doc, true); //Add fields only if it isn't already added. This can happen on split. if (forms.GetField(name) == null) { forms.AddField(button, page); } } }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); // Being set as true, this parameter is responsible to generate an appearance Stream // while flattening for all form fields that don't have one. Generating appearances will // slow down form flattening, but otherwise Acrobat might render the pdf on its own rules. form.SetGenerateAppearance(true); PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H); form.GetField("test").SetValue(VALUE, font, 12f); form.GetField("test2").SetValue(VALUE, font, 12f); pdfDoc.Close(); }
public virtual void DefaultAppearanceExtractionForNotMergedFieldsTest() { PdfDocument doc = new PdfDocument(new PdfReader(sourceFolder + "sourceDAExtractionTest.pdf"), new PdfWriter (destinationFolder + "defaultAppearanceExtractionTest.pdf")); PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, false); form.GetField("First field").SetValue("Your name"); form.GetField("Text1").SetValue("Your surname"); doc.Close(); CompareTool compareTool = new CompareTool(); String errorMessage = compareTool.CompareByContent(destinationFolder + "defaultAppearanceExtractionTest.pdf" , sourceFolder + "cmp_defaultAppearanceExtractionTest.pdf", destinationFolder, "diff_"); if (errorMessage != null) { NUnit.Framework.Assert.Fail(errorMessage); } }
public virtual void EncryptedDocumentWithFormFields() { PdfReader reader = new PdfReader(sourceFolder + "encryptedDocumentWithFormFields.pdf", new ReaderProperties ().SetPassword("12345".GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1))); PdfDocument pdfDocument = new PdfDocument(reader); PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(pdfDocument, false); acroForm.GetField("personal.name").GetPdfObject(); pdfDocument.Close(); }
public virtual void UnicodeFormFieldTest2() { String filename = sourceFolder + "unicodeFormFieldFile.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); String fieldName = "\u5E10\u53F71"; // 帐号1: account number 1 NUnit.Framework.Assert.IsNotNull(form.GetField(fieldName)); }
public void ManipulatePdf( String DEST, String USERNAME, String COMMENT) { // If pdf is locked or you dont know the password use below commented lines //PdfReader reader = new PdfReader(PDFSRC).SetUnethicalReading(true); //PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(dest)); PdfDocument pdfDoc = new PdfDocument(new PdfReader(PDFSRC), new PdfWriter(DEST)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); form.GetField("userid").SetValue(" " + USERNAME); form.GetField("reqdate").SetValue("20201015"); // form.GetField("justify").SetValue(COMMENT); form.GetField("optinfo").SetValue(COMMENT); pdfDoc.Close(); }
public virtual void EncryptedDocumentWithFormFields() { PdfReader reader = new PdfReader(new FileStream(sourceFolder + "encryptedDocumentWithFormFields.pdf", FileMode.Open , FileAccess.Read), new ReaderProperties().SetPassword("12345".GetBytes())); PdfDocument pdfDocument = new PdfDocument(reader); PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(pdfDocument, false); acroForm.GetField("personal.name").GetPdfObject(); pdfDocument.Close(); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); PdfFormField field = form.GetField("Name"); PdfWidgetAnnotation widgetAnnotation = field.GetWidgets()[0]; PdfArray annotationRect = widgetAnnotation.GetRectangle(); // Change value of the right coordinate (index 2 corresponds with right coordinate) annotationRect.Set(2, new PdfNumber(annotationRect.GetAsNumber(2).FloatValue() + 20f)); String value = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; field.SetValue(value); form.GetField("Company").SetValue(value); pdfDoc.Close(); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); // Set the visibility flag of the form field annotation. // Options are: HIDDEN, HIDDEN_BUT_PRINTABLE, VISIBLE, VISIBLE_BUT_DOES_NOT_PRINT form.GetField("Test").SetVisibility(PdfFormField.HIDDEN); pdfDoc.Close(); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); form.GetField("name").SetValue("CALIFORNIA"); form.GetField("abbr").SetValue("CA"); form.GetField("capital").SetValue("Sacramento"); form.GetField("city").SetValue("Los Angeles"); form.GetField("population").SetValue("36,961,664"); form.GetField("surface").SetValue("163,707"); form.GetField("timezone1").SetValue("PT (UTC-8)"); form.GetField("timezone2").SetValue("-"); form.GetField("dst").SetValue("YES"); pdfDoc.Close(); }
/// <summary>Gets a new signature field name that doesn't clash with any existing name.</summary> /// <returns>A new signature field name.</returns> public virtual String GetNewSigFieldName() { PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, true); String name = "Signature"; int step = 1; while (acroForm.GetField(name + step) != null) { ++step; } return(name + step); }
public virtual void NoWarningOnValueNotOfOptComboEditTest() { String srcPdf = sourceFolder + "noWarningOnValueNotOfOptComboEditTest.pdf"; String outPdf = destinationFolder + "noWarningOnValueNotOfOptComboEditTest.pdf"; String cmpPdf = sourceFolder + "cmp_noWarningOnValueNotOfOptComboEditTest.pdf"; PdfDocument pdfDocument = new PdfDocument(new PdfReader(srcPdf), new PdfWriter(outPdf)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, false); form.GetField("First").SetValue("Value not of /Opt array"); pdfDocument.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder)); }
public void FillOutAndSign(String keystore, String src, String name, String fname, String value, String dest) { Pkcs12Store pk12 = new Pkcs12Store(new FileStream(keystore, FileMode.Open, FileAccess.Read), PASSWORD); string alias = null; foreach (var a in pk12.Aliases) { alias = ((string)a); if (pk12.IsKeyEntry(alias)) { break; } } ICipherParameters pk = pk12.GetKey(alias).Key; X509CertificateEntry[] ce = pk12.GetCertificateChain(alias); X509Certificate[] chain = new X509Certificate[ce.Length]; for (int k = 0; k < ce.Length; ++k) { chain[k] = ce[k].Certificate; } PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties().UseAppendMode()); signer.SetFieldName(name); PdfAcroForm form = PdfAcroForm.GetAcroForm(signer.GetDocument(), true); form.GetField(fname).SetValue(value); form.GetField(name).SetReadOnly(true); form.GetField(fname).SetReadOnly(true); PrivateKeySignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256); signer.SignDetached(pks, chain, null, null, null, 0, PdfSigner.CryptoStandard.CMS); }