5

I'd like to make a PDF file comprised entirely of a folder of images, preferably with no added compression or cruft. Are there any programs that would let me author the PDF file manually via script or API? The important thing is that a) my JPEGs don't get compressed a second time, and b) they're displayed at their native resolution, with no borders. (In other words, I'd like my PDF file to have nothing more than the straight JPG image data centered at 0,0 on each page, sized correctly.) As far as I can tell, most authoring programs recompress the images and add their own layout stuff.

7
  • What's an "authoring tool" that you object to? If I use Mac OS iWork Pages and "save as PDF" would the Pages be an authoring tool? If not, why not? Please be precise on what "authoring tool" means to you.
    – S.Lott
    Sep 2, 2011 at 10:05
  • Any tool that pre-formats the pages and recompresses the images, which is basically all of them. To me, the important thing is that a) my JPEGs don't get compressed a second time, and b) they're displayed at their native resolution, with no borders. As far as I can tell, most authoring programs recompress the images and add their own layout stuff.
    – Archagon
    Sep 2, 2011 at 10:09
  • In other words, I'd like my PDF file to have nothing more than the straight JPG image data centered at 0,0 on each page, sized correctly.
    – Archagon
    Sep 2, 2011 at 10:12
  • So "Are there any programs that would let me generate the PDF file directly, without going through authoring tools?" is irrelevant? If so, please remove it from the question. "my JPEGs don't get compressed a second time, and b) they're displayed at their native resolution" is your real question? If so, please update the question to actually state what you actually want to do. Your question and your comments don't agree. Please update the question.
    – S.Lott
    Sep 2, 2011 at 10:12
  • OK, question updated.
    – Archagon
    Sep 2, 2011 at 22:19

2 Answers 2

5

You say "by hand" which confused some people, I think. You're not actually saying that you want to write raw PDF, right? Below is code that uses the open source iTextSharp library (5.1.1.0). Set the variable FolderWithImages to your folder containing images and PdfFileName to the PDF that you want to kick out and it will take all JPGs in the folder and create a PDF. This code is very simple but you can do a lot of things like resizing, scaling, etc. There's tons of code out there for iTextSharp and its parent project iText.

using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //The folder containing our images
            string FolderWithImages = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            //The PDF that we will output
            string PdfFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ImagesCombined.pdf");

            //Create a basic stream to write to
            using (FileStream fs = new FileStream(PdfFileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //Create a new PDF document
                using (Document doc = new Document())
                {
                    //Bind a the document to the stream
                    using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                    {
                        //Open our document for writing
                        doc.Open();

                        //Will hold an instance of our image
                        iTextSharp.text.Image img;

                        //Grab all JPGs from the given folder and loop through them
                        string[] Images = Directory.GetFiles(FolderWithImages, "*.jpg", SearchOption.TopDirectoryOnly);
                        foreach (string i in Images)
                        {
                            //Get the JPG as an iTextSharp "image"
                            img = iTextSharp.text.Image.GetInstance(i);

                            //Tell the image that when placed we want it at (0,0)
                            img.SetAbsolutePosition(0, 0);

                            //Tell the system that the next "page" that we add should be the dimension of the image
                            doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, img.Width, img.Height));

                            //Add a new blank page
                            doc.NewPage();

                            //Put the image on the blank page
                            doc.Add(img);
                        }

                        //Close our output PDF
                        doc.Close();
                    }
                }
            }
            this.Close();
        }
    }
}
5
  • I think this is pretty much exactly what I'm looking for! For future reference, are you aware of any other programs like this?
    – Archagon
    Sep 2, 2011 at 22:14
  • (And by "programs" I mean, programs, libraries, scripts, etc.)
    – Archagon
    Sep 2, 2011 at 22:22
  • Yep, there's a bunch of them out there. PDFTK, Aspose.Pdf, Essential PDF, AspPDF. Just search for "c# pdf library".
    – Chris Haas
    Sep 3, 2011 at 12:42
  • Heh, I'm an idiot. Searching for "PDF library" gave me a bunch of great results. All this time I've been looking for the wrong thing. Thanks!
    – Archagon
    Sep 3, 2011 at 18:04
  • Used a version of your code with the Java iText library. Works perfectly — exactly what I wanted! Thank you again.
    – Archagon
    Sep 9, 2011 at 7:52
7

Creating PDF files by hand is not recommend for the faint hearted! If you really want to try it, we wrote a series on creating PDF files by hand on our blog http://blog.idrsolutions.com/2010/09/grow-your-own-pdf-file-part-1-pdf-objects-and-data-types/

5
  • 2
    Link-only answer are discouraged on SU due to possible link-rot in the future. Please attempt to include pertinent information in your answer(s). Sep 2, 2011 at 20:10
  • 2
    This link is rotten.
    – kzh
    Sep 14, 2013 at 0:58
  • 2
    I have fixed it Sep 16, 2013 at 14:23
  • You should still include at least some details in the answer itself and not rely solely on the link.
    – Tom
    Jan 7, 2021 at 21:20
  • That somewhat reversed approach is very interesting. Cool article, thanks for writing it!
    – foxesque
    Jan 8 at 8:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .