PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Aug 20, 2017 11:07 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Thu Jul 06, 2017 8:07 pm 
Offline

Joined: Thu Jul 06, 2017 7:56 pm
Posts: 5
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 374 times ]


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


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 06, 2017 9:34 pm 
Offline
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 295
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);

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 06, 2017 10:47 pm 
Offline

Joined: Thu Jul 06, 2017 7:56 pm
Posts: 5
() => 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


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 07, 2017 6:48 am 
Offline
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 295
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.

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 07, 2017 9:15 pm 
Offline

Joined: Thu Jul 06, 2017 7:56 pm
Posts: 5
() => 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


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 08, 2017 6:00 am 
Offline
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 295
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.

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 10, 2017 7:24 pm 
Offline

Joined: Thu Jul 06, 2017 7:56 pm
Posts: 5
() => 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 338 times ]


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 11, 2017 9:24 am 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2689
Location: Cologne, Germany
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);

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 12, 2017 6:15 pm 
Offline

Joined: Thu Jul 06, 2017 7:56 pm
Posts: 5
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).


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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:  
Powered by phpBB® Forum Software © phpBB Group