使用C#将SharePoint库XML文档转换为PDF文档

问题描述 投票:0回答:1

Background

我有一个SharePoint 2013(SP2013)环境(env),其中Word自动化服务(WAS)已停止工作,因此我的应用程序无法将XML文档转换为PDF。

Previous Status

我使用OpenXML SDK将XML InfoPath文档转换为Word文档(按预期工作)。然后使用SP上的WAS将Word文档转换为PDF。

Current Status

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。

c# pdf sharepoint-2013 openxml-sdk
1个回答
0
投票

我遇到过同样的问题。 SharePoint库是一个没有物理位置的网络驱动器。 SharePoint的文件确实保存在数据库中,因此这就是我们无法在SharePoint库中编写和保存文件的原因。 MS Office有一种不同的方法将文件保存到SP库,它实际上传文件而不是直接保存文件。

解决此问题的方法是获取Word文件的本地副本并更改本地副本并将其上载(即复制)到SharePoint库中的相同位置。

希望这可以帮助。

谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.