PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Feb 21, 2020 4:44 pm 
Offline

Joined: Fri Feb 21, 2020 3:15 pm
Posts: 3
I am having a problem creating a pdf from a jpg. The image displays correctly in Firefox but not in other browsers (Chrome, Edge, IE), Adobe Acrobat, or PDF Architect. I cannot control the format of the image, or the browser as this is a production website.

I am using Visual Basic in Visual Studio 2017 with .Net Framework version 4.8 on Windows 10 Pro and PdfSharp 1.50.5147.

Here is a copy of the code I am using to create the file:
Code:
            Dim pdfName As String = pdfFullName.Substring(pdfFullName.LastIndexOf("\") + 1)
            Dim Doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
            Dim Page As New PdfSharp.Pdf.PdfPage
            Dim Img As PdfSharp.Drawing.XImage
            Dim XGFX As XGraphics
            Dim fontSize As Integer = 10
            Dim font As XFont = New XFont("Helvetica", fontSize)
            Try
                If Path.Trim <> String.Empty Then
                    'create pdf
                    Page = New PdfSharp.Pdf.PdfPage
                    Dim WebRequest As WebRequest = WebRequest.Create(Path)
                    Dim WebResponse As WebResponse = WebRequest.GetResponse
                    Img = XImage.FromStream(WebResponse.GetResponseStream)
                    Page.Width = Img.PointWidth
                    Page.Height = Img.PointHeight
                    Doc.Pages.Add(Page)
                    XGFX = XGraphics.FromPdfPage(Doc.Pages(0)) 'XGraphics.FromPdfPage(Doc.Pages(PageCount))
                    XGFX.DrawImage(Img, 0, 0, Page.Width, Page.Height)

                    'create watermark
                    Dim watermarkSize As XSize = XGFX.MeasureString(watermark, font)
                    Do While Page.Height.Value < watermarkSize.Width And fontSize > 2
                        fontSize = fontSize - 1
                        font = New XFont("Helvetica", fontSize)
                        watermarkSize = XGFX.MeasureString(watermark, font)
                    Loop
                    Dim watermarkLocation As Double = (Page.Height.Value - watermarkSize.Width) / 2
                    XGFX.TranslateTransform(0, Page.Height.Value)
                    XGFX.RotateTransform(-90)
                    XGFX.TranslateTransform(0, watermarkSize.Height)

                    Dim brush As XBrush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))
                    XGFX.DrawString(watermark, font, brush, watermarkLocation, 2)

                    XGFX.Dispose()
                    Img.Dispose()
                    Debug.WriteLine(Path)
                End If
            Catch ex As Exception
                Debug.WriteLine("Error generating '" & Path & "'")
                Exit For
            End Try
            Doc.Save(pdfPath & "\" & pdfName)
            Doc.Close()
            Doc.Dispose()


The following version of the code will create the pdf correctly, but it takes way too long to use on a production website.
Code:
            Dim pdfName As String = pdfFullName.Substring(pdfFullName.LastIndexOf("\") + 1)
            Dim Doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
            Dim Page As New PdfSharp.Pdf.PdfPage
            Dim Img As System.Drawing.Image = Nothing
            Dim xImg As PdfSharp.Drawing.XImage
            Dim XGFX As XGraphics
            Dim fontSize As Integer = 10
            Dim font As XFont = New XFont("Helvetica", fontSize)
            Try
                If Path.Trim <> String.Empty Then
                    'create pdf
                    Page = New PdfSharp.Pdf.PdfPage
                    Dim WebRequest As WebRequest = WebRequest.Create(Path)
                    Dim WebResponse As WebResponse = WebRequest.GetResponse
                    img = System.Drawing.Image.FromStream(WebResponse.GetResponseStream)
                    Dim strm As New System.IO.MemoryStream
                    Img.Save(strm, System.Drawing.Imaging.ImageFormat.Bmp)
                    xImg = XImage.FromStream(strm)
                    Page.Width = xImg.PointWidth
                    Page.Height = xImg.PointHeight
                    Doc.Pages.Add(Page)
                    XGFX = XGraphics.FromPdfPage(Doc.Pages(0)) 'XGraphics.FromPdfPage(Doc.Pages(PageCount))
                    XGFX.DrawImage(xImg, 0, 0, Page.Width, Page.Height)

                    'create watermark
                    Dim watermarkSize As XSize = XGFX.MeasureString(watermark, font)
                    Do While Page.Height.Value < watermarkSize.Width And fontSize > 2
                        fontSize = fontSize - 1
                        font = New XFont("Helvetica", fontSize)
                        watermarkSize = XGFX.MeasureString(watermark, font)
                    Loop
                    Dim watermarkLocation As Double = (Page.Height.Value - watermarkSize.Width) / 2
                    XGFX.TranslateTransform(0, Page.Height.Value)
                    XGFX.RotateTransform(-90)
                    XGFX.TranslateTransform(0, watermarkSize.Height)

                    Dim brush As XBrush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))
                    XGFX.DrawString(watermark, font, brush, watermarkLocation, 2)

                    XGFX.Dispose()
                    Img.Dispose()
                    Debug.WriteLine(Path)
                End If
            Catch ex As Exception
                Debug.WriteLine("Error generating '" & Path & "'")
                Exit For
            End Try
            Doc.Save(pdfPath & "\" & pdfName)
            Doc.Close()
            Doc.Dispose()


The pdf will also display correctly if the path used by xImage.FromStream is in UNC format like \\server1\f$\temp\image.jpg, but we must use https like https://mysite.com/images/image.jpg to retrieve the images through our firewall. It is also interesting that pdf files created from tiff files display correctly whether I use https or unc to get the image.

I tried to send a sample jpg and resulting pdf, but the files were too large even when zipped.

Is there anything I can do differently to make these images display correctly? Thanks in advance for any suggestions!


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 21, 2020 4:47 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
NikS wrote:
Is there anything I can do differently to make these images display correctly?
Download the image in your code and then pass a MemoryStream or a UNC path (temporary file) to PDFsharp. That should make a difference.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 21, 2020 5:21 pm 
Offline

Joined: Fri Feb 21, 2020 3:15 pm
Posts: 3
Do you mean the way I did in my second code sample? That works, but it takes too long to use. Here's that code again. Is there anything I can do to make it faster?
Code:
            Dim pdfName As String = pdfFullName.Substring(pdfFullName.LastIndexOf("\") + 1)
            Dim Doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
            Dim Page As New PdfSharp.Pdf.PdfPage
            Dim Img As System.Drawing.Image = Nothing
            Dim xImg As PdfSharp.Drawing.XImage
            Dim XGFX As XGraphics
            Dim fontSize As Integer = 10
            Dim font As XFont = New XFont("Helvetica", fontSize)
            Try
                If Path.Trim <> String.Empty Then
                    'create pdf
                    Page = New PdfSharp.Pdf.PdfPage
                    Dim WebRequest As WebRequest = WebRequest.Create(Path)
                    Dim WebResponse As WebResponse = WebRequest.GetResponse
                    img = System.Drawing.Image.FromStream(WebResponse.GetResponseStream)

                    Dim strm As New System.IO.MemoryStream
                    Img.Save(strm, System.Drawing.Imaging.ImageFormat.Bmp)
                    xImg = XImage.FromStream(strm)
                    Page.Width = xImg.PointWidth
                    Page.Height = xImg.PointHeight
                    Doc.Pages.Add(Page)
                    XGFX = XGraphics.FromPdfPage(Doc.Pages(0)) 'XGraphics.FromPdfPage(Doc.Pages(PageCount))
                    XGFX.DrawImage(xImg, 0, 0, Page.Width, Page.Height)

                    'create watermark
                    Dim watermarkSize As XSize = XGFX.MeasureString(watermark, font)
                    Do While Page.Height.Value < watermarkSize.Width And fontSize > 2
                        fontSize = fontSize - 1
                        font = New XFont("Helvetica", fontSize)
                        watermarkSize = XGFX.MeasureString(watermark, font)
                    Loop
                    Dim watermarkLocation As Double = (Page.Height.Value - watermarkSize.Width) / 2
                    XGFX.TranslateTransform(0, Page.Height.Value)
                    XGFX.RotateTransform(-90)
                    XGFX.TranslateTransform(0, watermarkSize.Height)

                    Dim brush As XBrush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))
                    XGFX.DrawString(watermark, font, brush, watermarkLocation, 2)

                    XGFX.Dispose()
                    Img.Dispose()
                    Debug.WriteLine(Path)
                End If
            Catch ex As Exception
                Debug.WriteLine("Error generating '" & Path & "'")
                Exit For
            End Try
            Doc.Save(pdfPath & "\" & pdfName)
            Doc.Close()
            Doc.Dispose()


Thank you so much for your fast response!


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 21, 2020 6:52 pm 
Offline

Joined: Fri Feb 21, 2020 3:15 pm
Posts: 3
I figured out how to make it faster. As you suggested, I used downloaded the file and used the UNC path to the temporary file. That was significantly faster than using the memory stream.

Here is the new version of my code:
Code:
            Dim pdfName As String = pdfFullName.Substring(pdfFullName.LastIndexOf("\") + 1)
            Dim Doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
            Dim Page As New PdfSharp.Pdf.PdfPage
            Dim Img As System.Drawing.Image = Nothing
            Dim xImg As PdfSharp.Drawing.XImage
            Dim XGFX As XGraphics
            Dim fontSize As Integer = 10
            Dim font As XFont = New XFont("Helvetica", fontSize)
            Try
                If Path.Trim <> String.Empty Then
                    'create pdf
                    Page = New PdfSharp.Pdf.PdfPage
                    Dim WebRequest As WebRequest = WebRequest.Create(Path)
                    Dim WebResponse As WebResponse = WebRequest.GetResponse
                    img = System.Drawing.Image.FromStream(WebResponse.GetResponseStream)
                    Dim strm As New System.IO.MemoryStream
                    Img.Save("c:\temp\tempfile.jpg")
                    xImg = XImage.FromFile("c:\temp\tempfile.jpg")
                    Page.Width = xImg.PointWidth
                    Page.Height = xImg.PointHeight
                    Doc.Pages.Add(Page)
                    XGFX = XGraphics.FromPdfPage(Doc.Pages(0)) 'XGraphics.FromPdfPage(Doc.Pages(PageCount))
                    XGFX.DrawImage(xImg, 0, 0, Page.Width, Page.Height)

                    'create watermark
                    Dim watermarkSize As XSize = XGFX.MeasureString(watermark, font)
                    Do While Page.Height.Value < watermarkSize.Width And fontSize > 2
                        fontSize = fontSize - 1
                        font = New XFont("Helvetica", fontSize)
                        watermarkSize = XGFX.MeasureString(watermark, font)
                    Loop
                    Dim watermarkLocation As Double = (Page.Height.Value - watermarkSize.Width) / 2
                    XGFX.TranslateTransform(0, Page.Height.Value)
                    XGFX.RotateTransform(-90)
                    XGFX.TranslateTransform(0, watermarkSize.Height)

                    Dim brush As XBrush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))
                    XGFX.DrawString(watermark, font, brush, watermarkLocation, 2)

                    XGFX.Dispose()
                    Img.Dispose()
                    Debug.WriteLine(Path)
                End If
            Catch ex As Exception
                Debug.WriteLine("Error generating '" & Path & "'")
                Exit For
            End Try
            Doc.Save(pdfPath & "\" & pdfName)
            Doc.Close()
            Doc.Dispose()


Again, thanks for your help!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 147 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