views:

10

answers:

0

Sample code:

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

namespace MergePDFs
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {
                int f = 1;
                // we create a reader for a certain document
                PdfReader reader = new PdfReader(args[f]);
                // we retrieve the total number of pagse
                int n = reader.NumberOfPages;
                Console.WriteLine("There are " + n + " pages in the original file.");
                // step 1: creation of a document-object
                Document document = new Document(reader.GetPageSizeWithRotation(1));
                Console.WriteLine("PS: " + reader.GetPageSizeWithRotation(1));
                // step 2: we create a writer that listens to the document
                String destinationFile = args[0];//The first argument is the destination
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
                // step 3: we open the document
                document.Open();
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage page;
                int rotation;
                // step 4: we add content
                while (f < args.Length)
                {
                    int i = 0;
                    while (i < n)
                    {
                        i++;
                        //document.SetPageSize(reader.GetPageSizeWithRotation(i));
                        document.NewPage();
                        page = writer.GetImportedPage(reader, i);
                        rotation = reader.GetPageRotation(i);
                        if (rotation == 90 || rotation == 270)
                        {
                            cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                        }
                        else
                        {
                            cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                        }
                        Console.WriteLine("Processed page " + i);
                    }
                    f++;
                    if (f < args.Length)
                    {
                        reader = new PdfReader(args[f]);
                        // we retrieve the total number of pages
                        n = reader.NumberOfPages;
                        Console.WriteLine("There are " + n + " pages in the original file.");
                    }
                }
                // step 5: we close the document
                document.Close();
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine(e.StackTrace);
            }
        }
    }
}

I would expect this to output a document the same dimensions as the two documents coming (which in my case are the same size). Unfortunately, this is not happening and a white border is being printed around the merged document. Any help would be greatly appreciated.