Esempio n. 1
0
 private static void FillBase(PdfAcroField.PdfAcroFieldCollection fields, KlasseBase k)
 {
     fields["Titel"].Value         = new PdfString(k.Titel);
     fields["Beruf"].Value         = new PdfString(k.Beruf.Name);
     fields["Bewegung"].Value      = new PdfString(k.Bewegung);
     fields["Stufe"].Value         = new PdfString(k.Stufe.ToString());
     fields["Erfahrung"].Value     = new PdfString(k.Erfahrungspunkte);
     fields["KritWürfel"].Value    = new PdfString(k.KritWürfel);
     fields["NahSchaden"].Value    = new PdfString(k.Attribute.Stärke.Modifikator.ToString());
     fields["RK"].Value            = new PdfString(k.Rüstungsklasse.ToString());
     fields["TP"].Value            = new PdfString(k.Trefferpunkte.ToString());
     fields["Max"].Value           = new PdfString(k.Trefferpunkte.ToString());
     fields["Ini"].Value           = new PdfString(k.Initiative.ToString());
     fields["Aktionswürfel"].Value = new PdfString(k.Aktionswürfel);
     fields["Angriff"].Value       = new PdfString(k.Angriffsmodifikator);
     fields["KritTabelle"].Value   = new PdfString(k.KritTabelle);
     fields["ModStä"].Value        = new PdfString(k.Attribute.Stärke.Modifikator.ToString());
     fields["Stä"].Value           = new PdfString(k.Attribute.Stärke.Value.ToString());
     fields["ModGes"].Value        = new PdfString(k.Attribute.Geschicklichkeit.Modifikator.ToString());
     fields["Ges"].Value           = new PdfString(k.Attribute.Geschicklichkeit.Value.ToString());
     fields["Aus"].Value           = new PdfString(k.Attribute.Ausdauer.Value.ToString());
     fields["ModAus"].Value        = new PdfString(k.Attribute.Ausdauer.Modifikator.ToString());
     fields["Per"].Value           = new PdfString(k.Attribute.Persönlichkeit.Value.ToString());
     fields["ModPer"].Value        = new PdfString(k.Attribute.Persönlichkeit.Modifikator.ToString());
     fields["Glü"].Value           = new PdfString(k.Attribute.Glück.Value.ToString());
     fields["ModGlü"].Value        = new PdfString(k.Attribute.Glück.Modifikator.ToString());
     fields["Int"].Value           = new PdfString(k.Attribute.Intelligenz.Value.ToString());
     fields["ModInt"].Value        = new PdfString(k.Attribute.Intelligenz.Modifikator.ToString());
     fields["NahAngriff"].Value    = new PdfString(k.Attribute.Stärke.Modifikator.ToString());
     fields["FernSchaden"].Value   = new PdfString("");
     fields["FernAngriff"].Value   = new PdfString(k.Attribute.Geschicklichkeit.Modifikator.ToString());
     fields["RefRW"].Value         = new PdfString(k.Reflexe.ToString());
     fields["ZähRW"].Value         = new PdfString(k.Zähigkeit.ToString());
     fields["WillRW"].Value        = new PdfString(k.Willenskraft.ToString());
     fields["Glückswurf"].Value    = new PdfString(k.Attribute.Glück.Modifikator.ToString());
     fields["Sprachen"].Value      = new PdfString(string.Join('\n', k.Sprachen));
     fields["Gesinnung"].Value     = new PdfString(k.Gesinnung.ToString());
     fields["Waffen"].Value        = new PdfString($"{k.Beruf.Startwaffe} ({k.Beruf.Schaden})");
     fields["Ausrüstung"].Value    = new PdfString($"{k.Beruf.Handelsware}\n{k.Ausrüstung.Gegenstand} ({k.Ausrüstung.Preis})");
     fields["Schätze"].Value       = new PdfString(k.Startkapital);
     if (fields["Notizen"] != null)
     {
         fields["Notizen"].Value = new PdfString($"{k.Geburtszeichen.Name}: {k.Geburtszeichen.Schicksalswurf} ({k.Geburtszeichen.Bonus})");
     }
 }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // Get a fresh copy of a sample PDF form file
            string filename;

            filename = "fw4.pdf";
            //filename = "fss4.pdf";
            //filename = "fw9.pdf";
            //filename = "f8822.pdf";

            while (!File.Exists(Path.Combine("d:/WImageTest/PDFs", filename)))
            {
                if (DialogResult.Yes ==
                    MessageBox.Show("This sample needs a PDF form that is not included with this release.\n" +
                                    "Please download the Form W-4 from http://www.irs.gov" +
                                    "\nSelect Yes to open this URL in your browser.\n" +
                                    "Select No to exit this sample now.",
                                    "PDFsharp FillFormFields sample",
                                    MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question))
                {
                    Process.Start("../../SampleForms/ReadMe.txt");
                    Process.Start("http://www.irs.gov/");
                    MessageBox.Show("Close this dialog to continue the sample after downloading the W-4 form to the appropriate folder.",
                                    "PDFsharp FillFormFields sample");
                }
                else
                {
                    return;
                }
            }

            File.Copy(Path.Combine("../../../../PDFs", filename),
                      Path.Combine(Directory.GetCurrentDirectory(), filename), true);

            // Open the file
            PdfDocument document = PdfReader.Open(filename, PdfDocumentOpenMode.Modify);

            // Get the root object of all interactive form fields
            PdfAcroForm form = document.AcroForm;

            // Get all form fields of the whole document
            PdfAcroField.PdfAcroFieldCollection fields = form.Fields;

            // Get all form fields of the whole document
            string[] names = fields.Names;
            names = fields.DescendantNames;

            // Fill some value in each field
            for (int idx = 0; idx < names.Length; idx++)
            {
                string       fqName = names[idx];
                PdfAcroField field  = fields[fqName];

                PdfTextField        txtField;
                PdfRadioButtonField radField;
                PdfCheckBoxField    chkField;
                PdfListBoxField     lbxField;
                PdfComboBoxField    cbxField;
                PdfGenericField     genField;

                if ((txtField = field as PdfTextField) != null)
                {
                    //
                    txtField.Text = "Hello";
                }
                else if ((radField = field as PdfRadioButtonField) != null)
                {
                    radField.SelectedIndex = 0;
                }
                else if ((chkField = field as PdfCheckBoxField) != null)
                {
                    chkField.Checked = idx % 2 == 0;
                }
                else if ((lbxField = field as PdfListBoxField) != null)
                {
                    lbxField.SelectedIndex = 0;
                }
                else if ((cbxField = field as PdfComboBoxField) != null)
                {
                    cbxField.SelectedIndex = 0;
                }
                else if ((genField = field as PdfGenericField) != null)
                {
                }
            }

            // Save the document...
            document.Save(filename);
            // ...and start a viewer.
            Process.Start(filename);
        }
 private void FixAcroFields(PdfAcroField.PdfAcroFieldCollection formFields, PdfImportedObjectTable importedObjectTable)
 {
     for (var i = 0; i < formFields.Elements.Count; i++)
     {
         var field = formFields[i];
         if (field.Owner != _document)
         {
             field._document = _document;
         }
         field.Reference.Document = _document;
         if (field.ObjectID.IsEmpty || !_document._irefTable.Contains(field.ObjectID))
         {
             _document._irefTable.Add(field);
         }
         //// fix the /P entry for Field-Annotations
         //var fieldPage = field.Elements.GetDictionary(PdfAcroField.Keys.Page);
         //if (fieldPage != null && fieldPage.Owner != _document)
         //{
         //    var importedPage = importedObjectTable.Contains(fieldPage.ObjectID) ? importedObjectTable[fieldPage.ObjectID] : null;
         //    if (importedPage != null)
         //    {
         //        field.Elements.SetReference(PdfAcroField.Keys.Page, importedPage.Value);
         //        Debug.WriteLine(String.Format("Fixed page of '{0}' ({1}) -> {2} = {3}", field.FullyQualifiedName, field.ObjectID, fieldPage.ObjectID, importedPage.ObjectID));
         //    }
         //    else
         //        Debug.WriteLine(String.Format("Can't fix page of '{0}' ({1}), imported page not found", field.FullyQualifiedName, field.ObjectID));
         //}
         // Annotations are "partly" imported, we need to fix the /P entry
         for (var a = 0; a < field.Annotations.Elements.Count; a++)
         {
             var widget = field.Annotations.Elements[a];
             if (widget != null)
             {
                 // the owner has to be fixed as well...
                 // Note: it was observed that some objects referenced by the widget were still owned by the imported document, but that seems to be fixed on saving...
                 if (widget.Owner != _document)
                 {
                     if (_document._irefTable.Contains(widget.ObjectID))
                     {
                         widget._document          = _document;
                         widget.Reference.Document = _document;
                         if (!_document._irefTable.Contains(widget.ObjectID))
                         {
                             _document._irefTable.Add(widget);
                         }
                     }
                     else
                     {
                         // this was never needed during debugging, we leave it here just in case...
                         var importedWidget = ImportClosure(importedObjectTable, _document, widget) as PdfDictionary;
                         if (importedWidget != null)
                         {
                             widget = new PdfWidgetAnnotation(importedWidget);
                         }
                     }
                     FixDocumentRef(widget, importedObjectTable);
                 }
                 var widgetPage = widget.Page;
                 if (widgetPage != null && widgetPage.Owner != _document)
                 {
                     var importedPage = importedObjectTable.Contains(widgetPage.ObjectID) ? importedObjectTable[widgetPage.ObjectID] : null;
                     if (importedPage != null)
                     {
                         widget.Elements.SetReference(PdfAnnotation.Keys.Page, importedPage.Value);
                         Debug.WriteLine(String.Format("Fixed page of Widget '{0}' ({1}) -> {2} = {3}", field.FullyQualifiedName, field.ObjectID, widgetPage.ObjectID, importedPage.ObjectID));
                         var ip = importedPage.Value as PdfPage;
                         // the widget is a PdfWidgetAnnotation, but the "real" object may be something else (e.g. a PdfGenericField), so we check the referenced object instead
                         if (ip != null && !ip.Annotations.Elements.Contains(widget.Reference))
                         {
                             ip.Annotations.Elements.Add(widget.Reference.Value);
                         }
                     }
                 }
                 else if (widgetPage is PdfPage)
                 {
                     // add widget to the pages' annotations if not already present
                     if (!((PdfPage)widgetPage).Annotations.Elements.Contains(widget.Reference))
                     {
                         ((PdfPage)widgetPage).Annotations.Elements.Add(widget);
                     }
                 }
             }
         }
         if (field.HasKids)
         {
             FixAcroFields(field.Fields, importedObjectTable);
         }
     }
 }
