officefileapi-116859-word-processing-document-api-examples-printing-how-to-show-a-print-preview-form.md
This example demonstrates how to invoke the Print Preview form using Word Processing Document API. To replicate this example, perform the following steps.
The following code snippet uses RichEditDocumentServer to create a new document, modify and print it.
Note
The code sample uses the PrintingSystem class. Make sure that the DevExpress.XtraPrinting.v25.2.dll assembly is added to your project.
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraPrinting;
RichEditDocumentServer wordProcessor = new RichEditDocumentServer();
// Specify default formatting
wordProcessor.Document.DefaultParagraphProperties.Alignment = ParagraphAlignment.Center;
// Specify page settings
wordProcessor.Document.Sections[0].Page.Landscape = true;
wordProcessor.Document.Sections[0].Page.Height = DevExpress.Office.Utils.Units.InchesToDocumentsF(10.0f);
wordProcessor.Document.Sections[0].Page.Width = DevExpress.Office.Utils.Units.InchesToDocumentsF(4.5f);
// Add document content
wordProcessor.Document.AppendText("This content is created programmatically\n");
ricwordProcessorhServer.Document.Paragraphs.Append();
//Create a table
Table _table = wordProcessor.Document.Tables.Create(wordProcessor.Document.Selection.Start, 8, 8, AutoFitBehaviorType.FixedColumnWidth);
_table.BeginUpdate();
_table.Borders.InsideHorizontalBorder.LineThickness = 1;
_table.Borders.InsideHorizontalBorder.LineStyle = BorderLineStyle.Double;
_table.Borders.InsideVerticalBorder.LineThickness = 1;
_table.Borders.InsideVerticalBorder.LineStyle = BorderLineStyle.Double;
_table.TableAlignment = TableRowAlignment.Center;
_table.ForEachCell((cell, rowIndex, columnIndex) =>
{
wordProcessor.Document.InsertText(cell.Range.Start, String.Format("{0}*{1} is {2}",
rowIndex + 2, columnIndex + 2, (rowIndex + 2) * (columnIndex + 2)));
});
_table.EndUpdate();
// Invoke the Print Preview dialog
using (PrintingSystem printingSystem = new PrintingSystem())
{
using (PrintableComponentLink link = new PrintableComponentLink(printingSystem))
{
link.Component = wordProcessor;
link.CreateDocument();
link.ShowPreviewDialog();
}
}
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraPrinting
Dim wordProcessor As New RichEditDocumentServer()
' Specify default formatting
wordProcessor.Document.DefaultParagraphProperties.Alignment = ParagraphAlignment.Center
' Specify page settings
wordProcessor.Document.Sections(0).Page.Landscape = True
wordProcessor.Document.Sections(0).Page.Height = DevExpress.Office.Utils.Units.InchesToDocumentsF(10.0F)
wordProcessor.Document.Sections(0).Page.Width = DevExpress.Office.Utils.Units.InchesToDocumentsF(4.5F)
' Add document content
wordProcessor.Document.AppendText("This content is created programmatically" & Constants.vbLf)
wordProcessor.Document.Paragraphs.Append()
' Create a table
wordProcessor.BeginUpdate()
Dim _table As Table = wordProcessor.Document.Tables.Create(wordProcessor.Document.Selection.Start, 8, 8, AutoFitBehaviorType.FixedColumnWidth)
_table.BeginUpdate()
_table.Borders.InsideHorizontalBorder.LineThickness = 1
_table.Borders.InsideHorizontalBorder.LineStyle = BorderLineStyle.Double
_table.Borders.InsideVerticalBorder.LineThickness = 1
_table.Borders.InsideVerticalBorder.LineStyle = BorderLineStyle.Double
_table.TableAlignment = TableRowAlignment.Center
_table.ForEachCell(Sub(cell, rowIndex, columnIndex)
wordProcessor.Document.InsertText(cell.Range.Start, String.Format("{0}*{1} is {2}",
rowIndex + 2, columnIndex + 2, (rowIndex + 2) * (columnIndex + 2)))
End Sub)
_table.EndUpdate()
wordProcessor.EndUpdate()
' Invoke the Print Preview dialog
Using printingSystem As New PrintingSystem()
Using link As New PrintableComponentLink(printingSystem)
link.Component = wordProcessor
link.CreateDocument()
link.ShowPreviewDialog()
End Using
End Using
The result is illustrated below.