Renders document pages into a .NET Graphics object. The rendered pages look like output from Microsoft Word.
For a list of all members of this type, see PageRenderer Members.
System.Object
Aspose.Editor.Client.PageRenderer
[Visual Basic]
Public Class PageRenderer
[C#]public class PageRenderer
Remarks
PageRenderer allows you to quickly and easily create thumbnails or images of individual document pages as well as implement print and preview. All this is possible thanks to the versatility of GDI+ and its main class Graphics.
You can obtain or create a Graphics object that represents a display, raster image, metafile, or a printer device. Pass this Graphics object to PageRenderer and the document will be rendered on that device.
The Aspose.Editor.Demo project with source code is included in the Aspose.Editor installer. The demo project shows how to use PageRenderer to create PNG, JPEG and EMF files of document pages (one page per file), as well as how to create a single multipage TIFF file that contains all pages of a document. You can also specify image scale or dimensions as well as desired resolution.
Example
Shows how to print an Aspose.Editor document to a printer using .NET printing classes.
[C#]
/// <summary>
/// Called when the user requests to print the document.
/// </summary>
private void FilePrint(object sender, System.EventArgs e)
{
// We use the standard .NET print document infrastructure.
PrintDialog dlg = new PrintDialog();
MyPrintDocument printDoc = new MyPrintDocument(editorControl.Document, editorControl.Options.IsShowFormattingMarks);
dlg.Document = printDoc;
dlg.AllowSomePages = true;
dlg.PrinterSettings.MinimumPage = 1;
dlg.PrinterSettings.MaximumPage = printDoc.PageCount;
dlg.PrinterSettings.FromPage = dlg.PrinterSettings.MinimumPage;
dlg.PrinterSettings.ToPage = dlg.PrinterSettings.MaximumPage;
if (dlg.ShowDialog().Equals(DialogResult.OK))
printDoc.Print();
}
}
/// <summary>
/// Represents an Aspose.Editor.Document that can be printed using standard .NET printing infrastructure.
/// </summary>
public class MyPrintDocument : PrintDocument
{
/// <summary>
/// Ctor. Creates a PrintDocument that wraps an Aspose.Editor.Document for the purpose of printing.
/// </summary>
/// <param name="doc">The Aspose.Editor.Document to print.</param>
/// <param name="isShowFormattingMarks">True to show formatting marks.</param>
public MyPrintDocument(Document doc, bool isShowFormattingMarks)
{
if (doc == null)
throw new ArgumentNullException("doc");
// Aspose.Editor.PageRenderer is the class that renders the document to a Graphics object.
mRenderer = new PageRenderer(doc);
mRenderer.Options.IsShowControlCharacters = isShowFormattingMarks;
}
protected override void OnBeginPrint(PrintEventArgs e)
{
base.OnBeginPrint(e);
switch (PrinterSettings.PrintRange)
{
case PrintRange.AllPages:
mCurPageNumber = 1;
mMaxPageNumber = mRenderer.PageCount;
break;
case PrintRange.SomePages:
mCurPageNumber = PrinterSettings.FromPage;
mMaxPageNumber = PrinterSettings.ToPage;
break;
default:
throw new ApplicationException("Unsupported print range.");
}
}
protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);
// You can use smoothing mode for drawing lines.
// This will make borders appear more exact, although smudged.
// renderer.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
// TextRenderingHint.AntiAliasGridFit results in the best looking text.
// Although the layout is correct like in Microsoft Word, the actual text outputted
// to the screen can be shorter due to grid fitting.
// If you want text to be outputted exactly, use TextRenderingHint.AntiAlias,
// but it will look a bit smudged.
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
// Render the page at the top left corner of the graphics object and at 100% zoom.
// Renderer requires zero based page index, not a page number.
int pageIdx = mCurPageNumber - 1;
mRenderer.Render(pageIdx, e.Graphics, 0, 0, 1.0f);
mCurPageNumber++;
e.HasMorePages = (mCurPageNumber <= mMaxPageNumber);
}
public int PageCount
{
get { return mRenderer.PageCount; }
}
private PageRenderer mRenderer;
/// <summary>
/// 1-based number of the current page.
/// </summary>
private int mCurPageNumber;
private int mMaxPageNumber;
}
[Visual Basic]
''' <summary>
''' Called when the user requests to print the document.
''' </summary>
Private Sub FilePrint(ByVal sender As Object, ByVal e As System.EventArgs)
' We use the standard .NET print document infrastructure.
Dim dlg As New PrintDialog()
Dim printDoc As New MyPrintDocument(editorControl.Document, editorControl.Options.IsShowFormattingMarks)
dlg.Document = printDoc
dlg.AllowSomePages = True
dlg.PrinterSettings.MinimumPage = 1
dlg.PrinterSettings.MaximumPage = printDoc.PageCount
dlg.PrinterSettings.FromPage = dlg.PrinterSettings.MinimumPage
dlg.PrinterSettings.ToPage = dlg.PrinterSettings.MaximumPage
If dlg.ShowDialog().Equals(DialogResult.OK) Then
printDoc.Print()
End If
End Sub
End Class
''' <summary>
''' Represents an Aspose.Editor.Document that can be printed using standard .NET printing infrastructure.
''' </summary>
Public Class MyPrintDocument
Inherits PrintDocument
''' <summary>
''' Ctor. Creates a PrintDocument that wraps an Aspose.Editor.Document for the purpose of printing.
''' </summary>
''' <param name="doc">The Aspose.Editor.Document to print.</param>
''' <param name="isShowFormattingMarks">True to show formatting marks.</param>
Public Sub New(ByVal doc As Document, ByVal isShowFormattingMarks As Boolean)
If doc Is Nothing Then
Throw New ArgumentNullException("doc")
End If
' Aspose.Editor.PageRenderer is the class that renders the document to a Graphics object.
mRenderer = New PageRenderer(doc)
mRenderer.Options.IsShowControlCharacters = isShowFormattingMarks
End Sub
Protected Overrides Sub OnBeginPrint(ByVal e As PrintEventArgs)
MyBase.OnBeginPrint(e)
Select Case PrinterSettings.PrintRange
Case PrintRange.AllPages
mCurPageNumber = 1
mMaxPageNumber = mRenderer.PageCount
Case PrintRange.SomePages
mCurPageNumber = PrinterSettings.FromPage
mMaxPageNumber = PrinterSettings.ToPage
Case Else
Throw New ApplicationException("Unsupported print range.")
End Select
End Sub
Protected Overrides Sub OnPrintPage(ByVal e As PrintPageEventArgs)
MyBase.OnPrintPage(e)
' You can use smoothing mode for drawing lines.
' This will make borders appear more exact, although smudged.
' renderer.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
' TextRenderingHint.AntiAliasGridFit results in the best looking text.
' Although the layout is correct like in Microsoft Word, the actual text outputted
' to the screen can be shorter due to grid fitting.
' If you want text to be outputted exactly, use TextRenderingHint.AntiAlias,
' but it will look a bit smudged.
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
' Render the page at the top left corner of the graphics object and at 100% zoom.
' Renderer requires zero based page index, not a page number.
Dim pageIdx As Integer = mCurPageNumber - 1
mRenderer.Render(pageIdx, e.Graphics, 0, 0, 1.0f)
mCurPageNumber += 1
e.HasMorePages = (mCurPageNumber <= mMaxPageNumber)
End Sub
Public ReadOnly Property PageCount() As Integer
Get
Return mRenderer.PageCount
End Get
End Property
Private mRenderer As PageRenderer
''' <summary>
''' 1-based number of the current page.
''' </summary>
Private mCurPageNumber As Integer
Private mMaxPageNumber As Integer
End ClassRequirements
Namespace: Aspose.Editor.Client
Assembly: Aspose.Editor.Client (in Aspose.Editor.Client.dll)
See Also
PageRenderer Members | Aspose.Editor.Client Namespace