Esempio n. 4
0
        static void Main(string[] args)
        {
            // Set the name of the filled file
            string filename;

            //filename = "fw4 (Acrobat 4).pdf";
            //filename = "fw4 (Acrobat 5).pdf";
            //filename = "fw4 (Acrobat 6).pdf";
            filename = "fw4 (Acrobat 7).pdf";
            filename = Path.Combine("../../FilledForms", filename);

            // Open the file
            PdfDocument document = PdfReader.Open(filename, PdfDocumentOpenMode.ReadOnly);

            // Get the root object of all interactive form fields
            PdfAcroForm form = document.AcroForm;

            // Get all form fields of the whole document
            PdfAcroField.PdfAcroFieldCollection fields = form.Fields;

            // Get all form fields of the whole document
            string[] names = fields.Names;
            names = fields.DescendantNames;

            // Read the value of each field
            for (int idx = 0; idx < names.Length; idx++)
            {
                string       fqName = names[idx];
                PdfAcroField field  = fields[fqName];

                PdfTextField        txtField;
                PdfRadioButtonField radField;
                PdfCheckBoxField    chkField;
                PdfListBoxField     lbxField;
                PdfComboBoxField    cbxField;
                PdfGenericField     genField;

                if ((txtField = field as PdfTextField) != null)
                {
                    Console.WriteLine(String.Format("text field '{0}': '{1}'", txtField.Name, txtField.Text));
                }
                else if ((radField = field as PdfRadioButtonField) != null)
                {
                    Console.WriteLine(String.Format("radio button '{0}': '{1}'", radField.Name, radField.Value));
                }
                else if ((chkField = field as PdfCheckBoxField) != null)
                {
                    Console.WriteLine(String.Format("radio button '{0}': '{1}'", chkField.Name, chkField.Value));
                }
                else if ((lbxField = field as PdfListBoxField) != null)
                {
                    Console.WriteLine(String.Format("radio button '{0}': '{1}'", lbxField.Name, lbxField.Value));
                }
                else if ((cbxField = field as PdfComboBoxField) != null)
                {
                    Console.WriteLine(String.Format("radio button '{0}': '{1}'", cbxField.Name, cbxField.Value));
                }
                else if ((genField = field as PdfGenericField) != null)
                {
                    Console.WriteLine(String.Format("radio button '{0}': '{1}'", genField.Name, genField.Value));
                }
            }

            // Wait for Return
            Console.ReadLine();
        }