2014-06-25 61 views
0

我試圖用Aspose.PDF從數據庫加載PDF,將其轉換爲HTML並呈現到我們的網站page.I以流想知道,如果我們能保存文檔和流式傳輸,因爲Aspose.PDF文檔中當前示例保存CSS和圖像到本地路徑的資源。我已經試過這一點,但就是和錯誤Aspose.Pdf.SaveFormat.Html不被支持。如何設置HtmlSaveOptions保存HTML和資源使用的Aspose PDF

Aspose.Pdf.Document PDFDocument = new Aspose.Pdf.Document(PDFStream); MemoryStream HTMLStreamFromPDF = new MemoryStream(); PDFDocument.Save(HTMLStreamFromPDF, Aspose.Pdf.SaveFormat.Html);

如果是可以做到的,怎麼寫CustomResourceSavingStrategy,CustomCssSavingStrategy和HtmlSaveOptions的CustomStrategyOfCssUrlCreation的參數。我很抱歉,我不是在C#中非常熟悉的委託

謝謝!

回答

0

終於找到了一種方法來保存所有的文件流。

 MemoryStream HTMLStreamFromPDF = new MemoryStream(); 
     List<MemoryStream> ResourseStreamList = new List<MemoryStream>(); 
     List<string> ResourceNameList = new List<string>(); 
     MemoryStream CSSStream = new MemoryStream(); 
     Aspose.Pdf.HtmlSaveOptions saveOptions = new Aspose.Pdf.HtmlSaveOptions(); 
     CustomResourcesProcessingBind customResourcesProcessingBind = new CustomResourcesProcessingBind((_1) => CustomResourcesProcessing(ResourseStreamList,ResourceNameList, RequestURL, _1)); 
     saveOptions.CustomResourceSavingStrategy = new Aspose.Pdf.HtmlSaveOptions.ResourceSavingStrategy(customResourcesProcessingBind); 
     CssUrlCreationCustomStrategyBind cssUrlCreationCustomStrategyBind = new CssUrlCreationCustomStrategyBind((_1) => CssUrlCreationCustomStrategy(RequestURL, _1)); 
     saveOptions.CustomStrategyOfCssUrlCreation = new Aspose.Pdf.HtmlSaveOptions.CssUrlMakingStrategy(cssUrlCreationCustomStrategyBind); 
     CustomCssSavingProcessingBind customCssSavingProcessingBind = new CustomCssSavingProcessingBind((_1) => CustomCssSavingProcessing(CSSStream, _1)); 
     saveOptions.CustomCssSavingStrategy = new Aspose.Pdf.HtmlSaveOptions.CssSavingStrategy(customCssSavingProcessingBind); 
     saveOptions.HtmlMarkupGenerationMode = Aspose.Pdf.HtmlSaveOptions.HtmlMarkupGenerationModes.WriteOnlyBodyContent; 
     PDFDocument.Save(HTMLStreamFromPDF, saveOptions); 

     private delegate string CustomResourcesProcessingBind(Aspose.Pdf.SaveOptions.ResourceSavingInfo resourceSavingInfo); 

     private static string CustomResourcesProcessing(List<MemoryStream> ResourseStreamList, List<string> ResourceNameList, string RequestURL, Aspose.Pdf.SaveOptions.ResourceSavingInfo resourceSavingInfo) 
     { 
      MemoryStream newResource = new MemoryStream(); 
      resourceSavingInfo.ContentStream.CopyTo(newResource); 
      ResourceNameList.Add(resourceSavingInfo.SupposedFileName); 
      ResourseStreamList.Add(newResource); 

      string urlThatWillBeUsedInHtml = RequestURL +"/"+ Path.GetFileName(resourceSavingInfo.SupposedFileName); 
      return urlThatWillBeUsedInHtml; 
     } 
     private delegate string CssUrlCreationCustomStrategyBind(Aspose.Pdf.HtmlSaveOptions.CssUrlRequestInfo requestInfo); 

     private static string CssUrlCreationCustomStrategy(string RequestURL,Aspose.Pdf.HtmlSaveOptions.CssUrlRequestInfo requestInfo) 
     { 
      return RequestURL + "/css_style.css"; 
     } 

     private delegate void CustomCssSavingProcessingBind(Aspose.Pdf.HtmlSaveOptions.CssSavingInfo resourceInfo); 

     private static void CustomCssSavingProcessing(MemoryStream CSSStream, Aspose.Pdf.HtmlSaveOptions.CssSavingInfo resourceInfo) 
     { 
      resourceInfo.ContentStream.CopyTo(CSSStream);   
     } 
