PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 7:54 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Sep 23, 2015 9:40 am 
Offline

Joined: Wed Sep 23, 2015 9:08 am
Posts: 3
I want to draw a System.Windows.Forms.DataVisualization.Charting.Chart on a PDFPage.
The method Chart.Printing.PrintPaint(Graphics graphics, Rectangle position) can be used to draw the chart on a Graphics.

How can I do to draw it on the XGraphics?

I tried to draw the chart on a Bitmap using the code below:
Code:
PdfDocument document = new PdfDocument();

PdfPage page = document.AddPage();
page.Orientation = PdfSharp.PageOrientation.Landscape;
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawRectangle(XBrushes.Red, rectanglePage);
Rectangle rectangle = new Rectangle(0, 0, (int)page.Width.Point, (int)page.Height.Point);

Bitmap bmp = new Bitmap(rectangle.Width, rectangle.Height);
Graphics g = Graphics.FromImage(bmp);
this.chart.Printing.PrintPaint(g, rectangle);

XImage img = XImage.FromGdiPlusImage(bmp);
gfx.DrawImage(img, 0, 0);


However, this does not work. On the result file, the chart is significantly smaller than the PDF page, as you can see on the attached image (The Page has been filled in red to differentiate the actual page to the chart).
In the code, page.Width.Point==842, and so is the chart on the image. However, the page actual width is 1123px.

I suspect a problem of PDI, but the answer from your team on this question http://stackoverflow.com/questions/1102 ... n-pdfsharp says that I cannot change the DPI.

This other question viewtopic.php?f=2&t=3141&hilit=graphics shows how to how to use XGraphics to draw on a Graphics. But I need to do the opposite.


Attachments:
File comment: Result of my code. The Page has been filled in red to differentiate the actual page to the chart.
ChartOnPdf.png
ChartOnPdf.png [ 74.83 KiB | Viewed 161249 times ]
Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 23, 2015 12:41 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!
smartstorm wrote:
However, the page actual width is 1123px.
PDF pages do not have pixels.

Instead of
Code:
gfx.DrawImage(img, 0, 0);
use
Code:
gfx.DrawImage(img, 0, 0, page.Width, page.Height);
and the image should fill the whole page.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 23, 2015 1:22 pm 
Offline

Joined: Wed Sep 23, 2015 9:08 am
Posts: 3
Hi Thomas,

Yes it does work! Thanks for the quick reply.

However the quality isn't great because the Bitmap is resized, even though the ratio is the same.

I initially thought that the XGraphicsUnit.Point was equivalent to a pixel, but of course not. It is obviously a typographical point.
Now that I understood that, I found another solution:

Code:
double ppi = page.Width.Point / page.Width.Inch;
double coeff = Graphics.FromHwnd(IntPtr.Zero).DpiX / ppi;
Rectangle rectangle = new Rectangle(0, 0, (int)(page.Width.Point * coeff ), (int)(page.Height.Point * coeff ));
//Rectangle rectangle = new Rectangle(0, 0, (int)page.Width.Point, (int)page.Height.Point);


In this solution we calculate the coefficient between the PPI of the PDF and the DPI of the display, and we use this coefficient to calculate the approximate size in pixel of the page.
The Bitmap doesn't need to be resized, which greatly improves the quality of the chart.
This solution assume the DpiX and DpiY are the same. I cannot think of any case where that would not be true.
This (should) work whatever the DPI of your display.

Thanks again.


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 23, 2015 1:35 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
smartstorm wrote:
The Bitmap doesn't need to be resized, which greatly improves the quality of the chart.
There are no pixels in PDF. The bitmap will only be resized by the application that displays the PDF file - depending on the zoom factor selected by the user.
Your change does not improve the quality of the chart. It will look better at some zoom factors and worse at others.

Try
Code:
gfx.DrawImage(img, 0, 0, page.Width, page.Height);
versus
Code:
gfx.DrawImage(img, 0, 0, 100, 75);
and compare the file sizes - there shouldn't be a significant difference (give or take a byte or two) - and when you zoom in with Adobe Reader you should see the same bitmap with both files.

You can try
Code:
img.Interpolate = false;
(true is the default IIRC). This works with Adobe Reader, but has no effect on some other viewers.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 28, 2015 9:47 am 
Offline

Joined: Wed Sep 23, 2015 9:08 am
Posts: 3
Yes the PDF does not have pixel, but the Bitmap has.
The problem would be the same if I was trying to print or even when displaying an image on a monitor, and is not caused by PDFSharp.

If I try to display a 100px*50px image on a 4k monitor, fullscreen, this would look absolutely poor, as you rightfully stated when talking about the zoom.

The attached image are screenshots of the generated PDF.
The middle one uses my algorithm to calculate the optimal dimension of the Bitmap.
For the top one, the Bitmap is twice smaller than the optimal.
And the bottom one is twice bigger than the optimal.

I tested the algorithm with SumatraPDF, AdboeReader and FoxitReader, and on various DPI settings for the monitor. I even used the same algorithm to print document (through the PDF or directly) and the result are the same than on the attached image.

The only thing I don't understand is why I need the use the current DPI of the PC.

Again, this has nothing to do with PDFSharp. Only the changes from a Pixel based dimension (the Bitmap) to a Inch based one (the PDF).


Attachments:
File comment: Middle image uses the optimal size for the Bitmap
Top one is twice smaller
Bottom one is twice bigger

BitmapSizeChangeds.png
BitmapSizeChangeds.png [ 67.29 KiB | Viewed 161214 times ]
Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 28, 2015 11:01 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Maybe there was a misunderstanding. PDFsharp takes the bitmap as it comes. No resizing, no loss of quality.
The rectangle you specify in the call of gfx.DrawImage() does not change the bitmap in any way.

Bitmap size does matter at the stage where you draw the chart to a bitmap. But this has nothing to do with PDFsharp.
More pixels are theoretically better, but may require proportional increase of line widths, font sizes, and other parameters to get a similar look.

If line widths and font sizes are specified in points, then the DPI of the bitmap must be adjusted before chart creation .

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 37 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group