PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

XGraphics not drawing entire image
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3621
Page 1 of 1

Author:  phoenixfire [ Thu Jul 06, 2017 8:07 pm ]
Post subject:  XGraphics not drawing entire image

Long story short, I'm using XGraphics to stitch an image onto a pdf but XGraphics is only drawing 2/3 of the image starting from the top right corner. I'm using pdfsharp 1.5b3.

Here's my code:
Code:
            string backgroundImage = "";//<---background image
            string frontImage = ""; //<---png image to put on top of pdf file
            string outputFile = ""; //<---Resulting pdf file

            PdfDocument document = new PdfDocument();
            PdfPage page = document.AddPage();

            XGraphics gfx = XGraphics.FromPdfPage(page);

            //Commented out code is for adding the front image. Unneeded to see the issue at this point
            XImage image = XImage.FromFile(backgroundImage);
            //XImage imageB = XImage.FromFile(frontImage);

            page.Width = image.PixelWidth;
            page.Height = image.PixelHeight;

            gfx.DrawImage(image, 0, 0);
            //gfx.DrawImage(imageB, 0, 0, page.Width, page.Height);

            document.Save(outputFile);


I have already tested this code with other files and this single page is the only one that is having this issue.
Any help or suggestions would be much appreciated

For certain reasons, please PM for the pdf file
Here's what the the original looks like
Attachment:
Original.png
Original.png [ 157.63 KiB | Viewed 16545 times ]


And here's the result from the code
Attachment:
Result.png
Result.png [ 88.96 KiB | Viewed 16545 times ]

Author:  () => true [ Thu Jul 06, 2017 9:34 pm ]
Post subject:  Re: XGraphics not drawing entire image

Hi!

PDF files have no pixels.
The following code may not have the effect you expect:
Code:
page.Width = image.PixelWidth;
page.Height = image.PixelHeight;


You have to set page width and height before obtaining the XGraphics object, otherwise it won't work correctly.

Using
Code:
gfx.DrawImage(image, 0, 0, page.Width, page.Height);
looks more promising than using
Code:
gfx.DrawImage(imageB, 0, 0);

Author:  phoenixfire [ Thu Jul 06, 2017 10:47 pm ]
Post subject:  Re: XGraphics not drawing entire image

() => true wrote:
Hi!

PDF files have no pixels.
The following code may not have the effect you expect:
Code:
page.Width = image.PixelWidth;
page.Height = image.PixelHeight;


You have to set page width and height before obtaining the XGraphics object, otherwise it won't work correctly.

Using
Code:
gfx.DrawImage(image, 0, 0, page.Width, page.Height);
looks more promising than using
Code:
gfx.DrawImage(imageB, 0, 0);


Thanks for answering but I already tried using
Code:
gfx.DrawImage(image, 0, 0, page.Width, page.Height);

It gives the same result as
Code:
gfx.DrawImage(image, 0, 0);
for this particular pdf where only part of the pdf is drawn.
And as stated, I have tested it with other pdf files as well and they all worked except for this one

Author:  () => true [ Fri Jul 07, 2017 6:48 am ]
Post subject:  Re: XGraphics not drawing entire image

phoenixfire wrote:
And as stated, I have tested it with other pdf files as well and they all worked except for this one
I read what you stated.
You have to try again because you have to set the page size before obtaining the XGraphics object.

I cannot replicate the issue and therefore I cannot investigate whether the problem is with your code or with PDFsharp.
When people show code snippets here, the problem is often outside the code snippet. That's why we made the Issue Submission Template.
If you create a VS solution that allows to replicate the issue, then I can send you the e-mail address for submissions to the team if you do not want to make it public.

Author:  phoenixfire [ Fri Jul 07, 2017 9:15 pm ]
Post subject:  Re: XGraphics not drawing entire image

() => true wrote:
phoenixfire wrote:
And as stated, I have tested it with other pdf files as well and they all worked except for this one
I read what you stated.
You have to try again because you have to set the page size before obtaining the XGraphics object.

