PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Dec 14, 2018 9:42 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Jul 11, 2018 8:35 pm 
Offline

Joined: Tue Jul 10, 2018 7:46 pm
Posts: 1
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);
                }
            }     
        }


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 11, 2018 9:41 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 429
Location: CCAA
There are 72 points in an inch. Points are a unit that measures distances. Points are not pixels.
Pixel means picture element, the smallest part of a raster image.

If the image has 72 DPI (dots per inch) then you have one dot per point. Each pixel will be one point wide and one point high.

Let's say picture A is 300 pixel wide at 300 DPI. The size of this image is one inch or 72 points.
Let's say picture B is 300 pixel wide at 100 DPI. The size of this image is three inches or 216 points.
Picture A has 4.167 pixels per point, picture B has 1.389 pixels per point.

If the term point gets you on the wrong track then maybe think of inches or centimetres.

For your use case it could provide a better user experience if you'd make all pages (all images) use the same fixed width - say 21 cm or 8 inches - and adjust the page height to maintain the aspect ratio.

Not all image formats have a DPI setting and not all images have it set.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 12, 2018 4:08 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2897
Location: Cologne, Germany
Hi!
dougdougdougdoug wrote:
gfx.DrawImage(image, 0, 0);
There is an overload of DrawImage that takes position and dimensions as parameters.
This is IMHO the recommended method because it does not depend on the DPI setting of the image.

If you invoke DrawImage as shown in the quote than you have to use PointWidth and PointHeight as page dimensions.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group