Batch PPTx to PDF file coversion in SharePoint 2013

Rajiv Ranjan 21 Reputation points
2021-09-24T13:08:12.637+00:00

In Sharepoint 2013 library Name "A", there are more then 1000 PPTX (Powerpoint) Files within that , I have to convert all these to PDF file. I have used Power Point Automation service for convert those item. But i can only convert 1 Items and store it in Library "B", I wanted to Convert all PPTX items to PDF and store it in Library "B". Below is the Code I have Used, Please help me.

namespace PptxToPdfConversion
{
    class Program
    {
        static void Main(string[] args)
        {
        try
        {
            string siteURL = "My Site URL ";
            using (SPSite site = new SPSite(siteURL))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    Console.WriteLine("Begin conversion");
                    SPFolder docs = web.Folders[siteURL +
                        "/List Name A"];

                    SPFile file = docs.Files[siteURL +
                        "/Presentations/Test.pptx"];

                    Stream fStream = file.OpenBinaryStream();
                    SPFileStream stream = new SPFileStream(web, 0x1000);

                    // Create the presentation conversion request.
                    PresentationRequest request = new PresentationRequest(
                        fStream,
                        ".pptx",
                        stream);


                    IAsyncResult result = request.BeginConvert(
                        SPServiceContext.GetContext(site),
                        null,
                        null);

                        request.EndConvert(result);

                    // Add the converted file to the document library.
                        SPFolder PDFdocs = web.Folders[siteURL +
                            "/List  B"];
                        SPFile newFile = PDFdocs.Files.Add(
                        "Text.pdf",
                        stream,
                        true);
                    Console.WriteLine("Output: {0}", newFile.Url);

                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            Console.WriteLine("Complete");
            Console.ReadKey();
        }
    }
}
}
Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint Server | Development
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,126 Reputation points
    2021-09-27T08:49:35.637+00:00

    Hi @Rajiv Ranjan ,

    You could loop for the files in the folder and convert them like this:

    foreach(SPFile file in docs.Files)
    {
    //convert the file here
    }


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.