我已经实现了使用 iTextsharp 将 swf 文件添加到 pdf 中,我的问题是,是否可以进行逆向工程,例如,如果我将 pdf 作为输入,我必须从中获取 swf 文件,如果是的话我该如何可以做到吗?
任何有关如何开始的想法,我们将不胜感激。
亲切的问候,
拉古.M
最佳答案
这是一个工作示例,在这里使用此嵌入的 pdf(我找到的第一个):
http://www.opf-labs.org/format-corpus/pdfCabinetOfHorrors/fileAttachment.pdf
并提取嵌入文件,在本例中为 KSBASE.WQ2 文件。
public static void ExtractAttachments(String src, String dir)
{
PdfReader reader = new PdfReader(string.Format("{0}\\{1}", dir, src));
PdfDictionary root = reader.Catalog;
PdfDictionary names = root.GetAsDict(PdfName.NAMES);
PdfDictionary embedded = names.GetAsDict(PdfName.EMBEDDEDFILES);
PdfArray filespecs = embedded.GetAsArray(PdfName.NAMES);
for (int i = 0; i < filespecs.Size; )
{
ExtractAttachment(reader, dir, filespecs.GetAsString(i++),
filespecs.GetAsDict(i++));
}
}
protected static void ExtractAttachment(PdfReader reader, string dir, PdfString name, PdfDictionary filespec)
{
PRStream stream;
FileStream fos;
String filename;
PdfDictionary refs = filespec.GetAsDict(PdfName.EF);
foreach(PdfName key in refs.Keys) {
stream = (PRStream)PdfReader.GetPdfObject(refs.GetAsIndirectObject(key));
filename = filespec.GetAsString(key).ToString();
// here you can do an filename.Contains(".swf) check
var fileBytes = PdfReader.GetStreamBytes(stream);
File.WriteAllBytes(string.Format("{0}\\{1}", dir, filename), fileBytes);
}
}
您可以这样调用它:
var dir = "C:\\temp\\PdfExtract";
ExtractAttachments("fileAttachment.pdf", dir);
您只需在提取之前在文件名周围添加 filename.Contains(".swf)
检查即可。
更新
好吧,如果上述方法不起作用,这就是我的解决办法。
这些文件必须位于目录中的不同位置,在看不到该文件的情况下,这就是我处理它的方式。
我会在 root 解析后添加一个断点,然后进入其中查看是否可以找到 swf 文件所在的位置。
如果您查看root.Keys
,您将看到Catalog
包含的内容。
要检索任何字典对象,您可以使用传入匹配的 PdfName
的 GetAsDict
方法。
再往下一层,您可以看到它包含EmbeddedFiles
等等。
有多个 PdfName
名称,甚至还有一个 Flash 名称。
由于任何文档的结构都可能不同,因此只需调查结构并使用 GetAsDict
的正确参数来读取文件即可。
关于c# - 从 PDF 中提取 SWF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21780168/