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:09 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: Mon Oct 10, 2016 8:30 pm 
Offline

Joined: Mon Oct 10, 2016 8:21 pm
Posts: 7
In our application, we have been using PDFSharp 1.2.1269.0 build and everything works perfectly fine. Since the new PDFSharp 1.5 supports iref streams, we thought of using PDFSharp 1.5 (GDI) beta in our windows application. Unfortunately, with the new code, none of our images are getting printed. We have the below code to draw the images on the PDF, but it doesn't work.

Code:
var pdfGraphics = XGraphics.FromPdfPage(page);
MemoryStream memory = new MemoryStream();
bmp1.Save(memory, jpgEncoder, myEncoderParameters);
Image img = Image.FromStream(memory);
pdfGraphics.DrawImage((Bitmap)img, 0, 0);


It produces a blank PDF file, which when opened, gives a out of memory error. The same code is working perfectly fine withe PDFSharp 1.2.1269.0 build. Can you let me know if something needs to be changed in our code to make it work?

Let me know if you require additional details.

Regards,
Aravind


Top
 Profile  
Reply with quote  
PostPosted: Tue Oct 11, 2016 12:36 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!

Maybe calling "XImage.FromStream(memory);" instead of "Image.FromStream(memory);" will already resolve the issue.

Code:
var img = XImage.FromStream(memory);
pdfGraphics.DrawImage(img, 0, 0);


aravindan wrote:
Let me know if you require additional details.
If I get a complete solution that allows me to replicate the issue in the debugger, I will investigate this issue.

See also:
viewtopic.php?p=2094#p2094

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Oct 11, 2016 2:17 pm 
Offline

Joined: Mon Oct 10, 2016 8:21 pm
Posts: 7
Thank you very much Thomas. That resolved the issue. I am able to print it successfully. Will get back to you, if I encounter any other issues.


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 12, 2016 7:52 pm 
Offline

Joined: Mon Oct 10, 2016 8:21 pm
Posts: 7
Hi,

This time, the DrawString is printing the text upside down. This is the code we have (which has been working all along with the PDFSharp 1.2.1269.0 build. After upgrading to PDFSharp 1.5 GDI, it is printing upside down.

Code:
            var page = document.AddPage();
            var gfx = XGraphics.FromPdfPage(page, XPageDirection.Upwards);
            XFont font = new XFont("Courier New", 12);
            XRect newRect = new XRect(16.9422, 740.974, 243.3568, 15.639);
            XStringFormat newStringFormat = new XStringFormat();
            newStringFormat.Alignment = XStringAlignment.Near;
            newStringFormat.LineAlignment = XLineAlignment.Near;
           
            string fntName = font.Name;

            double fntSize = 10;
            font = new XFont(fntName, fntSize);

            gfx.DrawString("Test Text", font, XBrushes.Black, newRect, newStringFormat);
            XGraphicsState state = gfx.Save();
            gfx.Restore(state);
            gfx.Dispose();


Can you please let us know why it is not working correctly?


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 13, 2016 8:37 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
aravindan wrote:
Can you please let us know why it is not working correctly?
Most likely it is working correctly.

The default value is "XPageDirection.Downwards". Call XGraphics.FromPdfPage() with just one parameter and it will work with both versions of PDFsharp.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 13, 2016 2:20 pm 
Offline

Joined: Mon Oct 10, 2016 8:21 pm
Posts: 7
Thanks for your response Thomas. If I change the direction to Downwards, or simply leave it blank, the text is getting printed correctly, but it is getting printed from bottom to top. The first line appears at the bottom of the page and so on.

By the way, this worked perfectly fine with the earlier version of PDFSharp. Any suggestions?

Regards,
Aravind


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 13, 2016 2:30 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Most programs work with the old version and the new version without any problems.

Maybe there is something outside your code snippet that causes the problem.
(0, 0) always was the top-left corner IIRC. Something has changed "under the hood" but normally user code is not affected.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 13, 2016 4:07 pm 
Offline

Joined: Mon Oct 10, 2016 8:21 pm
Posts: 7
I am not too sure about that Thomas. When the XPageDirection is Upwards, it is printing from top to bottom, but each and every letter is upside down. But when I change the XPageDirection to Downwards, or remove it altogether, it is printing from bottom to top and each letter is proper. I don't quite get the fact that it could be because of some other factor.


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 14, 2016 9:18 am 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 339
Maybe we need a VS solution that allows us to replicate the issue.


We created the Issue Submission Template for that purpose.
Starting from the Hello World sample you can add code from your project until the problem occurs.

Maybe you can solve the problem yourself when you know which step causes the problem.
Or send us the complete solution and we can investigate the issue.

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


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 27, 2016 9:35 pm 
Offline

Joined: Mon Oct 10, 2016 8:21 pm
Posts: 7
Thanks for your response. We were able to figure out the problem, as it was our code which was wrong. Now, we are trying to draw images into PDF, and here is the code we have. Can you please let me know what is wrong with this? After this, if we open the PDF, we are getting the error "There was an error while reading a stream.".

Code:
            Bitmap i =(Bitmap) Bitmap.FromFile(@"D:\Development\0.jpg", true);
            PdfDocument document = new PdfDocument();
            PdfPage newPage = document.AddPage();
            XRect imgRect = new XRect(0, 0.947815, 611.441, 791.052185);
            newPage.Orientation = PageOrientation.Portrait;
            XGraphics gfx;
            XColor back;
            XSolidBrush brush;
            gfx = XGraphics.FromPdfPage(newPage, XPageDirection.Downwards);
            back = XColors.White;
            back.A = 0.4;
            brush = new XSolidBrush(back);
            XImage img = XImage.FromGdiPlusImage(i);
            XFont font = new XFont("Arial", 12);
            gfx.DrawImage(img, imgRect.X, imgRect.Y, imgRect.Width, imgRect.Height);


Top
 Profile  
Reply with quote  
PostPosted: Sat Oct 29, 2016 1:21 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Hi!

Same problem as your first post.
Avoid "XImage.FromGdiPlusImage" and use "XImage.FromFile" instead of "Image.FromFile".

"XImage.FromGdiPlusImage" worked fine with PDFsharp 1.32 and Windows XP, but somewhere after XP MS broke something.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 31, 2016 3:02 pm 
Offline

Joined: Mon Oct 10, 2016 8:21 pm
Posts: 7
Thank you very much Thomas. I changed it to FromStream instead of FromGDIPlusImage and it works fine.


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 01, 2017 4:30 pm 
Offline

Joined: Wed Mar 01, 2017 4:14 pm
Posts: 4
Hi,

I am having the same issue as OP but in vb.net. Can you help?
Similar to OP, my code was working before I started using PdfSharp version 1.5 beta.
I am trying to reference a png logo image that is a project resource.

Code:
Dim logoimage As XImage = XImage.FromGdiPlusImage(My.Resources.logo)
gfx.DrawImage(logoimage, 60, 37, 40, 19)


I saw the comments about using image.fromstream(memory) instead but was unable to get the memory stream working.
Any help would be much appreciated.

Thanks,
Heath


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

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 167 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