简体   繁体   English

使用C#从PDF获取数据

[英]Getting data from a PDF using C#

I have a preexisting PDF with several drop down combo boxes. 我有一个预先存在的PDF,有几个下拉组合框。 I would like to be able to get the list of options from these combo boxes in a C# program. 我希望能够从C#程序中的这些组合框中获取选项列表。

I have looked at iText but have not been able to figure out if it can do what I want it to. 我看过iText,但一直无法弄清楚它是否可以做我想要的。

Any suggestions would be greatly appreciated. 任何建议将不胜感激。 Thank you in advance! 先感谢您!

iText[Sharp] can indeed do what you want: iText [夏普]确实可以做你想要的:

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. 这是在你的“你的答案”框中从袖口写下的Java代码,但我怀疑C#会非常相似。

(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. :/ ) (是否有其他人如此习惯于在线扩展他们期望在IDE之外?我一直在按住ctrl-space并希望看到可用功能列表。:/)

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 . 您可以尝试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. 免责声明:我为图书馆的供应商工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM