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?