[]
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.
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:
Load a Word document in GcWordLayout instance.
Save the Word document as PDF by using SaveAsPdf method of GcWordLayout class.
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 });
}
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:
Load a Word document in GcWordLayout instance.
Save the Word document as TIFF image by using SaveAsTiff method of GcWordLayout class.
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:
Create a new Word document by instantiating the GcWordDocument class.
Add content to the document by using Add method of ParagraphCollection class and specifying various BuiltInStyles by using BuiltInStyleId enumeration.
Load the document in GcWordLayout instance.
Save the first page of Word document as JPEG image by using SaveAsJpeg method of GcWordLayout class.
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.
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: