2012-07-06 156 views
3

我正在嘗試使用PDfSharp將pdf轉換爲jpeg。使用PdfSharp將PDF轉換爲Jpeg(資源爲空)

這裏是我的代碼:

PdfSharp.Pdf.PdfDocument document = PdfSharp.Pdf.IO.PdfReader.Open(doc); 
      PdfSharp.Pdf.PdfPage page = document.Pages[0];   
      // get resources dictionary 
      PdfSharp.Pdf.PdfDictionary resources = page.Elements.GetDictionary("/resources"); 
      if (resources != null) 
      { 
       // get external objects dictionary 
       PdfSharp.Pdf.PdfDictionary xobjects = resources.Elements.GetDictionary("/xobject"); 
       if (xobjects != null) 
       { 
        ICollection<PdfSharp.Pdf.PdfItem> items = xobjects.Elements.Values; 
        // iterate references to external objects 
        foreach (PdfSharp.Pdf.PdfItem item in items) 
        { 
         PdfSharp.Pdf.Advanced.PdfReference reference = item as PdfSharp.Pdf.Advanced.PdfReference; 
         if (reference != null) 
         { 
          PdfSharp.Pdf.PdfDictionary xobject = reference.Value as PdfSharp.Pdf.PdfDictionary; 
          // is external object an image? 
          if (xobject != null && xobject.Elements.GetString("/subtype") == "/image") 
          { 
           ExportJpegImage(xobject); 
          } 
         } 
        } 
       } 

     } 

行:if (resources != null)是返回false。我不確定應該包含哪些資源,但對於轉換的其餘部分似乎很重要。我從pdfSharp示例站點複製了此代碼。我的PDF可能有問題嗎?我使用單詞2010。

任何幫助將不勝感激!

+1

資源鍵應以大寫'R'開頭。你有沒有嘗試過使用**/Resources **而不是**/resources **?其他鍵也有錯誤的大寫。還是以不區分大小寫的方式查找PDFSharp? – mkl 2013-09-07 07:01:42

回答

3

如果要將PDF轉換爲JPEG格式,並且希望使用免費的軟件庫,請考慮使用ImageMagick。這可以在所有主要平臺上運行,所以在Windows上你會很好。它可以在命令行中啓動,並且可以設置您的首選有損壓縮級別。

編輯:啊,我看到另一個問題,你正在使用ImageMagick的.net接口。如果你可以使用它,那很棒,但是你可能會發現使用convert命令更容易!

+0

感謝您的回覆。是的,我一直在與ImageMagickNet合作,但發現它比我期待的要困難。我對.NET世界相當陌生,但是你知道我是否能夠使用'convert'命令作爲我的網站的一部分? – PFranchise 2012-07-06 22:51:44

+1

我確定你可以。只要找到一種方法來執行一個網頁內的系統命令(我不是一個.net程序員),並調用'convert.exe -quality 85 infile.pdf/temp/path/tempfile.jpg' – halfer 2012-07-06 22:59:34