开发者

Getting data from a PDF using C#

I have a preexisting PDF with seve开发者_Go百科ral drop down combo boxes. I would like to be able to get the list of options from these combo boxes in a C# program.

I have looked at iText but have not been able to figure out if it can do what I want it to.

Any suggestions would be greatly appreciated. Thank you in advance!


iText[Sharp] can indeed do what you want:

PdfReader read = new PdfReader(pdfPath);
AcroFields af = reader.getAcroFields();

String displayOptions[] = af.getListOptionDisplay(fldName);
String valueOptions[]   = af.getListOptionValue(fldName);

That's Java code written off the cuff in the "Your Answer" box here, but I suspect the C# will be remarkably similar.

(is anyone else so accustomed to in-line expansions that they're expecting it outside their IDE? I keep hitting ctrl-space and expecting to see a list of available functions. :/ )

Display options are what the user sees, value options are what is submitted to the server. They are often identical, but not always. A list of countries might show their full name in the local language to the user, then use an international country code as the value... or it might both show and submit those codes. You get the idea.


You may try Docotic PDF Library. That library allows you to read and write combo box or other form elements values.

Here is a short sample for your task:

using System.Collections.ObjectModel;
using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Samples
{
    public static class ReadComboOptions
    {
        public static void Main()
        {
            using (PdfDocument document = new PdfDocument("DocumentName.pdf"))
            {
                PdfCollection<PdfWidget> widgets = document.Pages[0].Widgets;
                foreach (PdfWidget widget in widgets)
                {
                    PdfComboBox comboBox = widget as PdfComboBox;
                    if (comboBox != null)
                    {
                        foreach (string item in comboBox.Items)
                        {
                            // do something with combo box option
                        }
                    }
                }
            }
        }
    }
}

Disclaimer: I work for the vendor of the library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