c# - 使用 C# 将 SharePoint 库 XML 文档转换为 PDF 文档
问题描述
背景
我有一个 SharePoint 2013 (SP2013) 环境 (env),其中 Word 自动化服务 (WAS) 已停止工作,因此我的应用程序无法将 XML 文档转换为 PDF。
以前的状态
我使用 OpenXML SDK 将 XML InfoPath 文档转换为 Word 文档(按预期工作)。然后使用 SP 上的 WAS 将 Word 文档转换为 PDF。
当前状态
WAS 停止工作。我的应用程序将 XML 转换为 Word,但从不转换为 PDF。作为权宜之计,我正在使用 C# 代码片段(如下所示)尝试转换为 PDF,但我不断收到错误消息“对象引用未设置为对象的实例”。
...
using Word = Microsoft.Office.Interop.Word;
...
string fileName = generatedDoc; //generatedDoc is Word doc converted from XML
string pdfFileName = fileName.Replace("docx", "pdf");
string sourceUrl = siteUrl + "/DocLibMemo/" + fileName;
string destUrl = siteUrl + "/ApprovedMemoPDF/" + pdfFileName;
Convert(sourceUrl, destUrl, Word.WdSaveFormat.wdFormatPDF);
public static void Convert(string input, string output, Word.WdSaveFormat format)
{
// Create an instance of Word.exe
Word._Application oWord = new Word.Application();
// Make this instance of word invisible (Can still see it in the taskmgr).
oWord.Visible = false;
oWord.ScreenUpdating = false;
// Interop requires objects.
object oMissing = System.Reflection.Missing.Value;
object isVisible = false;
object readOnly = false;
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
object oInput = input;
object oOutput = output;
object oFormat = format;
// Load a document into our instance of word.exe
Word._Document oDoc = oWord.Documents.Open(
ref oInput, ref oMissing, ref readOnly,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Make this document the active document.
oDoc.Activate(); // The execption is hit here
// Save this document using Word
oDoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Always close Word.exe.
oWord.Quit(ref oMissing, ref oMissing, ref doNotSaveChanges);
}
当我使用控制台应用程序对其进行测试并且 Word 文件位于我的 C 驱动器上时,上面的代码片段有效。但是,现在 Word 文件位于 SP 库中,它不会转换为 PDF。
解决方案
我遇到了同样的问题。SharePoint 库是没有物理位置的网络驱动器。SharePoint 的文件确实保存在数据库中,这就是为什么我们不能在 SharePoint 库中写入和保存文件的原因。MS Office 有另一种将文件保存到 SP 库的方法,它实际上是上传文件而不是直接保存它们。
该问题的解决方案是获取 Word 文件的本地副本并对本地副本进行更改并将其上传(即复制)到 SharePoint 库中的相同位置。
希望这可以帮助。
谢谢。
推荐阅读
- javascript - JavaScript。无法使用 Object.keys () 迭代对象以获取新对象
- javascript - 何时在项目中添加依赖项与自定义解决方案
- chart.js - 如何为内部甜甜圈添加工具提示
- tensorflow - 使用保存的模型在张量流中进行预测
- mysql - MySQL如何将日期拆分为已知间隔并将其转换为行?
- android - 像轻飘飘的鸟一样流畅的飞行动作-> isFlapping Boolean?
- javascript - 无法将消息从上下文菜单发送到内容脚本
- python - 在函数中定义的 Python 类的状态
- css - 如何在窗口的 50% 宽度中为这个 css 设置动画
- security - NPM 6 - 我应该审核修复所有包漏洞吗?