How to put a long web page screenshot in a PDF?

Hello.
I'm trying to save an entire web page screenshot into a PDF but I'm struggling against some difficulties.
The image I get from the WebView is a JPEG sized 804x2809 and I would like it to appear in a PDF, preferibly in a single page that should be sized (I think) just like the image is.

When I create a new PDF document and add a Page, I get these values:

  • Doc size: 804x2809
  • Page Client Size:515x762
The result is the following:


As you can see, the whole web page fitted in the PDF single page but it's very stretched and distorted.
I wrote the following code to achieve this result:

using (IRandomAccessStream bmpStream = await mScreenShot.OpenAsync(FileAccessMode.ReadWrite,StorageOpenOptions.AllowReadersAndWriters))
using (PdfDocument document = new PdfDocument(PdfConformanceLevel.Pdf_A2B))
            {
                
                PdfBitmap image = new PdfBitmap(bmpStream.AsStreamForRead());                
                PdfPage page = document.Pages.Add();
                PdfGraphics graphics = page.Graphics;
                Debug.WriteLine("Image size: " + image.Width.ToString() + "x" + image.Height.ToString());
                document.PageSettings.Size = new SizeF(image.Width, image.Height);                
                Debug.WriteLine("doc size: "+image.Width.ToString() + "x" + image.Height.ToString());
                SizeF size = page.GetClientSize();
                Debug.WriteLine("Page Client Size:" + size.Width.ToString() + "x" + size.Height.ToString());
                RectangleF imageBounds = new RectangleF(0, 0, size.Width,size.Height);
                graphics.DrawImage(image, imageBounds);
               FileSavePicker savePicker = new FileSavePicker();
               savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
               savePicker.FileTypeChoices.Add("Adobe PDF", new List<string>() { ".pdf" });
               savePicker.SuggestedFileName = "wb_pageshot.pdf";
               StorageFile oPdf = await savePicker.PickSaveFileAsync();
                    //StorageFile oPdf = await folder.CreateFileAsync("wb_links.pdf", CreationCollisionOption.ReplaceExisting);
                    using (Stream oStream = await oPdf.OpenStreamForWriteAsync())
                    {
                        document.Save(oStream);
                    }
}

Where am I wrong? How do I save the picture in a PDF with just one page without distorting it?

1 Reply 1 reply marked as answer

GK Gowthamraj Kumar Syncfusion Team February 24, 2021 01:12 PM UTC

Hi ZipGenius Team, 
 
Thank you for contacting Syncfusion support. 
 
We have analyzed your requirement with provided code snippet in our end. We can overcome this issue by creating the page size as image size using PageSettings property of PdfDocument. Please try the below code snippet before adding the page in the PDF document. 
 
//Create a new PDF document. 
PdfDocument document = new PdfDocument(); 
 
// Set the page size. 
document.PageSettings.Size = new SizeF(image.Width, image.Height); 
 
//Add a page to the document. 
PdfPage page = document.Pages.Add(); 
 
Please refer the below links for more information, 
 
If still you are facing an same issue, kindly provide more details such as complete code snippet with reproducing sample, output document, product version to check the issue in our end. So, that it will be helpful for us to analyze and assist you further on this. 
 
Regards, 
Gowthamraj K 


Marked as answer
Loader.
Up arrow icon