iTextSharp - 如何打开/读取/提取文件附件?
我有一些 PDF 文件,其中包含两个带有静态名称的附加文件。我想使用 iTextSharp 将这些文件提取到临时目录,以便我可以进一步使用它们。我尝试按照这里的教程进行操作,但是当我遇到问题时iTextSharp.text.pdf.PdfReader
没有 getCatalog()
方法,如底部示例所示。
关于如何提取附件有什么建议吗?简单地说,PDF 文档位于“C:\test.pdf”,两个附件存储为“attach1.xml”和“attach2.xml”。
I have some PDF's all with two attached files with static names. I would like to use iTextSharp to extract these files to a temp directory so that I can work with them further. I tried following the tutorial here but I ran into problems when the iTextSharp.text.pdf.PdfReader
didn't have a getCatalog()
method as shown in the bottom example.
Any advice on how I can extract the attachments? Let's just say for ease that the PDF document is at "C:\test.pdf" and the two attachments are stored as "attach1.xml" and "attach2.xml".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终找到了一种方法来做到这一点——尽管不完全是通过编程方式。我包含了一个名为“pdftk.exe”的二进制文件,它是 PDF ToolKit,它具有用于提取附件的命令行选项。
为了澄清这一点,我添加了 pdftk.exe,然后通过 Process.Start("./pdftk", "contains_attachments.pdf unpack_files output \"C:\\output_directory\"") 调用它。请注意,pdftk 不会输出到尾部带有反斜杠的文件夹。您可以在这里找到 pdftk: http://www.accesspdf.com/pdftk/
添加后.exe 文件复制到项目中,您需要将其属性设置为“始终复制”或“如果较新则复制”。
I ended up finding a way to do this - although not exactly programmatically. I included a binary called "pdftk.exe" which is PDF ToolKit, which has command-line options to extract the attachments.
To clarify, I added pdftk.exe, then called it via
Process.Start("./pdftk", "contains_attachments.pdf unpack_files output \"C:\\output_directory\"")
. Note that pdftk will not output to a folder with a trailing backslash. You can find pdftk here: http://www.accesspdf.com/pdftk/After adding the .exe file to the project, you need to set its properties to "Copy always" or "Copy if newer".
我找到了这个解决方案。我不知道这是否是最好的方法,但是,它有效!
I found this solution. I don't know if it's the best way, but, it work!!