We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Loading Existing PDF file and printing only selected area from the required page

Thread ID:

Created:

Updated:

Platform:

Replies:

144277 Apr 28,2019 10:07 AM UTC May 2,2019 12:01 PM UTC WinForms 4
loading
Tags: PdfViewer
Delhiganesh PG
Asked On April 28, 2019 10:07 AM UTC

Hi,
I wish to print pdf file only selected (x1,y1 and x2,y2) coordinates from the specific pdf page. 

Here the steps which i was followed:
1. Created new c# Winform application.
2. Created Button to browse pdf file via opendialog window.
3. Got the pdf file path and name.
4. Here the code i did.
            PdfDocumentView viewer = new PdfDocumentView();
            viewer.Load(pdf_file_path);
            viewer.PrintDocument.PrinterSettings.PrinterName = "My Printer Name";
            viewer.PrinterSettings.PageSize = PdfViewerPrintSize.ActualSize;
            viewer.PrinterSettings.PageOrientation = PdfViewerPrintOrientation.Auto;
            viewer.PrintDocument.DefaultPageSettings.Landscape = false;
            viewer.PrintDocument.DefaultPageSettings.Margins = new Margins(0,0,0,0);
            viewer.PrintDocument.OriginAtMargins = false;
            viewer.PrintDocument.DefaultPageSettings.Landscape = false;
            printPreviewDialog1.Document = viewer.PrintDocument;
            printPreviewDialog1.ShowDialog();
5. printPreviewDialog1 shows full pdf page, not desired section.

6. To Do:
    After browsing  pdf file from the file path, i should calculate total number of pages.
    Then in the particular page, i need to crop particular section and make it has new pdf file or image and to print via desired printer.

7. Here i have attached PDF file, also i have marked in the red color, which part of the section to be printed.


Thanks in advance.



Attachment: Desktop_c5fd963b.zip

Uthandaraja Selva Sundara Kani [Syncfusion]
Replied On April 29, 2019 11:48 AM UTC

Hi Delhiganesh, 
 
Thanks for contacting Syncfusion support. 
 
We cannot print the PDF pages only with particular portion using ‘PdfDocumentView.PrintDocument’. However, we can print the particular portion of a PDF page by exporting it as image using ‘PdfDocumentView.ExportAsImage(int pageIndex)’ method. 
 
Then crop the exported image and print the image using ‘System.Drawing.Printing.PrintDocument’. 
 
Using the below code snippet, we can export and crop the image, 
// ExportAsImage return the bitmap image of any page of the loaded pdf document 
Bitmap image = pdfDocumentView.ExportAsImage(m_printToPage); 
                         
// Select a region to take snapshot  
Rectangle newRect = new Rectangle(150, 100, image.Width / 2, image.Height / 2); 
 
// Use Bitmap.Clone( ) to  save the image in local drive 
System.Drawing.Imaging.PixelFormat format = image.PixelFormat; 
 
Bitmap cloneBitmap = image.Clone(newRect, format); 

 
 
Using below code snippet, we can draw the image while printing, 
PrintDocument printDocument = new PrintDocument(); 
 
printDocument.PrintPage += PrintDocument_PrintPage; 

private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e) 
             e.Graphics.DrawImage(cloneBitmap,0,0);
 
 
Calculate total number of pages: 
 
Using below code snippet, we can calculate total number of pages, 
 
PdfDocumentView viewer = new PdfDocumentView(); 
viewer.Load(pdf_file_path); 
 
// Get the page count 
Int totalPages = viewer.PageCount; 
 
 
 
Please find the sample from below link, 
 
Kindly click the “Print” button to print the PDF pages with selected coordinates, after run the sample. 
 
Please let us know, the details we provided met your requirements. Otherwise, please provide us with more details about the requirement. 
 
Regards, 
Uthandaraja S 


Delhiganesh PG
Replied On May 1, 2019 07:28 AM UTC

Thank you so much @ Uthandaraja Selva Sundara Kani.

Thanks for your timely and great support. Now i can use the code which you were guided and got it work.
1. I can export PDF pages as Image.
2. Then i can crop image as wish.
3. And then i can able to print via printDocument class.

* the only thing is, exported image quality(pixel) is very low resolution. Some times it causes the problem while printing in Label printer.

Thanks again  @Uthandaraja Selva Sundara Kani sir.



Sathish Sivakumar [Syncfusion]
Replied On May 2, 2019 11:53 AM UTC

Hi Delhiganesh, 
 
Thank you for your update. 
 
We can improve the quality of image exported from PDF document by setting high DPI value in “PdfViewerControl.ExportAsImage(int pageIndex, float dpiX, float dpiY)” method. 
 
Using the below code snippet, we can improve the quality of exporting image, 
// Exports the specified page as Image with respect to dpi specified. 
Bitmap image = pdfViewerControl.ExportAsImage(m_printToPage,500,500); 
 
Please find the sample from below link, 
 
Note: We suggest you to use “Syncfusion.Windows.Forms.PdfViewer.PdfViewerControl” to pass the DPI value as a parameter in the ExportAsImage overload method. In previous sample, we use “Syncfusion.Windows.Forms.PdfViewer.PdfDocumentView” which does not contain the overload for DPI in the ExportAsImage method.  
 
Regards,
Sathish
 


Delhiganesh PG
Replied On May 2, 2019 12:01 PM UTC

Dear  @Sathish Sivakumar,
Thank you for the support. I used to pass dpi value as parameter, and got the high resolution of exported image as what i needed.
Thanks again.


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon