Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Sep 2 '11 at 19:23

This question came from our site for professional and enthusiast programmers.

    
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 '11 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 '11 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 '11 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 '11 at 10:12
    
OK, question updated. –  Archagon Sep 2 '11 at 22:19

2 Answers 2

up vote 2 down vote accepted

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();
        }
    }
}
share|improve this answer
    
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 '11 at 22:14
    
(And by "programs" I mean, programs, libraries, scripts, etc.) –  Archagon Sep 2 '11 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 '11 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 '11 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 '11 at 7:52

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/

share|improve this answer
    
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). –  Ƭᴇcʜιᴇ007 Sep 2 '11 at 20:10
1  
This link is rotten. –  kzh Sep 14 '13 at 0:58
    
I have fixed it –  mark stephens Sep 16 '13 at 14:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.