[]
        
(Showing Draft Content)

Export

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 format with the help of Grapecity.Documents.Word.Layout (GcWordLayout) 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 GcWordLayout package separates the layout of source Word document from the exported PDF 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.

    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.

    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.

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

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

Export to SVG

In addition to above mentioned common image formats, GcWord also lets you save the Word document pages as SVG or its compressed format SVGZ. You can use SaveAsSvg and ToSvgz methods of the GrapeCity.Documents.Word.Layout.Page class to export an instance of word page to SVG file or stream(.svg) or a byte array(.svgz).

var wordDoc = new GcWordDocument();
wordDoc.Load("StatementOfWork.docx");
using (var la = new GcWordLayout(wordDoc))
{
    var page = la.Pages[0];
     
    // Render a Word page to the .svg file
    page.SaveAsSvg("StatementOfWork.svg", new ImageOutputSettings() { Zoom = 1f }, new XmlWriterSettings() { Indent = true });
 
    // Render a Word page to the byte array with compressed data in SVGZ format
    var svgData = page.ToSvgz();
    File.WriteAllBytes("StatementOfWork.svgz", svgData);
}

Limitations

GcWord has a few limitations while exporting to PDF and image file formats. Some of them, in particular, are:

  • While exporting to SVG, text is always rendered as graphics using paths. Hence, resulting .svg files for text pages are large and it is not possible to select or copy text on the SVG images opened in the browsers.
  • Objects that are not supported by the GcWord OM are not exported.
  • Footnotes are not exported.
  • Export of text boxes is supported, but linked text boxes, text outlines, gradients, font fill and line effect, and the "Do not rotate text" option are not supported.
  • When exporting shapes, custom shapes, ink shapes, sketched lines, gradient lines, and compound lines are not supported.
  • Only stored values in fields are exported (i.e. no recalculation), with the following exceptions:
    • Page numbering is supported.
    • Partial hyperlink field is supported.
  • Comments can optionally be exported, but complete formatting is not retained.