如何设置HtmlSaveOptions以使用Aspose PDF保存html和资源以进行流式传输

时间:2014-06-25 01:37:03

标签: aspose

我正在尝试使用Aspose.PDF从数据库加载PDF,将其转换为HTML并将它们呈现到我们的网页。我想知道我们是否可以将文档和资源保存到流中,因为当前示例中的Aspose.PDF文档将css和图像保存到本地路径。我试过这个,但不支持Aspose.Pdf.SaveFormat.Html的错误和错误。

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

如果可以,如何编写HtmlSaveOptions的CustomResourceSavingStrategy,CustomCssSavingStrategy和CustomStrategyOfCssUrlCreation的参数。对不起,我对C#

中的代表不太熟悉

谢谢!

2 个答案:

答案 0 :(得分:1)

终于找到了一种将所有文件保存到流中的方法。

        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);           
        }

答案 1 :(得分:0)

检查以下示例代码,了解如何在从PDF转换为HTML时使用参数CustomResourceSavingStrategy,CustomCssSavingStrategy和HtmlSaveOptions的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的社交媒体开发人员。