Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
本文说明如何 使用 C# 将各种其他类型的文件格式转换为 PDF。本文涵盖以下主题。
以下代码片段同样适用于 Aspose.PDF.Drawing 库。
Aspose.PDF for .NET 允许您轻松地将 EPUB 文件转换为 PDF 格式。
EPUB(全称 electronic publication)是一种由国际数字出版论坛(IDPF)推出的免费且开放的电子书标准。文件扩展名为 .epub。EPUB 旨在支持可重排内容,这意味着 EPUB 阅读器可以针对特定显示设备优化文本。
EPUB 还支持固定布局内容。该格式旨在作为出版商和转换机构内部使用的单一格式,同时也用于分发和销售。它取代了 Open eBook 标准。EPUB 3 版本同样得到图书行业研究小组(BISG)的认可,BISG 是一家领先的图书贸易协会,负责制定标准化最佳实践、研究、信息和活动,涉及内容包装。
以下代码片段展示了如何使用 C# 将 EPUB 文件转换为 PDF 格式。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertEPUBtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Convert options
var options = new Aspose.Pdf.EpubLoadOptions();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "EPUBToPDF.epub", options))
{
// Save PDF document
document.Save(dataDir + "ConvertEPUBtoPDF_out.pdf");
}
}
您还可以为转换设置页面尺寸。要定义新的页面尺寸,请使用 SizeF 对象并将其传递给 EpubLoadOptions 构造函数。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertEPUBtoPDFAdv()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Convert options
var options = new Aspose.Pdf.EpubLoadOptions(new SizeF(1190, 1684));
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "EPUBToPDF.epub", options))
{
// Save PDF document
document.Save(dataDir + "ConvertEPUBtoPDFAdv_out.pdf");
}
}
此功能在 19.6 版或更高版本中受支持。
Aspose.PDF for .NET 提供了基于输入的 Markdown 数据文件创建 PDF 文档的功能。要将 Markdown 转换为 PDF,请使用 MdLoadOptions 初始化 Document。
以下代码片段展示了如何使用 Aspose.PDF 库实现此功能:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertMarkdownToPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Convert options
var options = new Aspose.Pdf.MdLoadOptions();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "sample.md", options))
{
// Save PDF document
document.Save(dataDir + "ConvertMarkdownToPDF_out.pdf");
}
}
PCL(打印机命令语言)是惠普开发的打印机语言,用于访问标准打印机功能。PCL 1 到 5e/5c 级别是基于命令的语言,使用控制序列按接收顺序处理和解释。在消费者层面,PCL 数据流由打印驱动程序生成。自定义应用程序也可以轻松生成 PCL 输出。
当前仅支持 PCL 5 及更早版本
| 命令集合 | 支持 | 例外 | 描述 |
|---|---|---|---|
| 作业控制命令 | + | 双面打印模式 | 控制打印过程:副本数、输出托盘、单面/双面打印、左、上偏移等。 |
| 页面控制命令 | + | 穿孔跳过命令 | 指定页面尺寸、边距、页面方向、行间距和字符间距等。 |
| 光标定位命令 | + | 指定光标位置,从而确定文本、光栅或矢量图像的原点等细节。 | |
| 字体选择命令 | + |
|
允许从 PCL 文件加载软(嵌入)字体并在内存中管理。 |
| 光栅图形命令 | + | 仅限黑白 | 允许从 PCL 文件加载光栅图像到内存,指定宽度、高度、压缩类型、分辨率等光栅参数。 |
| 颜色命令 | + | 允许为所有可打印对象着色。 | |
| 打印模型命令 | + | 允许使用预定义或用户自定义的光栅图案填充文本、光栅图像和矩形区域;可为图案和源光栅图像指定透明模式。预定义图案包括交叉线、斜线和阴影。 | |
| 矩形区域填充命令 | + | 允许使用图案创建并填充矩形区域。 | |
| HP‑GL/2 矢量图形命令 | + | 未实现屏蔽向量命令 (SV)、透明模式命令 (TR)、透明数据命令 (TD)、旋转坐标系 (RO)、可伸缩或位图字体命令 (SB)、字符倾斜命令 (SL) 和额外空格 (ES)。DV(定义可变文本路径)命令在 beta 版中实现。 | 允许从 PCL 文件加载 HP‑GL/2 矢量图像到内存。矢量图像的原点位于可打印区域的左下角,可进行缩放、平移、旋转和裁剪。 矢量图像可包含文本(标签)和几何图形,如矩形、圆、椭圆、直线、弧线、贝塞尔曲线以及由简单图形组合而成的复杂图形。 闭合图形(包括标签字母)可使用实心填充或矢量图案填充。图案可为交叉线、斜线、阴影、光栅用户自定义、PCL 交叉线或斜线以及 PCL 用户自定义。PCL 图案为光栅。标签可单独旋转、缩放,并可指向四个方向:上、下、左、右。左右方向涉及字母依次排列;上下方向涉及字母上下叠置。 |
| 宏 | ― | 允许将一系列 PCL 命令加载到内存,并多次使用该序列,例如打印页面页眉或为一组页面设置格式。 | |
| Unicode 文本 | ― | 允许打印非 ASCII 字符。由于缺少包含 Unicode 文本的示例文件,未实现此功能。 | |
| PCL6 (PCL‑XL) | 仅在 beta 版中实现,原因是缺少测试文件。嵌入字体也不受支持。JetReady 扩展不受支持,因为 JetReady 规范不可用。 | 二进制文件格式。 |
为了实现 PCL 到 PDF 的转换,Aspose.PDF 提供了 PclLoadOptions 类,用于初始化加载选项对象。随后在 Document 初始化时将该对象作为参数传入,以帮助 PDF 渲染引擎确定源文档的输入格式。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPCLtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Convert options
var options = new Aspose.Pdf.PclLoadOptions();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "ConvertPCLtoPDF.pcl", options))
{
// Save PDF document
document.Save(dataDir + "ConvertPCLtoPDF_out.pdf");
}
}
您还可以在转换期间监控错误检测。为此,请通过设置(或取消设置)SupressErrors 来配置 PclLoadOptions 对象。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPCLtoPDFAdvanced()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Convert options
var options = new Aspose.Pdf.PclLoadOptions { SupressErrors = true };
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "ConvertPCLtoPDFAdvanced.pcl", options))
{
if (options.Exceptions != null)
{
foreach (var ex in options.Exceptions)
{
Console.WriteLine(ex.Message);
}
}
// Save PDF document
document.Save(dataDir + "ConvertPCLtoPDFAdvanced_out.pdf");
}
}
Aspose.PDF for .NET 支持将纯文本和预格式化文本文件转换为 PDF 格式的功能。
将文本转换为 PDF 意味着将文本片段添加到 PDF 页面。对于文本文件,我们处理两种类型:预格式化(例如每行 80 个字符、共 25 行)和非格式化(纯文本)。根据需求,您可以自行控制此添加过程,或让库自动处理。
针对纯文本文件,可使用以下技术:
TextReader 读取完整文本。Document 对象并向 Pages 集合添加新页面。TextFragment 对象,并将 TextReader 的内容传入其构造函数。TextFragment 作为段落添加到页面的 Paragraphs 集合中。如果文本量超过页面,库会自动添加额外页面。Document 类的 Save 方法。// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPlainTextFileToPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Read the source text file
using (var streamReader = new StreamReader(dataDir + "TextToPDFInput.txt"))
{
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Create a TextFragment from the read text
var text = new Aspose.Pdf.Text.TextFragment(streamReader.ReadToEnd());
// Add the text fragment as a paragraph
page.Paragraphs.Add(text);
// Save PDF document
document.Save(dataDir + "TextToPDF_out.pdf");
}
}
}
预格式化文本的转换方式类似于纯文本,但需要额外设置边距、字体类型和大小。显然,字体应为等宽字体(例如 Courier New)。
按照以下步骤使用 C# 将预格式化文本转换为 PDF:
Document 对象并向 Pages 集合添加新页面。Paragraphs 集合中。在此情况下,库同样会在需要时添加额外页面。下面的示例将预格式化文本文件转换为使用 A4 页面尺寸的 PDF 文档。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPreFormattedTextToPdf()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Read the text file as an array of strings
var lines = File.ReadAllLines(dataDir + "ConvertPreFormattedTextToPdf.txt");
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Set margins for better presentation
page.PageInfo.Margin.Left = 20;
page.PageInfo.Margin.Right = 10;
page.PageInfo.DefaultTextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Courier New");
page.PageInfo.DefaultTextState.FontSize = 12;
foreach (var line in lines)
{
// Check for a form‑feed character (page break)
if (line.StartsWith("\x0c"))
{
page = document.Pages.Add();
page.PageInfo.Margin.Left = 20;
page.PageInfo.Margin.Right = 10;
page.PageInfo.DefaultTextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Courier New");
page.PageInfo.DefaultTextState.FontSize = 12;
}
else
{
// Create a TextFragment for the line
var text = new Aspose.Pdf.Text.TextFragment(line);
// Add the text fragment as a paragraph
page.Paragraphs.Add(text);
}
}
// Save PDF document
document.Save(dataDir + "PreFormattedTextToPDF_out.pdf");
}
}
Aspose.PDF for .NET 支持将 XPS 文件转换为 PDF 格式的功能。本文说明如何执行此转换。
XPS 文件类型主要与微软公司的 XML Paper Specification(XML 纸张规范)相关。XML Paper Specification(XPS),曾用代号 Metro,最初是下一代打印路径(NGPP)营销概念的一部分,是微软将文档创建和查看集成到 Windows 中的倡议。
要使用 Aspose.PDF for .NET 将 XPS 转换为 PDF,我们引入了 XpsLoadOptions 类,用于初始化 LoadOptions 对象。随后将该对象传递给 Document 构造函数,以帮助 PDF 渲染引擎确定源文档的格式。
以下代码片段展示了使用 C# 将 XPS 文件转换为 PDF 格式的过程。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertXPSToPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Instantiate options
var options = new Aspose.Pdf.XpsLoadOptions();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "XPSTtoPDF.xps", options))
{
// Save PDF document
document.Save(dataDir + "ConvertXPSToPDF_out.pdf");
}
}
Aspose.PDF for .NET 支持将 PostScript 文件转换为 PDF 格式的功能。Aspose.PDF 的一项能力是您可以在转换期间设置要使用的字体文件夹集合。
要将 PostScript 文件转换为 PDF,Aspose.PDF for .NET 提供了 PsLoadOptions 类,用于初始化 LoadOptions 对象。随后将该对象传递给 Document 构造函数,以帮助 PDF 渲染引擎确定源文档的格式。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPostScriptToPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Convert options
var options = new PsLoadOptions();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "ConvertPostscriptInput.ps", options))
{
// Save PDF document
document.Save(dataDir + "PSToPDF_out.pdf");
}
}
此外,您可以指定在转换期间使用的一组字体文件夹:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPostscriptToPDFAdvanced()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Convert options with custom font folders
var options = new Aspose.Pdf.PsLoadOptions
{
FontsFolders = new[] { dataDir + @"\fonts1", dataDir + @"\fonts2" }
};
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "ConvertPostscriptInput.ps", options))
{
// Save PDF document
document.Save(dataDir + "ConvertPostscriptToPDFAdvanced_out.pdf");
}
}
XML 是一种用于存储结构化数据的格式。使用 Aspose.PDF 将 XML 转换为 PDF 有以下几种方式:
使用传统的 Aspose.PDF 技术——实例化带有 XslFoLoadOptions 的 Document 对象——即可实现 XSL‑FO 文件到 PDF 的转换。偶尔您可能会遇到文件结构不正确的情况。针对这种情况,XSL‑FO 转换器允许设置错误处理策略。您可以选择 ThrowExceptionImmediately、TryIgnore 或 InvokeCustomHandler。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void Convert_XSLFO_to_PDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Convert options
var options = new Aspose.Pdf.XslFoLoadOptions(dataDir + "XSLFOToPdfInput.xslt");
// Set error handling strategy
options.ParsingErrorsHandlingType = Aspose.Pdf.XslFoLoadOptions.ParsingErrorsHandlingTypes.ThrowExceptionImmediately;
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "XSLFOToPdfInput.xml", options))
{
// Save PDF document
document.Save(dataDir + "XSLFOToPdf_out.pdf");
}
}
LaTeX 文件格式是一种基于 TeX 系列语言的标记文本格式。LaTeX(发音为 /ˈleɪtɛk/ 或 lay‑tek,亦作 lah‑tek)是一套文档排版系统和标记语言。它在许多领域(包括数学、物理和计算机科学)中广泛用于科学文献的交流与出版。它还在包含复杂多语言材料(如梵文和阿拉伯文)的书籍和文章的准备与出版中发挥重要作用。LaTeX 使用 TeX 排版程序进行输出格式化,并本身使用 TeX 宏语言编写。
Aspose.PDF for .NET 支持将 TeX 文件转换为 PDF 格式的功能,为此 Aspose.Pdf 命名空间提供了 LatexLoadOptions 类,可加载 LaTeX 文件并使用 Document 类将输出渲染为 PDF 格式。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertTeXtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Convert options
var options = new Aspose.Pdf.TeXLoadOptions();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "samplefile.tex", options))
{
// Save PDF document
document.Save(dataDir + "TeXToPDF_out.pdf");
}
}
OFD 代表 “Open Fixed‑layout Document”,是中国的电子文件存储国家标准,作为流行的 PDF 格式的替代方案。它支持固定布局文档,确保在不同平台上显示一致。OFD 文件用于数字文档和业务应用等多种场景。
Aspose.PDF for .NET 支持将 OFD 文件转换为 PDF 格式的功能,为此 Aspose.Pdf 命名空间提供了 OfdLoadOptions 类,可加载 OFD 文件并使用 Document 类将输出渲染为 PDF 格式。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertOFDToPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Convert options
var options = new Aspose.Pdf.OfdLoadOptions();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "ConvertOFDToPDF.ofd", options))
{
// Save PDF document
document.Save(dataDir + "ConvertOFDToPDF_out.pdf");
}
}
富文本格式(RTF)是一种由微软开发的跨平台文件格式,旨在实现不同文字处理器之间的文档轻松共享。它支持基本的文本格式(如字体、颜色、粗体、斜体)以及图像,提供的样式选项比纯文本更丰富。
Aspose.PDF for .NET 提供了将 RTF 格式文件转换为 PDF 格式的选项。可通过指定特殊的加载选项类 RtfLoadOptions 来实现,在创建 Document 类实例时将其作为参数传入。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertRTFToPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Convert options
var options = new Aspose.Pdf.RtfLoadOptions();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "ConvertRtfToPdf.rtf", options))
{
// Save PDF document
document.Save(dataDir + "ConvertRtfToPdf_out.pdf");
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.