.NET Core 使用 OpenXml 将 PPT 转换为 PDF
要将 PPT 文件转换为 PDF 文件,可以使用 OpenXml 库和 Microsoft.Office.Interop.PowerPoint 库来实现。以下是使用 .NET Core 将 PPT 转换为 PDF 的步骤:
- 安装 OpenXml 和 Microsoft.Office.Interop.PowerPoint 库
可以使用 NuGet 包管理器或 .NET CLI 安装这两个库。
- 加载 PPT 文件
使用 Microsoft.Office.Interop.PowerPoint 库加载 PPT 文件。
using Microsoft.Office.Interop.PowerPoint;
var pptFilePath = 'path/to/ppt/file.pptx';
var app = new Application();
var presentation = app.Presentations.Open(pptFilePath);
- 将 PPT 文件保存为 PDF
使用 OpenXml 库将 PPT 文件保存为 PDF。
using DocumentFormat.OpenXml.Packaging;
var pdfFilePath = 'path/to/pdf/file.pdf';
var pdfStream = new FileStream(pdfFilePath, FileMode.Create);
var pdfDocument = PresentationDocument.Create(pdfStream, PresentationDocumentType.Presentation);
// 将 PPT 文件复制到 PDF 文件中
var pptPart = pdfDocument.AddPresentationPart();
presentation.SaveAs(pptPart);
pdfDocument.Close();
pdfStream.Close();
完整代码:
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using Microsoft.Office.Interop.PowerPoint;
namespace PptToPdfConverter
{
class Program
{
static void Main(string[] args)
{
var pptFilePath = 'path/to/ppt/file.pptx';
var pdfFilePath = 'path/to/pdf/file.pdf';
var app = new Application();
var presentation = app.Presentations.Open(pptFilePath);
var pdfStream = new FileStream(pdfFilePath, FileMode.Create);
var pdfDocument = PresentationDocument.Create(pdfStream, PresentationDocumentType.Presentation);
// 将 PPT 文件复制到 PDF 文件中
var pptPart = pdfDocument.AddPresentationPart();
presentation.SaveAs(pptPart);
pdfDocument.Close();
pdfStream.Close();
presentation.Close();
app.Quit();
}
}
}
原文地址: https://www.cveoy.top/t/topic/owxS 著作权归作者所有。请勿转载和采集!