Why are my page dimensions for my new PDF not correct?

Discussions about PDF management.
Post Reply
User avatar
ryancole11
Posts: 21
Joined: Fri May 21, 2010 7:19 pm

Why are my page dimensions for my new PDF not correct?

Post by ryancole11 » Fri May 20, 2011 11:54 pm

Hi all,

I'm working on migrating from the ActiveX API to the .NET API. They're slightly different. I'm simply opening an existing PDF and saving it, as images only, to a new PDF. My code is producing a new PDF but with incorrect page dimensions, or something. The new PDF looks pretty messed up. My code is below, does anyone see why this might be occurring?

Thanks in advance!

Code: Select all

using System;
using System.IO;
using System.Reflection;
using System.Drawing;
using GdPicture;

namespace DasFlattener
{
    class Program
    {
        static void Main(string[] args)
        {
            // make sure we have enough arguments
            if (args.Length != 2)
            {
                PrintUsage();
                return;
            }

            // make sure the input file exists
            String in_file = args[0];
            if (!File.Exists(in_file))
            {
                Console.WriteLine("Error: input file does not exist");
                return;
            }

            // make sure the dictionaries directory exists
            String dictionaries = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\dictionaries";
            if (!Directory.Exists(dictionaries))
            {
                Console.WriteLine("Error: dictionaries do not exist");
                return;
            }

            String out_file = args[1];
            Int32 image_count = 0;

            // imaging objects and license information
            GdViewer viewer = new GdViewer();
            GdPictureImaging imaging = new GdPictureImaging();

            imaging.SetLicenseNumber("xxx");

            // open the original pdf and convert it to an image-only pdf
            viewer.LockViewer = true;
            viewer.SilentMode = true;
            viewer.PdfDisplayFormField = true;

            if (viewer.DisplayFromFile(in_file) == GdPictureStatus.OK)
            {
                // create the output file for the flattened pdf
                Int32 pdf_id = imaging.PdfNewPdf(out_file + ".tmp.pdf");

                for (int x = 1; x <= viewer.PageCount; x++)
                {
                    if (viewer.DisplayPage(x) == GdPictureStatus.OK)
                    {
                        Int32 image_id = imaging.PdfAddImageFromGdPictureImage(pdf_id, viewer.PdfRenderPageToGdPictureImage(200, x));
                        if (image_id != 0)
                        {
                            Console.WriteLine("Flattening page {0}.", x);

                            float image_width = imaging.PdfGetImageWidth(pdf_id, image_id);
                            float image_height = imaging.PdfGetImageHeight(pdf_id, image_id);

                            imaging.PdfSetPageDimensions(pdf_id, image_width, image_height);
                            imaging.PdfNewPage(pdf_id);
                            imaging.PdfDrawImage(pdf_id, image_id, 0, 0, image_width, image_height);
                        }
                    }
                }

                imaging.PdfEndPdf(pdf_id);
                viewer.CloseDocument();
            }
        }

        static void PrintUsage()
        {
            Console.WriteLine("Usage: TheFlattener.exe <input_file> <output_file>");
        }
    }
}

User avatar
ryancole11
Posts: 21
Joined: Fri May 21, 2010 7:19 pm

Re: Why are my page dimensions for my new PDF not correct?

Post by ryancole11 » Mon May 23, 2011 4:58 pm

Also, the resulting PDFs repeat every page for some reason. Something I'm doing must be totally wrong. :(

User avatar
ryancole11
Posts: 21
Joined: Fri May 21, 2010 7:19 pm

Re: Why are my page dimensions for my new PDF not correct?

Post by ryancole11 » Mon May 23, 2011 5:43 pm

Well, I have modified my code a little bit. I have corrected the repeating pages and the weird dimensions. I am running into another issue, though. GdPicture Pro doesn't seem to be able to save these images into PDF at anything higher than 150 DPI. As soon as I bump it up to 200 DPI, or more, it fails to save certain pages. Sadly, 150 DPI just does not look that great. Is there perhaps a reason why I cannot at least save using 200-300 DPI?

Here's my code:

Code: Select all

using System;
using System.IO;
using System.Reflection;
using System.Drawing;
using GdPicture;

namespace DasFlattener
{
    class Program
    {
        static void Main(string[] args)
        {
            // make sure we have enough arguments
            if (args.Length != 2)
            {
                PrintUsage();
                return;
            }

            // make sure the input file exists
            String in_file = args[0];
            if (!File.Exists(in_file))
            {
                Console.WriteLine("Error: input file does not exist");
                return;
            }

            // make sure the dictionaries directory exists
            String dictionaries = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\dictionaries";
            if (!Directory.Exists(dictionaries))
            {
                Console.WriteLine("Error: dictionaries do not exist");
                return;
            }

            String out_file = args[1];

            // imaging objects and license information
            GdViewer viewer = new GdViewer();
            GdPictureImaging imaging = new GdPictureImaging();

            imaging.SetLicenseNumber("xxx");

            // open the original pdf and convert it to an image-only pdf
            viewer.LockViewer = true;
            viewer.SilentMode = true;
            viewer.PdfDisplayFormField = true;

            if (viewer.DisplayFromFile(in_file) == GdPictureStatus.OK)
            {
                // create the output file for the flattened pdf
                Int32 pdf_id = imaging.PdfNewPdf(out_file + ".tmp.pdf");

                for (int x = 1; x <= viewer.PageCount; x++)
                {
                    if (viewer.DisplayPage(x) == GdPictureStatus.OK)
                    {
                        if (imaging.PdfAddImageFromGdPictureImage(pdf_id, viewer.PdfRenderPageToGdPictureImage(150, x), false, true) != 0)
                        {
                            Console.WriteLine("Flattened page {0}.", x);
                        }
                        else
                        {
                            Console.WriteLine("Failed to flatten page {0}.", x);
                        }
                    }
                }

                imaging.PdfEndPdf(pdf_id);
                viewer.CloseDocument();
            }
        }

        static void PrintUsage()
        {
            Console.WriteLine("Usage: TheFlattener.exe <input_file> <output_file>");
        }
    }
}

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Why are my page dimensions for my new PDF not correct?

Post by Loïc » Mon May 23, 2011 7:14 pm

Hi,

You are running in an outofmemory state because you do not release correctly images from memory.

I suggest you to replace this part:

Code: Select all

if (imaging.PdfAddImageFromGdPictureImage(pdf_id, viewer.PdfRenderPageToGdPictureImage(150, x), false, true) != 0)
                        {
                            Console.WriteLine("Flattened page {0}.", x);
                        }
                        else
                        {
                            Console.WriteLine("Failed to flatten page {0}.", x);
                        }
by this one:

Code: Select all

int rasterized_page = viewer.PdfRenderPageToGdPictureImage(150, x);
if (imaging.PdfAddImageFromGdPictureImage(pdf_id, rasterized_page, false, true) != 0)
                        {
                            Console.WriteLine("Flattened page {0}.", x);
                        }
                        else
                        {
                            Console.WriteLine("Failed to flatten page {0}.", x);
                        }
imaging.ReleaseGdPictureImage(rasterized_page ); //release the rasterized page from the memory !!

User avatar
ryancole11
Posts: 21
Joined: Fri May 21, 2010 7:19 pm

Re: Why are my page dimensions for my new PDF not correct?

Post by ryancole11 » Mon May 23, 2011 8:23 pm

Great. That fixed it. Thanks, Loic!

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests