我正在尝试使用 Graph API 将 MS Word (.docx) 文件转换为 PDF 格式。该文件存储在 SharePoint Office 365 中。我正在使用以下有效代码。
var httpClient = await CreateAuthorizedHttpClient();
string path = $"{GraphEndpoint}sites/{SiteId}/drive/items/";
string requestUrl = $"{path}{fileId}/content?format={targetFormat}";
var response = await httpClient.GetAsync(requestUrl);
但是,当我们尝试转换包含使用以下代码添加的 HTML 的 .docx 文件时,转换失败。
string altChunkId = "myId123";
//Create an alternative format import part on the MainDocumentPart
AlternativeFormatImportPart altformatImportPart = wordDoc.MainDocumentPart
.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, altChunkId);
using (MemoryStream htmlMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes($"<html><head></head><body>{value}</body></html>")))
{
//Add the HTML data into the alternative format import part
altformatImportPart.FeedData(htmlMemoryStream);
//create a new altChunk and link it to the id of the AlternativeFormatImportPart
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
//p.InsertAfterSelf(altChunk);
documentBody.Append(altChunk);
break;
}
当我们尝试使用 Graph API 转换文件时,我收到 406 Not Acceptable 错误。我还看到该文件在浏览器中不可编辑并以兼容模式打开。如果我尝试在编辑模式下打开文档,则会出现错误:
<块引用>抱歉,无法打开此文档,因为它包含以下对象 词不支持
我尝试删除文档的 HTML 部分并将其粘贴到另一个文档中,然后尝试将该文档转换为有效的 PDF。当我看到文档的 XML 时,我看到 Word App 将该 HTML 转换为与 Word 兼容的 XML 标签。
问题 1: 如何将 HTML 转换为与单词兼容的标签?这样我就可以将文档转换为 PDF。
此外,如果我尝试以 PDF 格式下载,文件会毫无问题地转换为 PDF。
此选项使用以下 API 调用:
<块引用>问题 2: 有什么方法可以使用此 API 将 .docx 文件转换为 PDF?我看到访问令牌的受众值是“wopi/{TenantName}@{TenantID}”。如果我获得正确的访问令牌,我想我将能够使用上述 API。