Пример #1
0
// ---------------------------------------------------------------------------

        /**
         * Manipulates a PDF file src with the file dest as result
         * @param src the original PDF
         */
        public byte[] ManipulatePdf(byte[] src)
        {
            PdfReader reader = new PdfReader(src);

            using (MemoryStream ms = new MemoryStream()) {
                using (PdfStamper stamper = new PdfStamper(reader, ms)) {
                    AcroFields form = stamper.AcroFields;
                    form.SetField("choice_1", "NL");
                    form.SetListSelection("choice_2", new String[] { "German", "Spanish" });
                    String[] languages        = form.GetListOptionDisplay("choice_3");
                    String[] exportvalues     = form.GetListOptionExport("choice_3");
                    int      n                = languages.Length;
                    String[] new_languages    = new String[n + 2];
                    String[] new_exportvalues = new String[n + 2];
                    for (int i = 0; i < n; i++)
                    {
                        new_languages[i]    = languages[i];
                        new_exportvalues[i] = exportvalues[i];
                    }
                    new_languages[n]        = "Chinese";
                    new_exportvalues[n]     = "CN";
                    new_languages[n + 1]    = "Japanese";
                    new_exportvalues[n + 1] = "JP";
                    form.SetListOption("choice_3", new_exportvalues, new_languages);
                    form.SetField("choice_3", "CN");
                    form.SetField("choice_4", "Japanese");
                }
                return(ms.ToArray());
            }
        }
Пример #2
0
        public static byte[] fill_template(byte[] templateFile, List <FormElement> elements)
        {
            using (MemoryStream output = new MemoryStream())
                using (PdfReader pdfReader = new PdfReader(templateFile))
                    using (PdfStamper stamper = new PdfStamper(pdfReader, output))
                    {
                        //without this line of code, farsi fonts would not be visible
                        stamper.AcroFields.AddSubstitutionFont(GetFont().BaseFont);

                        AcroFields form = stamper.AcroFields;
                        form.GenerateAppearances = true;

                        elements.Where(e => !string.IsNullOrEmpty(e.Name) && form.Fields.ContainsKey(e.Name))
                        .ToList().ForEach(e =>
                        {
                            switch (form.GetFieldType(e.Name))
                            {
                            case AcroFields.FIELD_TYPE_TEXT:
                            case AcroFields.FIELD_TYPE_COMBO:
                            case AcroFields.FIELD_TYPE_RADIOBUTTON:
                                form.SetField(e.Name, e.Type == FormElementTypes.Numeric ? e.FloatValue.ToString() : e.TextValue);
                                break;

                            case AcroFields.FIELD_TYPE_CHECKBOX:
                                form.SetField(e.Name, e.BitValue.HasValue && e.BitValue.Value ? "Yes" : "Off");
                                break;

                            case AcroFields.FIELD_TYPE_LIST:
                                form.SetListOption(e.Name,
                                                   e.TextValue.Split('~').Select(t => t.Trim()).Where(t => !string.IsNullOrEmpty(t)).ToArray(), null);
                                form.SetField(e.Name, null);
                                break;

                            case AcroFields.FIELD_TYPE_PUSHBUTTON:
                                break;

                            case AcroFields.FIELD_TYPE_SIGNATURE:
                                break;
                            }
                        });

                        //by doing this, the form will not be editable anymore
                        stamper.FormFlattening = true;

                        //to make a specific field not editable
                        //stamper.PartialFormFlattening("[field_name]");

                        stamper.Close();
                        pdfReader.Close();

                        return(output.GetBuffer());
                    }
        }
Пример #3
0
        public static byte[] SetFields(byte[] pdfFile, IFormContentProvider contentProvider)
        {
            var        pr   = new PdfReader(pdfFile);
            var        ims  = new MemoryStream();
            var        ps   = new PdfStamper(pr, ims);
            AcroFields flds = ps.AcroFields;

            foreach (DictionaryEntry entry in flds.Fields)
            {
                string fieldName = entry.Key.ToString();
                var    type      = (FieldTypes)flds.GetFieldType(fieldName);
                switch (type)
                {
                case FieldTypes.Text:
                    string value   = null;
                    string display = null;
                    contentProvider.PopulateTextField(fieldName, out value, out display);
                    if (value != null)
                    {
                        flds.SetField(fieldName, value);
                    }
                    break;

                case FieldTypes.List:
                    string[] exportValues  = null;
                    string[] displayValues = null;
                    string[] selected      = null;
                    contentProvider.PopulateListField(fieldName, out exportValues, out displayValues, out selected);
                    if (exportValues != null)
                    {
                        flds.SetListOption(fieldName, exportValues, displayValues);
                    }
                    if (selected != null)
                    {
                        flds.SetListSelection(fieldName, selected);
                    }
                    break;

                default:
                    break;
                }
            }
            ps.Close();
            return(ims.ToArray());
        }