使用Aspose.PDF for .NET将PDF转换为HTML格式示例解读(12)——PDF转换到HTML时设置输出文件尺寸

PDF是当今最流行的文档格式之一,各种应用程序将其用作最终输出。由于支持多种数据类型和可移植性,因此它是创建和共享内容的首选格式。作为对开发文档管理应用程序感兴趣的.NET应用程序开发人员,可能希望嵌入处理功能,以读取PDF文档并将其转换为其他文件格式,例如HTML。

Aspose.PDF for .NET是一种高级PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成,修改,转换,渲染,保护和打印PDF文档,而无需使用Adobe Acrobat。


PDF转HTML-设置输出文件尺寸

将PDF文件转换为HTML格式时,将以与输入PDF文档相同的尺寸保存输出文件。但是,有时您需要生成具有特定尺寸的输出HTML。使用Aspose.PDF.Facades.PdfPageEditor和HtmlSaveOptions类来满足此要求。

PdfPageEditorAspose.PDF.Facades命名空间中的类提供了更新或操作PDF文档中页面的功能。它还提供了设置或更新输入PDF文件的页面尺寸的功能。在转换为HTML时设置特定尺寸:

  1. 将源PDF文档加载到PdfPageEditor对象中。
  2. 更新页面尺寸。
  3. 使用HtmlSaveOptions该类将输出呈现为HTML格式。

以下代码段显示了转换PDF文件HTML格式并为输出文件指定尺寸的步骤。

// 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进行处理,非常感谢!

上一篇 2020年1月5日
下一篇 2020年1月5日

相关推荐

发表回复

登录后才能评论