PDF是当今最流行的文档格式之一,各种应用程序将其用作最终输出。由于支持多种数据类型和可移植性,因此它是创建和共享内容的首选格式。作为对开发文档管理应用程序感兴趣的.NET应用程序开发人员,可能希望嵌入处理功能,以读取PDF文档并将其转换为其他文件格式,例如HTML。
Aspose.PDF for .NET是一种高级PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成,修改,转换,渲染,保护和打印PDF文档,而无需使用Adobe Acrobat。
PDF转HTML-嵌入所有资源的单个HTML
PDF到HTML的转换是Aspose.PDF for Java的最强大功能之一,因为它允许生成高保真HTML文档。转换期间,PDF文件中的所有资源(字体,图像和CSS)都保存在创建输出HTML的同一目录中的单独文件夹中。但是,用户需要新的功能,可将PDF文件转换为HTML格式并生成嵌入了所有资源的单个HTML文件。此功能的实现符合数据URI方案。为此,将值EmbedAllIntoHtml添加到HtmlSaveOptions.PartsEmbeddingModes枚举中。以下代码段显示了如何使用它。
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();// Load source PDF fileDocument doc = new Document(dataDir + "input.pdf");// Instantiate HTML Save options objectHtmlSaveOptions newOptions = new HtmlSaveOptions();// Enable option to embed all resources inside the HTMLnewOptions.PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml;// This is just optimization for IE and can be omittednewOptions.LettersPositioningMethod = HtmlSaveOptions.LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss;newOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground;newOptions.FontSavingMode = HtmlSaveOptions.FontSavingModes.SaveInAllFormats;// Output file pathstring outHtmlFile = "SingleHTML_out.html";doc.Save(outHtmlFile, newOptions);
// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();// Select desirable page sizefloat newPageWidth = 400f;float newPageHeight = 400f;// Tune PdfPageEditor classAspose.Pdf.Facades.PdfPageEditor pdfEditor = new Aspose.Pdf.Facades.PdfPageEditor();// Bind source PDF filepdfEditor.BindPdf(dataDir + "input.pdf");// Set the page dimensionspdfEditor.PageSize = new Aspose.Pdf.PageSize(newPageWidth, newPageHeight);// Set vertical alignment for page as center alignedpdfEditor.VerticalAlignmentType = Aspose.Pdf.VerticalAlignment.Center;// Set Horizontal alignment for page as center alignedpdfEditor.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;// This scales page content to fit width,// Comment it out or set Zoom to 1.0F if You don't want to scale// Content and only want to change page's size (i.e. crop it)float zoom = Math.Min((float)newPageWidth / (float)pdfEditor.Document.Pages[1].Rect.Width, (float)newPageHeight / (float)pdfEditor.Document.Pages[1].Rect.Height);pdfEditor.Zoom = zoom;// (float)595;// Create stream object to hold file with updated dimensionsMemoryStream output = new MemoryStream();// Save file to stream objectpdfEditor.Save(output);// Then reload scaled document and save it to HTMLDocument exportDoc = new Document(output);HtmlSaveOptions htmlOptions = new HtmlSaveOptions();// This code shows page boreder in result - sometimes it comes in handy to see bordersSaveOptions.BorderPartStyle borderStyle = new SaveOptions.BorderPartStyle();borderStyle.LineType = SaveOptions.HtmlBorderLineType.Dotted;borderStyle.Color = System.Drawing.Color.Gray;htmlOptions.PageBorderIfAny = new SaveOptions.BorderInfo(borderStyle);// Conversion to HTML itselfexportDoc.Save(dataDir + "SetOutputFileDimensions_out.html", htmlOptions);// Close the stream objectoutput.Close();
还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!