PDF是当今最流行的文档格式之一,各种应用程序将其用作最终输出。由于支持多种数据类型和可移植性,因此它是创建和共享内容的首选格式。作为对开发文档管理应用程序感兴趣的.NET应用程序开发人员,可能希望嵌入处理功能,以读取PDF文档并将其转换为其他文件格式,例如HTML。
Aspose.PDF for .NET是一种高级PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成,修改,转换,渲染,保护和打印PDF文档,而无需使用Adobe Acrobat。
在本文中,我们将探索并演示Aspose.PDF for .NET API的强大转换功能,将PDF文件转换为HTML格式并将输出保存在Stream对象中。
使用流作为目标会导致HtmlSaveOptions此类转换必须提供的类实例所要求的某些自然限制:
如果必须将输出保存到流中,请使用类似于以下代码的内容。(该代码段应放置在一个简单的控制台应用程序中。)请记住,保存链接的外部部分(字体,CSS和图像)并提供正确的URL和URL模板以供生成输出时使用,这是自定义的责任码。随意使用此代码片段作为编写自己的实现的基础。
static string _folderForReferencedResources_34748;
public static void PDFNEWNET_34748()
{
//-----------------------------------------------------
// 1)调整路径并设置许可证
//-----------------------------------------------------
(new Aspose.Pdf.License()).SetLicense(@"F:\_Sources\Aspose_5\trunk\testdata\License\Aspose.Total.lic");
Document pdfDocument = new Document(@"F:\ExternalTestsData\34748_36189.pdf");
string outHtmlFile = @"F:\ExternalTestsData\34748.html";
_folderForReferencedResources_34748 = @"F:\ExternalTestsData\out_34748\";
//-----------------------------------------------------
// 2)清除结果(如果已经存在)
//-----------------------------------------------------
if (Directory.Exists(_folderForReferencedResources_34748))
{
Directory.Delete(_folderForReferencedResources_34748, true);
}
File.Delete(outHtmlFile);
//-----------------------------------------------------
// 使用测试的功能创建HtmlSaveOption
//-----------------------------------------------------
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_34748))
{
Directory.CreateDirectory(_folderForReferencedResources_34748);
}
string path = _folderForReferencedResources_34748 + 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_34748.Replace(@"\", "/") + "css_style{0}.css";
}
private static string Strategy_11_CUSTOM_SAVE_OF_FONTS_AND_IMAGES(SaveOptions.ResourceSavingInfo resourceSavingInfo)
{
if (!Directory.Exists(_folderForReferencedResources_34748))
{
Directory.CreateDirectory(_folderForReferencedResources_34748);
}
string path = _folderForReferencedResources_34748 + Path.GetFileName(resourceSavingInfo.SupposedFileName);
//此方法的第一个路径是保存字体
System.IO.BinaryReader contentReader = new BinaryReader(resourceSavingInfo.ContentStream);
System.IO.File.WriteAllBytes(path, contentReader.ReadBytes((int)resourceSavingInfo.ContentStream.Length));
string urlThatWillBeUsedInHtml = "file:///" + _folderForReferencedResources_34748.Replace(@"\", "/") + Path.GetFileName(resourceSavingInfo.SupposedFileName);
return urlThatWillBeUsedInHtml;
}
如果需要将所有资源(CSS,字体,图像)嵌入到单个HTML流中,则可以使用以下代码示例。它以这样的方式调整转换:所有输出都被强制嵌入到结果HTML中,而无需外部文件,然后使用保存HTML的自定义策略代码将结果HTML写入某些流中。
//文档目录的路径。 string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat(); Document doc = new Document( dataDir + "input.pdf"); //音调转换参数 HtmlSaveOptions newOptions = new HtmlSaveOptions(); newOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground; newOptions.FontSavingMode = HtmlSaveOptions.FontSavingModes.SaveInAllFormats; newOptions.PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml; newOptions.LettersPositioningMethod = HtmlSaveOptions.LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss; newOptions.SplitIntoPages = false;// Force write HTMLs of all pages into one output document newOptions.CustomHtmlSavingStrategy = new HtmlSaveOptions.HtmlPageMarkupSavingStrategy(SavingToStream); //我们可以使用一些不存在的puth作为结果文件名-所有真正的保存都将完成 //在我们的自定义方法SavingToStream()中(遵循此方法) doc.Save(dataDir + "OutPutToStream_out.html", newOptions);
private static void SavingToStream(HtmlSaveOptions.HtmlPageMarkupSavingInfo htmlSavingInfo)
{
byte[] resultHtmlAsBytes = new byte[htmlSavingInfo.ContentStream.Length];
htmlSavingInfo.ContentStream.Read(resultHtmlAsBytes, 0, resultHtmlAsBytes.Length);
// 这里可以使用任何可写流,文件流仅作为示例
string fileName = "stream_out.html";
Stream outStream = File.OpenWrite(fileName);
outStream.Write(resultHtmlAsBytes, 0, resultHtmlAsBytes.Length);
}使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(8)——将输出保存到Stream对象
原文:https://www.cnblogs.com/mnrssj-Aspsoe/p/11926728.html