0

請檢查以下示例代碼,瞭解如何在從PDF轉換爲HTML時使用HtmlSaveOptions參數CustomResourceSavingStrategy,CustomCssSavingStrategy和CustomStrategyOfCssUrlCreation。

static string _folderForReferencedResources; 

static void Main(string[] args) 
{ 
    Document pdfDocument = new Document(@"F:\ExternalTestsData\input.pdf"); 
    string outHtmlFile = @"F:\ExternalTestsData\output.html"; 
    _folderForReferencedResources = @"F:\ExternalTestsData\resources\"; 
    //----------------------------------------------------- 
    // 2)clean results if they already present 
    //----------------------------------------------------- 
    if (Directory.Exists(_folderForReferencedResources)) 
    { 
     Directory.Delete(_folderForReferencedResources, true); 
    } 
    File.Delete(outHtmlFile); 
    //----------------------------------------------------- 
    // create HtmlSaveOption with tested feature 
    //----------------------------------------------------- 
    HtmlSaveOptions saveOptions = new HtmlSaveOptions(); 
    saveOptions.CustomResourceSavingStrategy = new HtmlSaveOptions.ResourceSavingStrategy(Strategy_11_CUSTOM_SAVE_OF_FONTS_AND_IMAGES); 
    saveOptions.CustomCssSavingStrategy = new HtmlSaveOptions.CssSavingStrategy(Strategy_11_CSS_WriteCssToPredefinedFolder); 
    saveOptions.CustomStrategyOfCssUrlCreation = new HtmlSaveOptions.CssUrlMakingStrategy(Strategy_11_CSS_ReturnResultPathInPredefinedTestFolder); 

    using (Stream outStream = File.OpenWrite(outHtmlFile)) 
    { 
     pdfDocument.Save(outStream, saveOptions); 
    } 

} 

private static void Strategy_11_CSS_WriteCssToPredefinedFolder(HtmlSaveOptions.CssSavingInfo resourceInfo) 
{ 
    if (!Directory.Exists(_folderForReferencedResources)) 
    { 
     Directory.CreateDirectory(_folderForReferencedResources); 
    } 
    string path = _folderForReferencedResources + Path.GetFileName(resourceInfo.SupposedURL); 
    System.IO.BinaryReader reader = new BinaryReader(resourceInfo.ContentStream); 
    System.IO.File.WriteAllBytes(path, reader.ReadBytes((int)resourceInfo.ContentStream.Length)); 
} 
private static string Strategy_11_CSS_ReturnResultPathInPredefinedTestFolder(HtmlSaveOptions.CssUrlRequestInfo requestInfo) 
{ 
    return "file:///" + _folderForReferencedResources.Replace(@"\", "/") + "css_style{0}.css"; 
} 
private static string Strategy_11_CUSTOM_SAVE_OF_FONTS_AND_IMAGES(SaveOptions.ResourceSavingInfo resourceSavingInfo) 
{ 
    if (!Directory.Exists(_folderForReferencedResources)) 
    { 
     Directory.CreateDirectory(_folderForReferencedResources); 
    } 
    string path = _folderForReferencedResources + Path.GetFileName(resourceSavingInfo.SupposedFileName); 
    // first path of this method is for saving of font 

    System.IO.BinaryReader contentReader = new BinaryReader(resourceSavingInfo.ContentStream); 
    System.IO.File.WriteAllBytes(path, contentReader.ReadBytes((int)resourceSavingInfo.ContentStream.Length)); 
    string urlThatWillBeUsedInHtml = "file:///" + _folderForReferencedResources.Replace(@"\", "/") + Path.GetFileName(resourceSavingInfo.SupposedFileName); 
    return urlThatWillBeUsedInHtml; 
} 

詳細情況可用here

P.S.我是Aspose的社交媒體開發人員。

+0

謝謝,但這段代碼仍然保存css和圖像到本地文件。 – Gary

+0

我與您分享了有關如何使用參數的詳細信息。您可以簡單地更改方法以將文件保存爲流,而不是將它們保存在本地路徑中。很高興知道你能夠做到這一點 –