Search code examples
c#.netsharepoint-2010sharepoint-2007

How to perform a PDF conversion in Event Receiver?


I am trying to convert the document a .docx file available in document library(conversion) to PDF format in same library whenever the file is updated.

The code I am using is given below:

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();
           }

When I run it in debug mode, it executes without any error or exception but it is not generating any PDF file.

I am not able to find out what the problem is

it tried this simple code

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();
        }
    }
}

This also did nt workd what i feel is there is no error in code must be some problem with sharepoint settings


Solution

  • Do you have a Document object?

    I'm not sure if you can use this in your context, but if yes, you could try:

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

    More info here