How to: Show a Print Preview Form

  • 3 minutes to read

This example demonstrates how to invoke the Print Preview form using Word Processing Document API. To replicate this example, perform the following steps.

  1. Create a RichEditDocumentServer instance in code.
  2. Specify page settings for the first section in a document using the properties of the SectionPage object accessed via Section.Page.
  3. Specify document content. In this example a simple multiplication table is inserted programmatically in a document.
  4. Create a new PrintingSystem instance for creating and printing a document.
  5. Create a PrintableComponentLink printing link with the specified printing system and assign the document server to the PrintableComponentLinkBase.Component property.
  6. Call the LinkBase.CreateDocument method to generate a report and use the PrintingSystem.PreviewFormEx property to access the Print Preview form used to display a document preview. Call the PrintPreviewFormExBase.ShowDialog method to invoke the form.

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.v21.1.dll assembly is added to your project.

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraPrinting;

RichEditDocumentServer richServer = new RichEditDocumentServer();
// Specify default formatting
richServer.Document.DefaultParagraphProperties.Alignment = ParagraphAlignment.Center;
// Specify page settings
richServer.Document.Sections[0].Page.Landscape = true;
richServer.Document.Sections[0].Page.Height = DevExpress.Office.Utils.Units.InchesToDocumentsF(10.0f);
richServer.Document.Sections[0].Page.Width = DevExpress.Office.Utils.Units.InchesToDocumentsF(4.5f);
// Add document content
richServer.Document.AppendText("This content is created programmatically\n");
richServer.Document.Paragraphs.Append();

//Create a table
Table _table = richServer.Document.Tables.Create(richServer.Document.Selection.Start, 8, 8, AutoFitBehaviorType.FixedColumnWidth);
_table.BeginUpdate();
_table.Borders.InsideHorizontalBorder.LineThickness = 1;
_table.Borders.InsideHorizontalBorder.LineStyle = TableBorderLineStyle.Double;
_table.Borders.InsideVerticalBorder.LineThickness = 1;
_table.Borders.InsideVerticalBorder.LineStyle = TableBorderLineStyle.Double;
_table.TableAlignment = TableRowAlignment.Center;

_table.ForEachCell((cell, rowIndex, columnIndex) =>
{
    richServer.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 = richServer;
        link.CreateDocument();
        link.ShowPreviewDialog();
    }
}

The result is illustrated below.

PrintPreviewDialog_Example