在这个简洁明了的教程中,您将学习如何在云中使用 C# REST API**将 PDF 转换为 DOCX。为了将 PDF 导出为 DOCX 格式,我们使用 Aspose.PDF for C# Cloud SDK。如果您需要 在 C# 低代码 API 中将 PDF 转换为 DOCX,则可以借助以下示例代码和步骤轻松实现。
先决条件
- 创建帐户 API 凭据
- 下载 Aspose.PDF Cloud SDK for Dotnet
- 使用上述 SDK 配置您的 C# 项目
在 C# REST API 中将 PDF 转换为 DOCX 的步骤
- 配置 API 的客户端 ID 和客户端密钥
- 使用客户端凭据实例化 PdfApi 类对象
- 标记输入和输出文件
- 访问源PDF文件并将其上传到云存储
- 使用 PutPdfInStorageToDoc 方法使用 REST API 将 PDF 转换为 DOCX
- 从云端获取生成的DOCX文件并保存到本地磁盘
上述步骤可帮助您使用低代码 C# 将 PDF 转换为 DOCX。您首先将所需的 PDF 文件上传到云端,然后需要使用 API 公开的 PutPdfInStorageToDoc 方法来获取响应流。最后将响应流转换为DOCX,实现PDF到DOCX的转换。
C# 低代码 API 中的 PDF 到 DOCX 转换代码
using System; | |
using System.IO; | |
using Aspose.Pdf.Cloud.Sdk.Api; | |
using Aspose.Pdf.Cloud.Sdk.Model; | |
namespace Aspose.PDF.Cloud.Examples.Kb | |
{ | |
public class PdfToDocx | |
{ | |
public static void ConvertPdfToDocx() | |
{ | |
try | |
{ | |
PdfApi pdfApi = new PdfApi("ID", "Secret"); | |
string localPath = @"C:\Words\"; | |
string srcFileName = "Sample.pdf"; | |
string outputFile = "PDFtoDocx.docx"; | |
string fileformat = "DOCX"; | |
//Upload the source PDF file to the cloud storage | |
pdfApi.UploadFile(srcFileName, File.Open(localPath + srcFileName, FileMode.Open)); | |
AsposeResponse response = pdfApi.PutPdfInStorageToDoc(srcFileName, outputFile, null, | |
fileformat, null, null, null, null, null, null, null, null, null); | |
// Download the output DOCX file from Cloud Storage | |
var stream = pdfApi.DownloadFile(outputFile, null, null); | |
using (var fileStream = File.Create(localPath + outputFile)) | |
{ | |
stream.Seek(0, SeekOrigin.Begin); | |
stream.CopyTo(fileStream); | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
} | |
} | |
} | |
} |
上面的示例代码允许您使用 REST API 将 PDF 转换为 DOCX,并且可以在 macOS、Linux 或 Windows 等任何操作系统上的任何无代码或低代码应用程序中使用。您可以通过设置 API Swagger 中提到的图像分辨率、行间距、项目符号识别、相对水平接近度和密码保护中提到的不同选项来自定义输出 DOCX。
在本主题中,我们探索了低代码 C# PDF 到 DOCX 转换 API 来创建将 PDF 转换为 DOCX 文件格式的应用程序。如果您有兴趣将 PDF 转换为 EPUB 文件,请参阅有关如何使用 C# REST API 将 PDF 转换为 EPUB的文章。