Rendering PDF to custom paper size

We have a PDF document as a base64 string. If I take that string a use any of the online base64 decoders I get a PDF document that by default prints to a custom size. The special paper we are having to use is 210mm x 172mm.

However if I take that base64 string, convert it to a byearray and loading it into the PdfDocumentView via a PdfLoadedDocument I get what seems to be a document centered in a A4 size. If i try and print on custom paper the document is way off.

How do I specify that I want to print the PDF data at a certain size?

I have tried setting PrinterSettigs.PageSize to Actual but that does not help.

Thanks



7 Replies

KK Karthik Krishnaraj Syncfusion Team September 5, 2018 11:41 AM UTC

Hi Pat, 
 
Greetings from Syncfusion, 
 
However if I take that base64 string, convert it to a byearray and loading it into the PdfDocumentView via a PdfLoadedDocument I get what seems to be a document centered in a A4 size. If i try and print on custom paper the document is way off. 
We suggest passing the printer setting page size to be fit. It will print the document after fitting the content. 
 
documentView.PrinterSettings.PageSize = PdfViewerPrintSize.Fit;  
 
How do I specify that I want to print the PDF data at a certain size? 
It is possible to print the PDF data at custom size. Please find the sample to achieve the same. 
 
Sample Link: 
 
 
 
 
 
Please check with the provided information and let us know if it helps. 
 
Regards, 
Karthik. 



PA Pat replied to Karthik Krishnaraj September 5, 2018 12:22 PM UTC

Hi Pat, 
 
Greetings from Syncfusion, 
 
However if I take that base64 string, convert it to a byearray and loading it into the PdfDocumentView via a PdfLoadedDocument I get what seems to be a document centered in a A4 size. If i try and print on custom paper the document is way off. 
We suggest passing the printer setting page size to be fit. It will print the document after fitting the content. 
 
documentView.PrinterSettings.PageSize = PdfViewerPrintSize.Fit;  
 
How do I specify that I want to print the PDF data at a certain size? 
It is possible to print the PDF data at custom size. Please find the sample to achieve the same. 
 
Sample Link: 
 
 
 
 
 
Please check with the provided information and let us know if it helps. 
 
Regards, 
Karthik. 


>>documentView.PrinterSettings.PageSize = PdfViewerPrintSize.Fit;  <<
Is this only useful when I am able to apply the second half of your suggestion? Simply setting "size to fit" unsurprisingly does not work and in fact just makes it worse as the document still thinks it is A4

As you can see from my question I am using PdfDocumentView and PdfLoadedDocument. Neither of these offer the PrintPage event that your sample uses

Your sample also uses a SizeF structure of 827,667. I'm assuming that that is in pixels rather than mm. How do I get from 210mm x 172mm to a SizeF?


SR Srilatha R Syncfusion Team September 5, 2018 02:48 PM UTC

Hi Pat
We are somewhat unclear on some aspects of this and since this appears to be an urgent issue for your team, we would like to schedule a web meeting today with our technical team. Are you available for this? What time would work best for you?

Regards,
Srilatha R


PA Pat September 6, 2018 06:50 AM UTC

Thanks for your help, I now better understand your sample and have integrated it into our code. I'll need to try it with some custom paper later.

Have you got a link that explains the mm to inches to px conversion and how to get the printer DPI? 


KK Karthik Krishnaraj Syncfusion Team September 6, 2018 01:58 PM UTC

 
Hi Pat, 
 
Please find the sample using DPI value to print PDF using custom paper size from below, 
 
 
Note: The requested links are shared in the sample. 
 
Regards, 
Karthik. 



PA Pat September 6, 2018 03:35 PM UTC

I haven't tried it yet having just read it this line jumped out at me.

>>//100 is used as the DPI value of the printer<<

During our conversation this morning you guys said i needed to use the DPi that was valid for the chosen printer but this looks like you are hardcoding it. Have i misunderstood?

The PdfUnitConvertor that is used in the PrintPage event is not initialised with the same arguments as the one used when first creating the PrintDocument. Is this correct?


KK Karthik Krishnaraj Syncfusion Team September 7, 2018 01:25 PM UTC

Hi Pat, 
 
>>//100 is used as the DPI value of the printer<< 
 
During our conversation this morning you guys said i needed to use the DPi that was valid for the chosen printer but this looks like you are hardcoding it. Have i misunderstood? 
 
We already mentioned that there is no need to use this code, when custom paper size in available in the target printer. 
 
The DPI of different machines will be different which is based on screen resolution. And this setting will only affect the printed output in virtual printers of the machine. Hence, we included the 100 as DPI value of the printer which is required only for virtual printers. 
 
As you are using the physical printer, you can execute the project without using the DPI value as it would automatically detect the DPI of the target printer. I have attached the output file of virtual printers with and without using the DPI value for your reference below. 
 
  
The PdfUnitConvertor that is used in the PrintPage event is not initialised with the same arguments as the one used when first creating the PrintDocument. Is this correct? 
Yes. Page content DPI value is different for various monitor resolutions. In this state, parameter value of PdfUnitConvertor will be updated with default DPI value. 
 
 
 
 
Sample Link: 
 
Please check with the provided information and let us know if it helps. 
 
Regards, 
Karthik. 


Loader.
Up arrow icon