This post concerns the WPF/nuget version of PDFSharp, although that probably doesn't really matter here...
So I had a bunch of differently-sized images organized into folders, and wanted one PDF file for each folder, and one image on each page, with each page matching the existing image resolution and size of each image.
I figured this out myself by looking at the forum, and trying a bunch of random things until it worked, as the samples don't really explain how to do this. I looked in the forum, and saw many posts explaining that PDF is vector, and doesn't have a DPI. But for a newbie, that doesn't really explain what PDF
is, or how the sizing works without DPI/pixels/etc. So, for instance, when I wanted to make the page size equal to the size of the image I was using, I would try page.Width = image.Width; page.Height = image.Height, or page.Height = image.PointHeight; page.width = image.PointWidth.
Neither of these work, instead they produce screwy results where the image takes up only a small corner of the page. I really don't know why exactly, because I don't know what units are used for image.Height, image.PointHeight, or page.Height, and I don't see anywhere that explains that (If anyone knows, please point me to it, because I would love to understand better--I know/think that a point is 1/72 of an inch, but how is that different from a pixel or 72 DPI? and why doesn't page.Height = image.Height work?).
Anyway, I then saw a post that told someone to use XUnit.FromMillimeter() to convert from millimeters, and so I began looking at XUnit, and saw that it has XUnit.FromPoint(). So I tried page.Height = XUnit.FromPoint(image.PointHeight); page.Width = XUnit.FromPoint(image.PointWidth), and it worked perfectly. I still don't really know what a point or an XUnit is, or how they differ from pixels or DPI, but oh well...
The code I came up with is below, and it seems to do exactly what I wanted it to do (your XAML file should have a button to call CreatePdf(), and a TextBox named "textbox" to log your results in. Also, if your folders have anything other than image files in them, I imagine that would cause problems...). If anyone has any comments on the code, additional tips, or pointers to more information on the basics of how image sizes, points, XUnits, etc. all work in PDFSharp or PDFs in general, please chime in. Thanks!
Code:
private void CreatePdf()
{
string basefolder = "C:\\Restored\\Multipage";
foreach (String folder in Directory.EnumerateDirectories(basefolder))
{
PdfDocument document = new PdfDocument();
document.Info.Title = folder;
foreach(String imagefile in Directory.EnumerateFiles(folder))
{
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromFile(imagefile);
page.Height = XUnit.FromPoint(image.PointHeight);
page.Width = XUnit.FromPoint(image.PointWidth);
gfx.DrawImage(image, 0, 0);
}
string filename = folder+".pdf";
if (File.Exists(filename))
{
textbox.Text += "Can't create " + filename + " because the file already exists.\n";
}
else
{
document.Save(filename);
textbox.Text += "Created " + filename + "\n";
//Process.Start(filename);
}
}
}