Best way to create PDF from images

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v5)  /  Best way to create PDF from images

DynamicPDF CoreSuite for .NET (v5) Forum

 Jun 22 2009 11:36 AM
Hello,

I'm writing .NET 3.5 application that should view PDF files and then save rasterized images to new PDF file. PDF loading code looks like this:

private System.Drawing.Bitmap[] imageCache = null;

private void Load_Click(object sender, EventArgs e)
{
    PdfRasterizer rasterizer = new PdfRasterizer("my file.pdf");

    imageCache = new System.Drawing.Bitmap[rasterizer.Pages.Count];
    for (int i = 0; i < rasterizer.Pages.Count; i++)
    {
        Bitmap bitmap = rasterizer.Pages[i].DrawToBitmap(BitmapColorFormat.Rgb, ImageSize.Dpi150);
        imageCache[i] = bitmap;
    }
}

The code above works very fast.

However saving rendered images back to PDF file takes number of minutes on 25 pages file. The code looks like this:

private void SaveButton_Click(object sender, EventArgs e)
{
    ceTe.DynamicPDF.Document document = new ceTe.DynamicPDF.Document();
    document.Creator = "HelloWorld.aspx";
    document.Author = "Your Name";
    document.Title = "Hello World";

    for (int i = 0; i < imageCache.Length; i++)
    {
        // Create a page to add to the document
        ceTe.DynamicPDF.Page page = new ceTe.DynamicPDF.Page(PageSize.A4, PageOrientation.Portrait, 54.0f);
        page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(imageCache[i], 0, 0));

        document.Pages.Add(page);
    }

    document.Draw("my file1.pdf");
}

May be there is some way to improve this code or use another approach for saving images to PDF file?

Thanks in advance,
Mick
Posted by a ceTe Software moderator
Hello Mick,

The code you are using for this is correct. We have tried this code with a 25 page PDF and the second part of code took 1 min 7 sec to save the images as PDF document. Please double check your code if any other process is taking more time and also make sure that you have enough memory for this process.

The time taken may also dependent on the image size you have. If the image quality is more then the size of the image will increase which increased the PDF size and time to draw the PDF to disk.

Thanks,
ceTe Software Support Team.
Hello, thank you for the quick reply. :)

Perhaps I was a bit incorrect in time estimation and real time is about one minute. However this appears very long too - rendering to images on file open takes less then 1 second. And saving the same images takes one minute so the load/save time ratio is 1:60. Looks strange for me, users will not be happy to use application that saves data during one minute...

May be I should use BitmapImageData or ImageData classes to make saving quicker? It seems there is some time consuming conversion between .NET image and DynamicPDF image formats on saving, isn't it?

Thanks,
Mick
Posted by a ceTe Software moderator
Hello,

Yes, using our ImageData and the image formats available with our products, it is working fast. Below is a sample code we used for this. To read the PDF to image it took 5 sec and to save it as PDF it took less than 1 sec.

   private ceTe.DynamicPDF.Imaging.ImageData[] imageCache = null;

   private void button2_Click(object sender, EventArgs e)
   {
      PdfRasterizer rasterizer = new PdfRasterizer(@"filepath");
      imageCache = new ceTe.DynamicPDF.Imaging.ImageData[rasterizer.Pages.Count];
      for (int i = 0; i < rasterizer.Pages.Count; i++)
      {
         Stream stream = new MemoryStream();
         rasterizer.Pages[i].Draw(stream, ImageFormat.Jpeg, ImageSize.Dpi150);
         ceTe.DynamicPDF.Imaging.ImageData data = ceTe.DynamicPDF.Imaging.ImageData.GetImage(stream);
         imageCache[i] = data;
      }
   }

   private void button3_Click(object sender, EventArgs e)
   {
      ceTe.DynamicPDF.Document document = new ceTe.DynamicPDF.Document();
      document.Creator = "HelloWorld.aspx";
      document.Author = "Your Name";
      document.Title = "Hello World";

      for (int i = 0; i < imageCache.Length; i++)
      {
         ceTe.DynamicPDF.Page page = new ceTe.DynamicPDF.Page(PageSize.A4, PageOrientation.Portrait, 54.0f);
         page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(imageCache[i], 0, 0));
         document.Pages.Add(page);
      }
      document.Draw(@"D:\Attachments\251.pdf");
   }

Thanks,
ceTe Software Support Team.
Thanks a lot, this works much better! :)
I have been experimenting with converting PDFs into images and then turning them back into PDFs.  I have found that there might be better way.

During rasterizing, draw the PDF to a byte array and write the array to a Memory Stream, or to File if it serves your purpose.  During conversion back to PDF, do the same thing.  It works much faster. 

The size of the image is a very big problem with conversions.  I need to be able to do conversions at 150dpi, but the files end up huge and the PDFs as a result are about 5 times larger than when they started.  There's a bit to smooth out on my end, but maybe this method can help out.

Posted by a ceTe Software moderator
Hello,

When you convert the image back to a PDF using Generator, the image gets added to the PDF as-is, so using the above suggested code may speed up the process but it may not reduce the size of the resulting PDF. 

The size of the Rasterizer’s output image depends on the dpi setting and the resulting image dimensions. If you want to reduce the final PDF size you would want to optimize the image size when rasterizing the PDF in the first step. You can try using the FixedImageSize or PercentageImageSize classes suggested in the following link.

Thanks,
ceTe Software Support Team.
Yes, you are correct.  The final PDf size is larger than I would like it to be.  Using a lower DPI does help to decrease the final PDF size, but you end up with a poor quality image.  However, I did find that the biggest problem with using images in PDFs is not the size of the image or the DPI, but the type of image being embedded.  Specifically a raster image or vector image.

Unfortunately, rasterized images are extremely large because of their format.  They need to be able to remember pixel values which results in a bitmap or pixel array.

Vector images are great because they do not require every point to be mapped out because they are drawn using formulas.  Very little data is needed for these formats because of this reason.

Embedding a vector image over a raster image into a PDF will result in a much smaller image with high image quality.  It saddens me that converting a raster image to a vector image is not the simplest of tasks, at least not a program that could be written in an hour or two.  It would be really nice if DynamicPDF could work a raster-to-vector based conversion into their libraries.  It would be indispensable.  :)

Thanks for the advice though!

Posted by a ceTe Software moderator
Hello,

Currently the DynamicPDF Rasterizer for .NET does not support vector image output and DynamicPDF Generator for .NET does not support adding vector images to PDF. We will add this to the feature request list for consideration. However at this time we don’t have any immediate plans for supporting vector images in these two products.

Thanks,
ceTe Software Support Team.

All times are US Eastern Standard time. The time now is 6:26 AM.