执行“pdftk my-pdf-form.pdf dump_data_fields”不显示任何内容。

6
我正在使用工具pdftk,我有一个可编辑的PDF文件,并在文档中看到参数dump_data_fields应该显示表单字段。
我在Windows使用以下命令:pdftk my-pdf-form.pdf dump_data_fields 我正在使用pdftk服务器版本。
文档:https://www.pdflabs.com/docs/pdftk-man-page/ 关键是PDF是可编辑的,它具有可以用Adobe PDF Viewer编写的字段。
4个回答

8
问题在于 PDF 文件是由 Adobe LiveCycle Designer 创建的,并保存为 “Adobe Dynamic XML From” 格式。解决方法是将文件保存为 “Adobe Static PDF Form” 格式。可能 pdftk 无法处理 livecycle 文件。

1
我认为被接受的答案可能是我的解决方案,但事实证明,我正在处理的PDF文档实际上没有设置表单字段。如果文档看起来像一个表单,但表单字段没有变灰,那么就不会检测到任何字段。
我唯一能解决这个问题的方法是在Acrobat Pro中打开文档,并通过其表单工具添加字段。然后,pdftk正常工作。

1
如果您在Windows环境中遇到OP问题,请按照以下说明进行操作。
1- 打开GUI PDFtk程序。(如果愿意,也可以使用cli)

extracting pdf fields using pdftk on Windows

2- 点击“添加PDF...”按钮,搜索您的可填写PDF文件。

extracting pdf fields using pdftk on Windows

3- 向下滚动到GUI PDFtk窗口底部,点击“创建PDF...”按钮,不要添加或更改任何设置。

extracting pdf fields using pdftk on Windows

4- 将新的填写好的PDF文件以新名称保存到您选择的目录中。

extracting pdf fields using pdftk on Windows

5- 最后,使用cmd发出dump_data_fields命令的Windows版本,如下所示。(请注意,“output”代替了“>”)

extracting pdf fields using pdftk on Windows

6- 打开文本文件"fields.txt",你会看到字段名称。下面是示例。

extracting pdf fields using pdftk on Windows


0

我不知道这是否有帮助,但我编写了一些C#代码来计算文档中的数据字段。请参见以下函数。

  1. 在这里,我们将文件路径传递给一个文件,并计算文档中字段的总数。

    public int countDataFields(string inputFile)
    {
        int fieldCount = 0;
        string arguments = "";
    
        using (Process newProcess = new Process())
        {
            arguments = inputFile + " dump_data_fields";
            newProcess.StartInfo = new ProcessStartInfo("pdftk ", arguments);
            newProcess.StartInfo.RedirectStandardInput = true;  
            newProcess.StartInfo.RedirectStandardOutput = true;
            newProcess.StartInfo.RedirectStandardError = true;
            newProcess.StartInfo.UseShellExecute = false;
            newProcess.StartInfo.CreateNoWindow = false;
            newProcess.Start();
    
            while (!newProcess.StandardOutput.EndOfStream)
            {
                var line = newProcess.StandardOutput.ReadLine();
                fieldCount = fieldCount + 1;
            }
    
            Console.WriteLine("Field Counts: " + fieldCount);
            newProcess.WaitForExit();
        }
    
        return fieldCount;
    }
    
  2. 如果您想通过标准输入流传递文件

    public void countDataFieldsWhenFilePassedAsBinaryStream(string file1)
    {
        int fieldCount = 0;
        // 初始化二进制读取器并使用传入文件的文件流打开二进制读取器。
        BinaryReader binaryReader = new BinaryReader(File.Open(file1, FileMode.Open, FileAccess.Read));
    
        //创建一个大小为1024的缓冲区数组。
        byte[] buffer = new byte[1024];
    
        using (Process newProcess = new Process())
        {
            newProcess.StartInfo = new ProcessStartInfo("pdftk");
            newProcess.StartInfo.Arguments = @" - dump_data_fields";
            newProcess.StartInfo.UseShellExecute = false;
            newProcess.StartInfo.RedirectStandardInput = true;
            newProcess.StartInfo.RedirectStandardOutput = true;
            newProcess.Start();
    
            int bytesRead = 0;
    
            //我们按1024字节的块读取二进制文件
            //只要读取的字节数大于0,我们就循环执行
            while ((bytesRead = binaryReader.Read(buffer, 0, 1024)) > 0)
            {
                //将标准输入字节写入缓冲区。
                newProcess.StandardInput.BaseStream.Write(buffer, 0, bytesRead);
            }
    
            //关闭binaryReader
            binaryReader.Close();
    
            //关闭标准输入流
            newProcess.StandardInput.Close();
    
            //在这里,我们将循环遍历标准输出流直到eof。我们正在计算
            while (newProcess.StandardOutput.EndOfStream == false)
            {
                //读取行;
                newProcess.StandardOutput.ReadLine();
                //增加计数器
                fieldCount++;;
            }
    
            // console writeline the field count.
            Console.WriteLine(fieldCount);
    
            newProcess.WaitForExit();
        }// end of using
    }// end of function convertPDFToStandardInput
    

希望这可以帮到你 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接