Hide

SftPrintPreview/OCX 1.0 - ActiveX Print Preview Control

Display
Print

PreviewPages Sample (C#)

This sample illustrates application-generated output.

The source code is located at C:\Program Files (x86)\Softelvdm\SftPrintPreview OCX 1.0\Samples\Visual Studio - CSharp\PreviewPages\Form1.cs or C:\Program Files\Softelvdm\SftPrintPreview OCX 1.0\Samples\Visual Studio - CSharp\PreviewPages\Form1.cs (on 32-bit Windows versions).

private void menuExit_Click(object sender, System.EventArgs e) {
    Application.Exit();
}

private void Form1_Load(object sender, System.EventArgs e)
{
    axSftPrintPreview1.Dock = DockStyle.Fill;

    // Use the RenderContent event to render pages
    axSftPrintPreview1.ContentProviderCallback(0, IntPtr.Zero, IntPtr.Zero);

    Show(); // Show this form so we get the form and the messagebox at the same time

    MessageBox.Show("This example demonstrates application-generated output.\r\n\r\nIt prints one bitmap and 50 lines of text a total of 20 times.  " +
        "The included source code illustrates how an application can print one page at a time, with an easy " +
        "mechanism to keep track of restart positions.");
}

private void axSftPrintPreview1_CloseWanted(object sender, System.EventArgs e)
{
    Application.Exit();
}

private void axSftPrintPreview1_HelpWanted(object sender, AxSftPrintPreviewLib10._ISftPrintPreviewEvents_HelpWantedEvent e)
{
    MessageBox.Show("Sorry, this sample doesn't offer a help file");
}

private void axSftPrintPreview1_PageSetupWanted(object sender, System.EventArgs e)
{
    axSftPrintPreview1.PageSetup(0);
}

private void axSftPrintPreview1_RenderContent(object sender, AxSftPrintPreviewLib10._ISftPrintPreviewEvents_RenderContentEvent e)
{
    // This function is called by SftPrintPreview to print pages, to initialize and
    // terminate printing (to prepare and free resources) and for page positioning
    if (e.function == SftPrintPreviewRenderContentConstants.renderSftPrintPreview_Init)
        m_PrintFont = new Font("Arial", 10);
    else if (e.function == SftPrintPreviewRenderContentConstants.renderSftPrintPreview_Term)
        m_PrintFont = null;
    else if (e.function == SftPrintPreviewRenderContentConstants.renderSftPrintPreview_1Page)
        PrintOnePage(ref e.visitedPageInfo, ref e.lastPage);
}

private void PrintOnePage(ref object visitedPageInfo, ref int lastPage)
{
    // Print one page (can occur multiple times)
    // RenderingInfo.CurrentPage has the absolute page # to print (0..n)
    // RenderingInfo.PreviousVisitedPageInfo contains the value you returned in
    // RenderingInfo.VisitedPageInfo when you finished printing the previous page
    // This allows you to simply restart where you left off.
    // The information you return in RenderingInfo.VisitedPageInfo is of type
    // object, so it can be a simple counter or even a complex object, with cached
    // information.

    // In this example we print 1 bitmap, then 50 lines of text,
    // 20 times

    // We use RenderingInfo.PreviousVisitedPageInfo to resume printing.
    // We simply start at 0 and add 1 for each bitmap or line we
    // printed. Using a simple formula, we can determine where we
    // left off.  In an application, the concept remains the same, but
    // instead of a simple counter, you could use RenderingInfo.VisitedPageInfo
    // to store more complex information.

    // Set up a Graphics object where we render the page
    Graphics g = Graphics.FromHdc((IntPtr)axSftPrintPreview1.RenderingInfo.hDC);
    g.PageUnit = GraphicsUnit.Pixel; // set it to pixel mode
    // It is also possible to set it to GraphicsUnit.Inch or GraphicsUnit.Millimeter or the other
    // unit modes.  Information in RenderingInfo is available in pixels and inches.

    long counter = 0;
    SolidBrush brush = new SolidBrush(Color.Black);// Color printers support other colors too
    Rectangle outRect = new Rectangle(axSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.left, axSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.top,
        axSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.right-axSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.left, axSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.bottom-axSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.top);
    if (axSftPrintPreview1.RenderingInfo.PreviousVisitedPageInfo != null)
        counter = (long) axSftPrintPreview1.RenderingInfo.PreviousVisitedPageInfo;// we left off here on the previous page


    // starting output position on this page
    int xPrinter, yPrinter;
    xPrinter = outRect.Left;
    yPrinter = outRect.Top;

    for ( ; ; )
    {
        if (counter >= (1+50)*20) // 1 bitmap, 50 lines of text, 20 times
        {
            // we have printed 1 bitmap and 50 lines 20 times, we're done
            lastPage = axSftPrintPreview1.RenderingInfo.CurrentPage; // return that this is the last page
            break;
        }

        if ((counter % (50+1)) == 0)
        {
            // Print a bitmap

            // get bitmap size
            Bitmap bmp = new Bitmap(pictureBox1.Image);
            int wPrinterPix, hPrinterPix, wDispPix, hDispPix;
            IntPtr hBmp = bmp.GetHbitmap();
            axSftPrintPreview1.GetDDBSize((int) hBmp, (int) axSftPrintPreview1.RenderingInfo.hDC, out wPrinterPix, out hPrinterPix, out wDispPix, out hDispPix);
            // make sure entire image fits on remaining page
            if (yPrinter + hPrinterPix > outRect.Bottom) {
                DeleteObject(hBmp);
                break;
            }
            // print bitmap, horizontally centered
            axSftPrintPreview1.PrintDDB((int) hBmp, axSftPrintPreview1.RenderingInfo.hDC, xPrinter +(outRect.Width-wPrinterPix)/2,
                yPrinter, wPrinterPix, hPrinterPix, wDispPix, hDispPix);
            DeleteObject(hBmp);
            yPrinter += hPrinterPix; // next available space
        }
        else
        {
            // Print line
            // get text size
            SizeF s = g.MeasureString(m_drawText, m_PrintFont);
            // make sure text fits on remaining page
            if (yPrinter + s.Height > outRect.Bottom)
                break;

            Point pt = new Point(outRect.Left + (outRect.Width - (int) s.Width)/2, yPrinter);
            g.DrawString(m_drawText, m_PrintFont, brush, pt);
            yPrinter += (int) s.Height;
        }

        ++counter;  // one more item done
    }
    visitedPageInfo = counter; // we left off here
}

Last Updated 08/13/2020 - (email)
© 2024 Softel vdm, Inc.