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>");
}
}
}