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!