Documents for Word, .NET Edition
Features / Export
In This Topic
    Export
    In This Topic

    Exporting a Word document to PDF and images is a very common use case and is generally required for various business purposes. Some of the advantages are security, cross-platform compatibility, availability of free readers, reduced file size etc.

    GcWord allows you to export Word documents to PDF and image file formats with the help of Grapecity.Documents.Word.Layout (GcLayout) package. Hence, it is mandatory to install Grapecity.Documents.Word.Layout package while using the export functionality in GcWord.

    Note: The old GrapeCity.Documents.Layout package is replaced by the new Grapecity.Documents.Word.Layout package which has been refactored to improve the functionality.

    The GcWordLayout class in GcLayout package separates the layout of source Word document from the exported PDF or image formats. You can also use the WordLayoutSettings class to perform various customizations of the set of pages being exported.

    Export to PDF

    The SaveAsPdf method of GcWordLayout class can be used to export Word documents to PDF. You can also set various PDF options like compression level, back color, metadata, PDF version by using properties of PdfOutputSettings class.

    To export a Word document to PDF:

    1. Load a Word document in GcWordlayout instance.
    2. Save the Word document as PDF by using SaveAsPdf method of GcWordlayout class.
    3. Use PdfOutputSettings class to specify the CompressionLevel as Fastest.
    C#
    Copy Code
    var wordDoc = new GcWordDocument();
    wordDoc.Load("SimpleText.docx");
    using (var layout = new GcWordLayout(wordDoc))
    {
    // save the whole document as PDF
    layout.SaveAsPdf("SimpleText.pdf", null, new PdfOutputSettings() { CompressionLevel = CompressionLevel.Fastest });
    }


    Export to Image

    You can save Word Documents to TIFF, PNG and JPEG image formats by using SaveAsTiff, SaveAsPng and SaveAsJpeg methods respectively. The properties of ImageOutputSettings class can be used to choose from various options like zoom, back color and resolution while saving to image files.

    To export a Word document to TIFF image format:

    1. Load a Word document in GcWordlayout instance.
    2. Save the Word document as TIFF image by using SaveAsTiff method of GcWordlayout class.
    3. Use ImageOutputSettings class to specify the Compression as Deflate.
    C#
    Copy Code
    var wordDoc = new GcWordDocument();
    wordDoc.Load("JsFrameworkExcerpt.docx");
            
    using (var layout = new GcWordLayout(wordDoc))
    {
    // save a few pages of the Word document as TIFF
    layout.SaveAsTiff("JsFrameworkExcerpt.tiff", new OutputRange("2, 6-7"),
    new ImageOutputSettings() { Zoom = 2f }, new TiffFrameSettings() { Compression = TiffCompression.Deflate });        
    }


    To export a single page of Word document to JPEG image format:

    1. Create a new Word document by instantiating the GcWordDocument class.
    2. Add content to the document by using Add method of ParagraphCollection class and specifying various BuiltInStyles by using BuiltInStyleId enumeration.
    3. Load the document in GcWordlayout instance.
    4. Save the first page of Word document as JPEG image by using SaveAsJpeg method of GcWordlayout class.
    5. Use ImageOutputSettings class to specify the Zoom and BackColor properties.
    C#
    Copy Code
    var doc = new GcWordDocument();
    var sec = doc.Body.Sections.First;
    var pars = sec.GetRange().Paragraphs;
    
    // Title
    pars.Add("Some Common Built-in Styles (Title)", doc.Styles[BuiltInStyleId.Title]);
    
    // Subtitle
    pars.Add("Demonstration of some of the built-in styles. (Subtitle)", doc.Styles[BuiltInStyleId.Subtitle]);
    
    // Headings 1-4
    var heading1 = pars.Add("Heading 1", doc.Styles[BuiltInStyleId.Heading1]);
    var heading2 = pars.Add("Heading 2", doc.Styles[BuiltInStyleId.Heading2]);
    var heading3 = pars.Add("Heading 3", doc.Styles[BuiltInStyleId.Heading3]);
    var heading4 = pars.Add("Heading 4", doc.Styles[BuiltInStyleId.Heading4]);
    
    // Character styles
    var p = pars.Add("In this paragraph we demonstrate some of the built-in character styles. ");
    var runs = p.GetRange().Runs;
    runs.Add("This run uses the 'Strong' style. ", doc.Styles[BuiltInStyleId.Strong]);
    runs.Add("A run of normal text. ");
    runs.Add("This run uses 'Emphasis' style. ", doc.Styles[BuiltInStyleId.Emphasis]);
    runs.Add("A run of normal text. ");
    runs.Add("This run uses 'Intense Emphasis' style. ", doc.Styles[BuiltInStyleId.IntenseEmphasis]);
    runs.Add("A run of normal text. ");
    runs.Add("This run uses 'Subtle Emphasis' style. ", doc.Styles[BuiltInStyleId.SubtleEmphasis]);
    
    pars.Add("The End.");
    using (var layout = new GcWordLayout(doc))
    { 
    layout.Pages[0].SaveAsJpeg("example.jpg", new ImageOutputSettings() { Zoom = 2f, BackColor = Color.Yellow });
    }

     

    Limitations

    For more information on how to convert a Word document into PDF and Image formats using GcWord, see GcWord sample browser.