private void Save_Pdf_Button_Click(object sender, RoutedEventArgs e) { { Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = Path.GetDirectoryName(SRC) + "\\" + Path.GetFileNameWithoutExtension(SRC) + " - Filled.pdf"; dlg.DefaultExt = ".pdf"; dlg.Filter = "PDF Files (*.pdf)|*.pdf"; // Show save file dialog box Nullable <bool> result = dlg.ShowDialog(); // Process save file dialog box results if (result == true) { // Save document DEST = dlg.FileName; PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); try { form.SetGenerateAppearance(true); PdfFont font = PdfFontFactory.CreateFont(); IEnumerable <TextBox> elements = FindVisualChildren <TextBox>(lbFormFields).Where(x => x.Tag != null && x.Tag.ToString() == "textBox_FieldValue"); foreach (TextBox tb in elements) { String FieldName = ((Grid)tb.Parent).Tag.ToString(); IEnumerable <TextBox> fontSizeTextBoxElements = FindVisualChildren <TextBox>(tb.Parent).Where(x => x.Tag != null && x.Tag.ToString() == FieldName); float fieldFontSize = 10f; float.TryParse(fontSizeTextBoxElements.First().Text, out fieldFontSize); form.GetField(FieldName).SetValue(tb.Text, font, fieldFontSize); } if (MessageBox.Show("PDF saved! \nPath: " + DEST + "\n\nDo you want to open the file?", "Successful", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { System.Diagnostics.Process.Start(DEST); } } catch { MessageBox.Show("An error occurred!"); } finally { pdfDoc.Close(); } } } }
public virtual void AlignmentInheritanceInFieldsTest() { String name = "alignmentInheritanceInFields"; String fileName = destinationFolder + name + ".pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + name + ".pdf"), new PdfWriter(fileName)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); form.SetGenerateAppearance(false); IDictionary <String, PdfFormField> fields = form.GetFormFields(); fields.Get("root").SetValue("Deutschland"); form.FlattenFields(); pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(fileName, sourceFolder + "cmp_" + name + ".pdf", destinationFolder + name, "diff_")); }
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 SetGenerateAppearanceTest() { PdfDocument outputDoc = CreateDocument(); PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(outputDoc, true); acroForm.SetNeedAppearances(false); acroForm.SetGenerateAppearance(true); bool isModified = acroForm.GetPdfObject().IsModified(); bool isReleaseForbidden = acroForm.GetPdfObject().IsReleaseForbidden(); bool isGenerateAppearance = acroForm.IsGenerateAppearance(); Object needAppearances = acroForm.GetPdfObject().Get(PdfName.NeedAppearances); outputDoc.Close(); NUnit.Framework.Assert.IsNull(needAppearances); NUnit.Framework.Assert.IsTrue(isGenerateAppearance); NUnit.Framework.Assert.IsTrue(isModified); NUnit.Framework.Assert.IsTrue(isReleaseForbidden); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); // This method tells 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 the results can be unexpected in Acrobat. form.SetGenerateAppearance(true); PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H); // ӧ character is used here form.GetField("Name").SetValue("\u04e711111", font, 12f); // If no fields have been explicitly included, then all fields are flattened. // Otherwise only the included fields are flattened. form.FlattenFields(); pdfDoc.Close(); }
public FileStreamResult FillForm(int id, [FromBody] PdfFormFieldCollection value) { string tempFile = GetTempFilePath(); using (PdfReader reader = new PdfReader(GetFilePath())) { reader.SetUnethicalReading(true); using (PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(tempFile))) { PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); form.SetGenerateAppearance(true); foreach (var field in value) { var pdfField = form.GetField(field.Name); pdfField.SetValue(field.Value); } pdfDoc.Close(); } } return(new FileStreamResult(new FileStream(tempFile, FileMode.Open), "application/pdf")); }
private void Fill_Open_pdf_Button_Click(object sender, RoutedEventArgs e) { // temp save document and open var filePath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); filePath = Path.Combine(filePath, "Fill-A-Doc"); System.IO.Directory.CreateDirectory(filePath); DEST = filePath + "\\temp.pdf";; PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); try { form.SetGenerateAppearance(true); PdfFont font = PdfFontFactory.CreateFont(); IEnumerable <TextBox> elements = FindVisualChildren <TextBox>(lbFormFields).Where(x => x.Tag != null && x.Tag.ToString() == "textBox_FieldValue"); foreach (TextBox tb in elements) { String FieldName = ((Grid)tb.Parent).Tag.ToString(); IEnumerable <TextBox> fontSizeTextBoxElements = FindVisualChildren <TextBox>(tb.Parent).Where(x => x.Tag != null && x.Tag.ToString() == FieldName); float fieldFontSize = 10f; float.TryParse(fontSizeTextBoxElements.First().Text, out fieldFontSize); form.GetField(FieldName).SetValue(tb.Text, font, fieldFontSize); } System.Diagnostics.Process.Start(DEST); } catch { MessageBox.Show("An error occurred!"); } finally { pdfDoc.Close(); } }