如何在事件接收器中执行PDF转换?

时间:2011-09-26 13:46:20

标签: c# .net sharepoint-2010 sharepoint-2007

每当文件更新时,我都会尝试将文档库(转换)中的.docx文件转换为PDF格式。

我正在使用的代码如下:

public override void ItemUpdated(SPItemEventProperties properties)
           {          
                   ConversionJob job = new ConversionJob(wordAutomationServiceName);
                   job.UserToken = properties.Web.CurrentUser.UserToken;
                   job.Settings.UpdateFields = true;
                   job.Settings.OutputFormat = SaveFormat.PDF;
                   string input = siteURL + "Conversion/Test.docx";
                   string output = siteURL + "Conversion/Test.pdf";
                   job.AddFile(input, output);
                   job.Start();
           }

当我在调试模式下运行时,它会在没有任何错误或异常的情况下执行,但它不会生成任何PDF文件。

我无法找出问题所在

尝试了这个简单的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Word.Server.Conversions;

class Program
{
    static void Main(string[] args)
    {

        string siteUrl = "http://siteurl";
        string wordAutomationServiceName = "Word Automation Services";
        using (SPSite spSite = new SPSite(siteUrl))
        {
            ConversionJob job = new ConversionJob(wordAutomationServiceName);
            job.UserToken = spSite.UserToken;
            job.Settings.UpdateFields = true;
            job.Settings.OutputFormat = SaveFormat.PDF;
            job.AddFile(siteUrl + "/Shared%20Documents/Test.docx",
            siteUrl + "/Shared%20Documents/Test.pdf");
            job.Start();
        }
    }
}

这也没有奏效 我觉得代码中没有错误必须是sharepoint设置的一些问题

2 个答案:

答案 0 :(得分:0)

你有一个Document对象吗?

我不确定你是否可以在你的上下文中使用它,但如果是,你可以尝试:

docObject.ExportAsFixedFormat("Yourdoc.pdf", WdExportFormat.wdExportFormatPDF, false,
                        WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0,
                        WdExportItem.wdExportDocumentContent, true, true,
                        WdExportCreateBookmarks.wdExportCreateNoBookmarks, true, false, false, ref oMissing);

更多信息here

答案 1 :(得分:-1)

我假设您从here获取了代码。你在哪里wordAutomationServiceName?因为它需要与您在管理中心中配置的Word Automation Service的名称相匹配。

还要检查SharePoint TraceLog(14 / LOGS)或EventViewer以获取更多信息/错误。另请参阅有关如何配置Word Automation Services for Development的this文章。

How Word Automation Services Work