I cannot replicate the issue and therefore I cannot investigate whether the problem is with your code or with PDFsharp.
When people show code snippets here, the problem is often outside the code snippet. That's why we made the Issue Submission Template.
If you create a VS solution that allows to replicate the issue, then I can send you the e-mail address for submissions to the team if you do not want to make it public.


Ya can you send me the email address please? I'll send in a sample with the pdf

Author:  () => true [ Sat Jul 08, 2017 6:00 am ]
Post subject:  Re: XGraphics not drawing entire image

The file has /ROTATE 90 set, so it is in landscape format. Maybe that is causing the problem.

If you are using the PDFsharp source code, you can try the patch from this post:
viewtopic.php?p=9591#p9591

The next version of PDFsharp with that change should be available in a fortnight or two.
Waiting for the next NuGet packages is the second option.

Author:  phoenixfire [ Mon Jul 10, 2017 7:24 pm ]
Post subject:  Re: XGraphics not drawing entire image

() => true wrote:
The file has /ROTATE 90 set, so it is in landscape format. Maybe that is causing the problem.

If you are using the PDFsharp source code, you can try the patch from this post:
viewtopic.php?p=9591#p9591

The next version of PDFsharp with that change should be available in a fortnight or two.
Waiting for the next NuGet packages is the second option.


Just tried that fix. Well it did do something. Now it stretches the image so the bottom is cut off and the image is uh....pushed to the right a little
Attachment:
Result2.png
Result2.png [ 133.29 KiB | Viewed 16509 times ]

Author:  Thomas Hoevel [ Tue Jul 11, 2017 9:24 am ]
Post subject:  Re: XGraphics not drawing entire image

Hi!

There is a bug, caused by the "/Rotate 90" setting of the document.

I found a workaround that works with the current internal build of PDFsharp. I didn't check 1.50 beta 3b (with the patch mentioned earlier) yet, but I assume it will work as well.

The trick is with these two lines:
Code:
page.Rotate = image.Page.Rotate;
image.Page.Rotate = 0;

Disclaimer: this trick may be harmful as soon as we fix the "/Rotate 90" issue in PDFsharp (but I have no idea when we will tackle this).


Here's the complete code (another change is "XPdfForm.FromFile" to get the image):
Code:
string backgroundImage = "testBackground.pdf";//<---background image
string outputFile = "HelloWorld000.pdf"; //<---Resulting pdf file

PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();

var image = XPdfForm.FromFile(backgroundImage);

page.Rotate = image.Page.Rotate;
image.Page.Rotate = 0;
page.Width = image.PixelWidth;
page.Height = image.PixelHeight;

XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawImage(image, 0, 0, page.Width, page.Height);

document.Save(outputFile);
Process.Start(outputFile);

Author:  phoenixfire [ Wed Jul 12, 2017 6:15 pm ]
Post subject:  Re: XGraphics not drawing entire image

Thomas Hoevel wrote:
Hi!

There is a bug, caused by the "/Rotate 90" setting of the document.

I found a workaround that works with the current internal build of PDFsharp. I didn't check 1.50 beta 3b (with the patch mentioned earlier) yet, but I assume it will work as well.

The trick is with these two lines:
Code:
page.Rotate = image.Page.Rotate;
image.Page.Rotate = 0;

Disclaimer: this trick may be harmful as soon as we fix the "/Rotate 90" issue in PDFsharp (but I have no idea when we will tackle this).


Here's the complete code (another change is "XPdfForm.FromFile" to get the image):
Code:
string backgroundImage = "testBackground.pdf";//<---background image
string outputFile = "HelloWorld000.pdf"; //<---Resulting pdf file

PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();

var image = XPdfForm.FromFile(backgroundImage);

page.Rotate = image.Page.Rotate;
image.Page.Rotate = 0;
page.Width = image.PixelWidth;
page.Height = image.PixelHeight;

XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawImage(image, 0, 0, page.Width, page.Height);

document.Save(outputFile);
Process.Start(outputFile);



Oooo Thanks very much Thomas!
Solution worked (Although I will need to do a bit of tinkering to get the result I need now) but this definitely solves the biggest problem.
And thank you "() => true" for helping me out with this :) (It feels weird typing that name).

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/