2014-03-12 83 views
0

所以我有这个程序,我解析出Excel数据(使用Gembox)。但是,有时会在我想要下载/提取的某些工作表中嵌入PDF文件。我一直无法找到检测和下载这些对象的方法。任何人都能指出我如何实现这一目标的正确方向?我知道微软有一个可以读取excel文件的Office文档提取器,但它只能检测Word等文件。如何在Excel工作表中下载嵌入式PDF文件?

我没有要求任何人为我做我的工作,并写出代码,我只是在这里失去,似乎是一个非常复杂的过程。

回答

0

GemBox.Spreadsheet目前没有对此的支持,但是您可以在WindowsBase.dll程序集中使用System.IO.Packaging命名空间实现您的请求。 尝试下面的代码示例:

 
using System; 
using System.IO; 
using System.IO.Packaging; 
using System.Text; 

static class PdfExtractor 
{ 
    public static void ExtractPdf(string packagePath, string destinationDirectory) 
    { 
     using (var package = Package.Open(packagePath)) 
     { 
      int i = 1; 
      foreach (var part in package.GetParts()) 
       if (part.ContentType == "application/vnd.openxmlformats-officedocument.oleObject") 
       { 
        // PDF data is embedded into OLE Object package part. 

        var pdfContent = GetPdfContent(part.GetStream()); 
        if (pdfContent != null) 
         File.WriteAllBytes(Path.Combine(destinationDirectory, "EmbeddedPdf" + (i++) + ".pdf"), pdfContent); 
       } 
     } 
    } 
 
    private static byte[] GetPdfContent(Stream stream) 
    { 
     // Every PDF file/data starts with '%PDF' and ends with '%%EOF'. 
     const string pdfStart = "%PDF", pdfEnd = "%%EOF"; 

     byte[] bytes = ConvertStreamToArray(stream); 

     string text = Encoding.ASCII.GetString(bytes); 

     int startIndex = text.IndexOf(pdfStart, StringComparison.Ordinal); 
     if (startIndex < 0) 
      return null; 

     int endIndex = text.LastIndexOf(pdfEnd, StringComparison.Ordinal); 
     if (endIndex < 0) 
      return null; 

     var pdfBytes = new byte[endIndex + pdfEnd.Length - startIndex]; 
     Array.Copy(bytes, startIndex, pdfBytes, 0, pdfBytes.Length); 

     return pdfBytes; 
    } 
 
    private static byte[] ConvertStreamToArray(Stream stream) 
    { 
     var buffer = new byte[16 * 1024]; 
     using (var ms = new MemoryStream()) 
     { 
      int read; 
      while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) 
       ms.Write(buffer, 0, read); 

      return ms.ToArray(); 
     } 
    } 
}