将Excel工作簿转换为PDF
PDF文件被广泛用于组织、政府部门和个人之间交换文档。它是一种标准文档格式,软件开发人员经常被要求找到一种方法将Microsoft Excel文件转换为PDF文档。
Aspose.Cells支持将Excel文件转换为PDF,并在转换过程中保持高度的视觉保真度。
Aspose.Cells for .NET 直接在输出文档中写入 API 和版本号的信息。例如,在将文档渲染为 PDF 时,Aspose.Cells for .NET 在 PDF 生成器 字段中填充值,例如 ‘Aspose.Cells v23.2’。
请注意,您可以通过 PdfSaveOptions.Producer 属性在输出文档中更改此信息。
直接转换
Aspose.Cells for .NET支持将电子表格独立转换为PDF,无需其他软件。只需使用Workbook类的Save方法将Excel文件保存为PDF。Save方法提供了将原生Excel文件转换为PDF格式的SaveFormat.Pdf枚举成员。
按以下步骤直接将Excel电子表格转换为PDF格式:
通过调用其空构造函数实例化Workbook类的对象。
- 您可以打开/加载现有模板文件,或者如果您是从头开始创建工作簿,则跳过此步骤。
- 使用Aspose.Cells的API在电子表格上进行任何工作(输入数据,应用格式,设置公式,插入图片或其他绘图对象等)。 当电子表格代码完成时,调用Workbook类的Save方法保存电子表格。
文件格式应为PDF,因此从SaveFormat枚举中选择Pdf(预定义值)生成最终PDF文档。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate the Workbook object | |
// Open an Excel file | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
// Save the document in PDF format | |
workbook.Save(dataDir + "output.pdf", SaveFormat.Pdf); |
高级转换
您还可以选择使用PdfSaveOptions类来设置转换的不同属性。设置PdfSaveOptions类的不同属性可控制输出PDF的打印、字体、安全和压缩设置。
最重要的属性是Compliance,它允许您设置PDF的标准兼容级别。目前,您可以保存为PDF 1.4、PDF 1.5、PDF 1.6、PDF 1.7、PDF/A-1a、PDF/A-1b、PDF/A-2a、PDF/A-2b、PDF/A-2u、PDF/A-3a、PDF/A-2ab和PDF/A-3u格式。请注意,使用PDF/A格式时,输出文件大小大于常规PDF文件大小。
将工作簿保存为PDF/A兼容文件
下面提供的代码片段演示了如何使用PdfSaveOptions类将Excel文件保存为PDF/A兼容的PDF格式。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate new workbook | |
Workbook workbook = new Workbook(); | |
// Insert a value into the A1 cell in the first worksheet | |
workbook.Worksheets[0].Cells[0, 0].PutValue("Testing PDF/A"); | |
// Define PdfSaveOptions | |
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
// Set the compliance type | |
pdfSaveOptions.Compliance = PdfCompliance.PdfA1b; | |
// Save the file | |
workbook.Save(dataDir + "output.pdf", pdfSaveOptions); |
设置PDF创建时间
使用PdfSaveOptions类,您可以获取或设置PDF创建时间。以下代码演示了使用PdfSaveOptions.CreatedTime属性设置PDF文件的创建时间。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string inputPath = dataDir + "Book1.xlsx"; | |
// Load excel file containing charts | |
Workbook workbook = new Workbook(inputPath); | |
// Create an instance of PdfSaveOptions | |
PdfSaveOptions options = new PdfSaveOptions(); | |
options.CreatedTime = DateTime.Now; | |
// Save the workbook to PDF format while passing the object of PdfSaveOptions | |
workbook.Save(dataDir + "output.pdf", options); |
设置ContentCopyForAccessibility选项
使用PdfSaveOptions类,您可以获取或设置PDF的AccessibilityExtractContent选项,以控制转换后PDF中的内容访问。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
string inputPath = sourceDir + "BookWithSomeData.xlsx"; | |
// Load excel file containing some data | |
Workbook workbook = new Workbook(inputPath); | |
// Create an instance of PdfSaveOptions and pass SaveFormat to the constructor | |
PdfSaveOptions pdfSaveOpt = new PdfSaveOptions(); | |
// Create an instance of PdfSecurityOptions | |
PdfSecurityOptions securityOptions = new PdfSecurityOptions(); | |
// Set AccessibilityExtractContent to true | |
securityOptions.AccessibilityExtractContent = false; | |
// Set the securityoption in the PdfSaveOptions | |
pdfSaveOpt.SecurityOptions = securityOptions; | |
// Save the workbook to PDF format while passing the object of PdfSaveOptions | |
workbook.Save(outputDir + "outFile.pdf", pdfSaveOpt); |
导出自定义属性到PDF
使用PdfSaveOptions类,您可以将源工作簿中的自定义属性导出到PDF。提供了PdfCustomPropertiesExport枚举用于指定属性的导出方式。这些属性可以通过单击“文件”然后选择“属性”在Adobe Acrobat Reader中观察。模板文件"sourceWithCustProps.xlsx"可在此处下载进行测试,输出的PDF文件"outSourceWithCustProps"可在此处进行分析。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Load excel file containing custom properties | |
Workbook workbook = new Workbook("sourceWithCustProps.xlsx"); | |
// Create an instance of PdfSaveOptions | |
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
// Set CustomPropertiesExport property to PdfCustomPropertiesExport.Standard | |
pdfSaveOptions.CustomPropertiesExport = Aspose.Cells.Rendering.PdfCustomPropertiesExport.Standard; | |
// Save the workbook to PDF format while passing the object of PdfSaveOptions | |
workbook.Save("outSourceWithCustProps.pdf", pdfSaveOptions); |
转换属性
我们致力于增强每个新版本的转换功能。 Aspose.Cells的Excel转PDF转换仍然存在一些限制。 在转换为PDF格式时不支持MapChart。 还有一些绘图对象支持不佳。
下表列出了使用Aspose.Cells导出为PDF时全部或部分支持的所有功能。 该表不是最终版本,不涵盖所有电子表格属性,但确实标识了在转换为PDF时不支持或部分支持的功能。
文档元素 | 属性 | 支持 | 备注 |
---|---|---|---|
对齐 | 支持 | ||
背景设置 | 支持 | ||
边框 | 颜色 | 支持 | |
边框 | 线条样式 | 支持 | |
边框 | 线宽 | 支持 | |
单元格数据 | 是 | ||
备注 | 是 | ||
条件格式 | 是 | ||
文档属性 | 是 | ||
绘图对象 | 部分 | 绘图对象的阴影和3D效果支持不佳;WordArt和智能图表部分支持。 | |
字体 | 大小 | 是 | |
字体 | 颜色 | 是 | |
字体 | 样式 | 是 | |
字体 | 下划线 | 是 | |
字体 | 效果 | 是 | |
图像 | 是 | ||
超链接 | 是 | ||
图表 | 部分 | 不支持地图图表。 | |
合并单元格 | 是 | ||
分页符 | 是 | ||
页面设置 | 页眉/页脚 | 是 | |
页面设置 | 页边距 | 是 | |
页面设置 | 页面方向 | 是 | |
页面设置 | 页面大小 | 是 | |
页面设置 | 打印区域 | 是 | |
页面设置 | 打印标题 | 是 | |
页面设置 | 缩放 | 是 | |
行高/列宽 | 是 | ||
RTL (从右到左) 语言 | 是 |
高级主题
- 添加PDF书签
- 使用命名目标添加PDF书签
- 当没有需要打印的内容时,避免在输出PDF中出现空白页
- 在保存为PDF时仅更改特定Unicode字符的字体
- 控制在将MS Excel工作簿渲染为PDF时加载外部资源
- 将XLSX文件转换为PDF格式
- 将 Excel 文件转换为兼容 PDFA-1a 格式的 PDF
- 将带有图片或图表的XLS文件转换为PDF
- 为图表工作表创建PdfBookmarkEntry
- 将所有工作表列调整到单个PDF页面上
- 在使用DrawObjectEventHandler类呈现到PDF时获取DrawObject和边界
- 在呈现Excel文件时获取字体替换的警告
- 在将Excel渲染为PDF时忽略错误
- 限制生成的页面数量-从Excel转换为PDF
- 保存为PDF时打印注释
- 在将Excel转换为PDF时呈现Office加载项
- 将每个Excel工作表呈现为一个PDF页面-从Excel转换为PDF
- 通过Aspose.Cells在输出PDF中呈现Unicode补充字符
- 重新采样添加的图像-从Excel转换为PDF
- 将每个工作表保存为不同的PDF文件
- 以标准或最小尺寸保存Excel到PDF
- 将指定的工作表保存为 PDF
- 安全的PDF文件
- 指定如何在输出PDF和图像中跨越字符串