GetDotNetPaperSize

PageInfo.GetDotNetPaperSize method

Gets the PaperSize object suitable for printing the page represented by this PageInfo.

public PaperSize GetDotNetPaperSize(PaperSizeCollection paperSizes)
ParameterTypeDescription
paperSizesPaperSizeCollectionAvailable paper sizes.

Return Value

An object that you can use in the .NET printing framework to specify the paper size.

Examples

Shows how to customize the printing of Aspose.Words documents.

Document doc = new Document(MyDir + "Rendering.docx");

    MyPrintDocument printDoc = new MyPrintDocument(doc);
    printDoc.PrinterSettings.PrintRange = System.Drawing.Printing.PrintRange.SomePages;
    printDoc.PrinterSettings.FromPage = 1;
    printDoc.PrinterSettings.ToPage = 1;

    printDoc.Print();
}

/// <summary>
/// Selects an appropriate paper size, orientation, and paper tray when printing.
/// </summary>
public class MyPrintDocument : PrintDocument
{
    public MyPrintDocument(Document document)
    {
        mDocument = document;
    }

    /// <summary>
    /// Initializes the range of pages to be printed according to the user selection.
    /// </summary>
    protected override void OnBeginPrint(PrintEventArgs e)
    {
        base.OnBeginPrint(e);

        switch (PrinterSettings.PrintRange)
        {
            case System.Drawing.Printing.PrintRange.AllPages:
                mCurrentPage = 1;
                mPageTo = mDocument.PageCount;
                break;
            case System.Drawing.Printing.PrintRange.SomePages:
                mCurrentPage = PrinterSettings.FromPage;
                mPageTo = PrinterSettings.ToPage;
                break;
            default:
                throw new InvalidOperationException("Unsupported print range.");
        }
    }

    /// <summary>
    /// Called before each page is printed. 
    /// </summary>
    protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
    {
        base.OnQueryPageSettings(e);

        // A single Microsoft Word document can have multiple sections that specify pages with different sizes, 
        // orientations, and paper trays. The .NET printing framework calls this code before 
        // each page is printed, which gives us a chance to specify how to print the current page.
        PageInfo pageInfo = mDocument.GetPageInfo(mCurrentPage - 1);
        e.PageSettings.PaperSize = pageInfo.GetDotNetPaperSize(PrinterSettings.PaperSizes);

        // Microsoft Word stores the paper source (printer tray) for each section as a printer-specific value.
        // To obtain the correct tray value, you will need to use the "RawKind" property, which your printer should return.
        e.PageSettings.PaperSource.RawKind = pageInfo.PaperTray;
        e.PageSettings.Landscape = pageInfo.Landscape;
    }

    /// <summary>
    /// Called for each page to render it for printing. 
    /// </summary>
    protected override void OnPrintPage(PrintPageEventArgs e)
    {
        base.OnPrintPage(e);

        // Aspose.Words rendering engine creates a page drawn from the origin (x = 0, y = 0) of the paper.
        // There will be a hard margin in the printer, which will render each page. We need to offset by that hard margin.
        float hardOffsetX, hardOffsetY;

        // Below are two ways of setting a hard margin.
        if (e.PageSettings != null && e.PageSettings.HardMarginX != 0 && e.PageSettings.HardMarginY != 0)
        {
            // 1 -  Via the "PageSettings" property.
            hardOffsetX = e.PageSettings.HardMarginX;
            hardOffsetY = e.PageSettings.HardMarginY;
        }
        else
        {
            // 2 -  Using our own values, if the "PageSettings" property is unavailable.
            hardOffsetX = 20;
            hardOffsetY = 20;
        }

        mDocument.RenderToScale(mCurrentPage, e.Graphics, -hardOffsetX, -hardOffsetY, 1.0f);

        mCurrentPage++;
        e.HasMorePages = mCurrentPage <= mPageTo;
    }

    private readonly Document mDocument;
    private int mCurrentPage;
    private int mPageTo;
}

See Also