簡體   English   中英

使用C#將SharePoint庫XML文檔轉換為PDF文檔

[英]Convert SharePoint Library XML document to PDF document using C#

背景

我有一個SharePoint 2013(SP2013)環境(env),其中Word Automation Services(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庫中的同一位置。

希望這可以幫助。

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM