( I am using PDF Sharp )
I am have a few Bitmaps and for each Bitmap I create a new PDF Page. The problem is that my pdf page does not have enough height to contain the entire Bitmap, so I'm losing a portion of the Bitmap.
What is the best way to resize the pdf page so that the entire Bitmap will fit to that one PDF Page?
public static PdfDocument GetPDF(List<Bitmap> pages, bool makeFit = false)
{
using (var doc = new PdfDocument())
{
for (byte i = 0; i < pages.Count(); i++)
{
PdfPage oPage = new PdfPage();
doc.Pages.Add(oPage);
using (var xgr = XGraphics.FromPdfPage(oPage))
{
using (var bm = pages[i])
{
using (var img = XImage.FromGdiPlusImage(bm))
{
xgr.DrawImage(img, 0, 0);
}
}
}
}
return doc;
}
}
I did try to set the size at this location
xgr.DrawImage(img, 0, 0);
but this made the Bitmap larger and not the PDF Page.
I may need someone to simply point out the concept behind saving Bitmaps to PDF Pages, and then give a small simple example of how its done.
I've already looked over some of the help files at PDF Sharp and they're not that easy to just grasp.
I'm hoping someone can just tell me how to approach this in the simplest form. All I need to do is to save a Bitmap to a PDF Page and make sure that the Bitmap that has been saved to the PDF Page is completely viewable.
Thank you!!
------------------------------------------EDIT------------------------------------
I've added an updated version, the snapshot on the left is of a raw .png image being rendered in a browser and the one on the right is the browser rendering the PDF.
Here is the updated code:
public static PdfDocument GetPDF(List<Bitmap> pages, bool makeFit = false)
{
using (var doc = new PdfDocument())
{
for (byte i = 0; i < pages.Count(); i++)
{
PdfPage oPage = new PdfPage();
doc.Pages.Add(oPage);
if (makeFit)
{
XSize size = new XSize(pages[i].Width, pages[i].Height);
oPage.Height = size.Height;
oPage.Width = size.Width;
}
using (var xgr = XGraphics.FromPdfPage(oPage))
{
using (var bm = pages[i])
{
using (var img = XImage.FromGdiPlusImage(bm))
{
xgr.DrawImage(img, 0, 0);
}
}
}
}
return doc;
}
}
----Update image------------
------------------------------Last Edit that works----------------------------
What I've done is resize the PDF Page to the Bitmap and then drawed the Bitmap onto the page and resize the Bitmap as to the PDF Page size -5px.
public static PdfDocument GetPDF(List<Bitmap> pages, bool makeFit = false)
{
XSize size = new XSize(0, 0);
using (var doc = new PdfDocument())
{
for (byte i = 0; i < pages.Count(); i++)
{
PdfPage oPage = new PdfPage();
doc.Pages.Add(oPage);
if (makeFit)
{
size = new XSize(pages[i].Width, pages[i].Height);
oPage.Height = size.Height;
oPage.Width = size.Width;
}
using (var xgr = XGraphics.FromPdfPage(oPage))
{
using (var bm = pages[i])
{
using (var img = XImage.FromGdiPlusImage(bm))
{
if (makeFit)
{
xgr.DrawImage(img, 0, 0, size.Width - 5, size.Height - 5);
}
else
{
xgr.DrawImage(img, 0, 0);
}
}
}
}
}
return doc;
}
}
--Snap shots shows they produce just about the same view---------