ImageGear .NET v25.2 - Updated
Developer Guide / How to Work with... / PDF / How to... / Convert... / Microsoft Office Documents to PDF
In This Topic
    Microsoft Office Documents to PDF
    In This Topic

    ImageGear .NET provides the ability to convert any Microsoft Office Open XML and Microsoft Office 97-2003 document currently supported by the product or any page of a Microsoft Office Open XML or Microsoft Office 97-2003 document and save the converted content to a PDF file. This can be done by using one of the following API calls:

    ImageGear .NET Office to PDF converter provides the conversion of all currently supported text elements, raster images, and graphic shapes for Microsoft Office Open XML and Microsoft Office 97-2003 formats described in Using ImageGear.Formats.Office Namespace.

    Note the following conversions that take place when converting to PDF:

    • Gradients with Transparency: Microsoft Office Open XML gradients with transparency are converted as non-transparent PDF gradients.
    • Text with Gradients: Microsoft Office Open XML text with gradient fill and stroke is converted to solid PDF text with the color of the first gradient stop.
    • Patterns: Microsoft Office Open XML text with patterns is converted to solid black PDF text.

    Word (RTF/DOCX/DOC) to PDF

    This sample shows how to input a Word document in the RTF/DOCX/DOC format and output all pages as a PDF document.

    After some initializations, load the necessary ImGear filters to create an instance of Microsoft Word format for input and an instance of PDF format for output using code that looks like:

     C#
    Copy Code
    ImGearFileFormats.Filters.Add(ImGearOffice.CreateWordFormat());
    ImGearFileFormats.Filters.Add(ImGearOffice.CreateRTFFormat());
    ImGearFileFormats.Filters.Add(ImGearPDF.CreatePDFFormat());
    
    VB.NET
    Copy Code
    ImGearFileFormats.Filters.Add(ImGearOffice.CreateWordFormat())
    ImGearFileFormats.Filters.Add(ImGearOffice.CreateRTFFormat())
    ImGearFileFormats.Filters.Add(ImGearPDF.CreatePDFFormat())
    

    Next, the PDF library requires its own initialization:

     C#
    Copy Code
    ImGearPDF.Initialize();
    
    VB.NET
    Copy Code
    ImGearPDF.Initialize()
    

    Then, simply read in all pages of the Word document using the ImGearFileFormats.LoadDocument() method:

     C#
    Copy Code
    ImGearDocument igDocument;
    using (FileStream fileStream = new FileStream(inputFileName, FileMode.Open,
           FileAccess.Read, FileShare.Read))
    {
       igDocument = ImGearFileFormats.LoadDocument(fileStream);
    }
    
    VB.NET
    Copy Code
    Dim igDocument As ImGearDocument
    Using fileStream As New FileStream(inputFileName, FileMode.Open, FileAccess.Read, FileShare.Read)
        igDocument = ImGearFileFormats.LoadDocument(fileStream)
    End Using
    

    Finally, write out the document as PDF using the ImGearFileFormats.SaveDocument() method with the saving format set to ImGearSavingFormats.PDF and no special options:

     
    Copy Code
    using (FileStream fileStream = new FileStream(outputFileName, FileMode.Create,
           FileAccess.ReadWrite))
    {
       ImGearFileFormats.SaveDocument(igDocument, fileStream, 0,
           ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, null);
    }
    
    VB.NET
    Copy Code
    Using fileStream As New FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite)
        ImGearFileFormats.SaveDocument(igDocument, fileStream, 0, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.PDF, Nothing)
    End Using
    

    Excel (XLSX/XLS) to PDF

    This sample can be easily adjusted to accept Excel Spreadsheet XLSX/XLS documents as input.

    To accept XLSX/XLS files as input, create an instance of Microsoft Excel format after initializing ImageGear .NET:

     C#
    Copy Code
    ImGearFileFormats.Filters.Add(ImGearOffice.CreateExcelFormat());
    
    VB.NET
    Copy Code
    ImGearFileFormats.Filters.Add(ImGearOffice.CreateExcelFormat())
    

    and modify the sample's open file dialog to accept the *.xlsx/*.xls extensions:

    C# 
    Copy Code
    ofd.Filter = @"DOCX files (*.docx)|*.docx|XLSX files (*.xlsx)|*.xlsx|XLS files (*.xls)|*.xls";
    
    VB.NET
    Copy Code
    ofd.Filter = "DOCX files (*.docx)|*.docx|XLSX files (*.xlsx)|*.xlsx|XLS files (*.xls)|*.xls"
    

    PowerPoint (PPTX/PPT) to PDF

    This sample can be easily adjusted to accept PowerPoint PPTX/PPT files as input.

    To accept PPTX files as input, create an instance of Microsoft PowerPoint format after initializing ImageGear .NET:

     C#
    Copy Code
    ImGearFileFormats.Filters.Add(ImGearOffice.CreatePowerPointFormat());
    
    VB.NET
    Copy Code
    ImGearFileFormats.Filters.Add(ImGearOffice.CreatePowerPointFormat())
    

    and modify the sample's open file dialog to accept the *.pptx extension:

    C# 
    Copy Code
    ofd.Filter = @"DOCX files (*.docx)|*.docx|PPTX files (*.pptx)|*.pptx|PPT files (*.ppt)|*.ppt";
    
    VB.NET
    Copy Code
    ofd.Filter = "DOCX files (*.docx)|*.docx|PPTX files (*.pptx)|*.pptx|PPT files (*.ppt)|*.ppt"