The PDF document can be generated from the print document by using the // Event handler to save the PrintDocument page as image. Save PrintDocument to Image: Need help understanding this procedure. For that I use PrintDocument, for preview PrintPreview. I have a Could you tell me how to save printdocument to PDF format, please??.
||2 April 2011
|PDF File Size:
|ePub File Size:
||Free* [*Free Regsitration Required]
This article describes the implementation of an enhanced PrintPreviewDialog class that provides PDF output in addition to the standard print and preview capabilities.
Enhanced PrintPreviewDialog Class with PDF Output
The PrintPreviewDialog is convenient and easy to use. All you need to do is create an instance of the dialog class, assign your PrintDocument object to the Document property, and call the ShowDialog method.
The CoolPrintPreviewDialog class presented here addresses these shortcomings. It is just as easy to use as the standard PrintPreviewDialogbut has the following enhancements:. You instantiate the control, set the Document property to the PrintDocument you want to preview, then call the dialog’s Show method. If you have code that uses the PrintPreviewDialog class, switching to the CoolPrintPreviewDialog only requires changing one line of code.
The PrintDocument object dave a PrintController property that specifies an object responsible for creating the Graphics objects where the document is rendered. The default print controller creates Graphics objects for the default printer and is not interesting in this case. These remain available to the caller to be shown in the preview area. The CoolPreviewControl works by temporarily replacing the document’s printdocujent print controller with a PreviewPrintControllercalling the document’s Print method, and getting the page images while the document is rendered.
The images represent pages in the document, and are scaled and displayed in the control just like any regular Image object. The code that creates the page previews looks like this this code is simplified for clarity, refer to the source for a better version:.
Forums – C# Corner
The code installs the controller and hooks up the event handlers, then calls the Print method to generate the pages, and cleans up when it’s done. When the Print method is invoked, the document starts firing events. The PrintPage and EndPrint event handlers capture the pages as soon as they are rendered and add them to an internal image list. The event handlers also call the Application. DoEvents method to keep the dialog responsive to user actions while the document renders.
This allows users to switch pages, adjust the zoom factor, or cancel the document generation process. Without this call, the dialog would stop operating until the whole document finishes rendering. This is the core of the preview code. The rest is concerned with housekeeping tasks such as scaling the preview images, updating the scrollbars, handling navigation buttons, mouse, keyboard, and so on.
Please refer to the source code for the implementation details. The preview dialog allows users to update the print layout. This is very easy to implement, thanks to the.
Here is the code that gets called when users click the “Page Layout” button:. The code shows a PageSetupDialog that allows the user to change the paper size, orientation, and margins. Changes made by the user are reflected in the document’s DefaultPageSettings property. If the user clicks OK, then we assume that the page layout has been modified, and call the RefreshPreview method on the preview control. This method regenerates all preview images using the new settings, so the user can see the changes applied to margins, page orientation, and so on.
When the user clicks the “Print” button, the dialog shows a PrintDialog so the user can select the printer, page range, or change his mind and cancel the printing.
Unfortunately, page range selections are not honored if you simply #c the Print method directly on the document. To remedy this, the dialog calls the Print method on the enhanced preview control instead. That implementation uses the page images already stored in the prijtdocument, and honors page ranges defined in the document’s PrinterSettings properties.
The Print method in the preview control starts by determining the range of pages that should be rendered. This may be the full document, a specific range, or the current selection page being previewed. Once the page range has been determined, the code creates a DocumentPrinter helper class to prinrdocument the actual printing:.
The DocumentPrinter class is simple. It inherits from PrintDocument and overrides the OnPrintPage method to print only the pages selected by the user:. This implementation renders the page images assuming all pages have the same size and orientation, which is the case for most documents. If the document contains pages of different sizes, or with different orientation, this simple implementation will not work correctly.
To fix this, we would have to check that the current paper size and orientation match the preview image size before printing each page and adjust prinfdocument printer settings if necessary. That is left as an exercise for the reader. The PDF format is extremely popular because it is compact and portable. PDF files can be posted on the web, distributed by e-mail, and viewed or printed almost anywhere. Using this approach, any application that uses the PrintDocument class to provide printing and previewing can add a PDF export feature with minimal effort.
For example, you may want to provide a PDF export button on the main application, that does not cause a preview dialog to appear at all. The main public method in the class is called RenderDocument.
This method starts by assigning a PreviewPrintController to the document, which causes pages to be rendered into metafile images. Both of these classes are part of the.
Convert printdocument to PDF
Once the controller has printdcument installed, the code connects event handlers for the PrintPage and EndPrint events. These event handlers are responsible for rendering the page images into the PDF document. Once the event handlers are installed, the code invokes the Print method to render the document. The method returns a boolean value that indicates whether the document was fully generated and should be saved or whether the user canceled the process by pressing the “Cancel” button on the optional progress dialog.
The document event handlers are listed below. As each page is rendered, the v# call the DrawPage method to render the page images metafiles into the PDF document. This is done using the C1PdfDocument. The metafile is never converted into a raster image, which means the content remains scalable, searchable, and compact.
After I posted the first version of this project, I got some great feedback from other CodeProject users. One of them mentioned printocument problem I also had a while ago.
If the document contains several thousand pages, caching all those images may cause problems. Windows has a limit of 10, GDI objects, and each page image represents printdocumet least one.
If you use too many GDI objects, your application may crash, or cause other apps to crash. One easy way to solve this problem is to convert the page images into streams. You can then store the streams and create images on demand, only when they are needed for previewing or printing.
The code below shows a PageImageList class that does the job. You can use it much like a regular Listexcept when you get or set an image, it is automatically converted to and from a byte array. This way, the images stored in the list are not Tto objects and don’t use up the system resources. Note that the Add method disposes of the image after storing it. Normally we would not do printdocjment this way since the caller owns the image and should be responsible for disposing of it.
But in this project, this arrangement allows us to swap the PageImageList implementation with a regular Listwhich is convenient for testing and benchmarking. This API makes the metafile invalid, so the image cannot be used after this method is called. To avoid destroying the original image, the method creates a clone first, then gets the bits from the clone.
I think this is a printdocumejt price to pay for the benefit if your documents have a few hundred or a few thousand pages. And in case you are concerned about memory usage, consider compressing the byte v# when you store them. It is easy to do using C1Zipand metafiles tend to compress really well.
Of course, there would be a small additional performance penalty involved. We implemented an enhanced PrintPreviewDialog class that provides PDF output in addition to the standard print and preview capabilities. If you have requests or suggestions for improving this document or the application, please post on our site. Download the full source code: ComponentOne Studio Safe, v3.
ComponentOne Studio for Xamarin, v3. For more information, contact Amy Reese at press grapecity. Introduction This article describes the implementation of an enhanced PrintPreviewDialog class that provides PDF output in addition to the standard print and preview capabilities.
Background The PrintPreviewDialog is convenient and easy to prindocument. However, PrintPreviewDialog has some shortcomings, including the following: The entire document must be rendered before the preview appears.
This is annoying for long documents.
There are no options for choosing the printer, adjusting the page layout, or selecting specific pages to print. The dialog looks outdated. It hasn’t changed since. The dialog allows little or no customization.
There is no option to export the document to other formats such as PDF. Page images are cached in the control, which limits the size of the documents that can be previewed. It is just as easy to use as the standard PrintPreviewDialogbut has the following enhancements: Pages can be previewed as soon as they are rendered. Pritndocument first page is shown almost instantly and subsequent pages become available while the user browses the first ones.
The “Print” button shows a dialog that allows users to select the printer and page ranges to print. A “Page Layout” button is also available so users can change page size, orientation, and margins.