Why is iTextSharp re-sizing my documents instead of just merging them?

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.

Answers


Try

cb.AddTemplate(page, 0, 0);

It's entirely possible that the original PDFs have crop boxes in addition to their media boxes.

If you really just want to copy pages from files A & B into file C, use PdfCopy.

From http://itextpdf.com/examples/iia.php?id=123

    String[] files = { MovieLinks1.RESULT, MovieHistory.RESULT };
    // step 1
    Document document = new Document();
    // step 2
    PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT));
    // step 3
    document.open();
    // step 4
    PdfReader reader;
    int n;
    // loop over the documents you want to concatenate
    for (int i = 0; i < files.length; i++) {
        reader = new PdfReader(files[i]);
        // loop over the pages in that document
        n = reader.getNumberOfPages();
        for (int page = 0; page < n; ) {
            copy.addPage(copy.getImportedPage(reader, ++page));
        }
    }
    // step 5
    document.close();

This technique will preserve annotations (including fields, though you should really use PdfCopyFields if you want to move fields around PROPERLY), but not bookmarks. I'm not sure about layers... I think so.

Oh, and you probably want to add copy.freeReader(reader) after you're done with each one... particularly if you're planning on looping through a lot of them.


Need Your Help

Wrong document if script dynamically inserted into iframe head

javascript html iframe google-analytics domdocument

On iframe load, I insert google analytics snippet into iframe document HEAD.

Adding class to ValidationSummary

javascript c# jquery css asp.net-mvc

Apparently the unobtrusive js and ModelState errors do not play together. What I want to accomplish is 2 things. When either a ModelState or a client side error occurs, it will be displayed by the