4 Replies - 1278 Views - Last Post: 01 December 2010 - 08:19 AM Rate Topic: -----

#1 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

What library can I use to output a PDF document?

Posted 30 November 2010 - 07:15 PM

What library can I use in a C#/.NET 4 application that can let me output a PDF document?

Imagine I have this:

List<Image> Pictures = GetPictureCollection();


I'd like to have each Image in my collection be a single page, and all of them be collected into a single PDF document.

Any suggestions? I'm looking for ease of use, I don't really want to reinvent the wheel on this.

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: What library can I use to output a PDF document?

#2 Guest_Guest*


Reputation:

Re: What library can I use to output a PDF document?

Posted 30 November 2010 - 07:34 PM

ITextSharp is good and free
Was This Post Helpful? 0

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: What library can I use to output a PDF document?

Posted 30 November 2010 - 07:45 PM

I would also suggest ITextSharp. I've never used it for pictures, but you can look into it.
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: What library can I use to output a PDF document?

Posted 01 December 2010 - 07:12 AM

Thanks for the suggestion guys, it works beautifully! What a great library!

I'm having some trouble though. The pictures are added but cropped. I found iTextSharp has a method in the Image class called ScaleToFit() that takes two float parameters, but it seems that I don't know how to use it.

Here's my entire PDF creation class I made. It's pretty straightforward.

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing;
using System.Collections.Generic;

namespace WinformsPlayground
{
    public class PDFWrapper
    {
        public void CreatePDF(List<System.Drawing.Image> images)
        {
            if (images.Count >= 1)
            {
                Document document = new Document(PageSize.LETTER);
                try
                {

                    // step 2:
                    // we create a writer that listens to the document
                    // and directs a PDF-stream to a file

                    PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.Create));

                    // step 3: we open the document
                    document.Open();

                    foreach (var image in images)
                    {
                        iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
                        pic.ScaleToFit(200f, 200f);
                        document.Add(pic);
                        document.NewPage();
                    }
                }
                catch (DocumentException de)
                {
                    Console.Error.WriteLine(de.Message);
                }
                catch (IOException ioe)
                {
                    Console.Error.WriteLine(ioe.Message);
                }

                // step 5: we close the document
                document.Close();
            }
        }
    }
}



http://i.imgur.com/bHS6p.png

Any ideas on how to use the ScaleToFit() method? I'd just like the pictures to be like the PictureBox.Zoom method, the image stretches but also maintains the aspect ratio.

Thanks for your time.
Was This Post Helpful? 0
  • +
  • -

#5 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: What library can I use to output a PDF document?

Posted 01 December 2010 - 08:19 AM

In case anyone needs this, I solved it using the following code:

iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);

if (pic.Height > pic.Width)
{
    //Maximum height is 700 pixels.
    float percentage = 0.0f;
    percentage = 700 / pic.Height;
    pic.ScalePercent(percentage * 100);
}
else
{
    //Maximum width is 540 pixels.
    float percentage = 0.0f;
    percentage = 540 / pic.Width;
    pic.ScalePercent(percentage * 100);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1