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.
DsWord allows you to export Word documents to PDF format with the help of GcWordLayout class. This class 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:
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 }); } |
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:
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:
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 }); } |
For more information on how to convert a Word document into PDF and image formats using DsWord, see DsWord sample browser.
In addition to above mentioned common image formats, DsWord 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).
C# |
Copy Code |
---|---|
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
DsWord has a few limitations while exporting to PDF and image file formats. Some of them, in particular, are: