0

I am trying to use iTextSharp to merge 2 or more PDF files. However I am not getting any details about the TrimBox. Performing the code below on the PDF (which was merged) always return NULL

Rectangle rect = reader.GetBoxSize(1, "trim");

This is the code for merging.

public void Merge(List<String> InFiles, String OutFile)
        {
            using (FileStream stream = new FileStream(OutFile, FileMode.Create))
            using (Document doc = new Document())
            using (PdfCopy pdf = new PdfCopy(doc, stream))
            {
                doc.Open();

                PdfReader reader = null;
                PdfImportedPage page = null;

                InFiles.ForEach(file =>
                {
                    reader = new PdfReader(file);

                    for (int i = 0; i < reader.NumberOfPages; i++)
                    {
                         page = pdf.GetImportedPage(reader, i + 1);
                         pdf.AddPage(page);
                    }


                    pdf.FreeReader(reader);
                    reader.Close();
                });
            }
        }

How to keep I keep the box information after the merge?

-Alan-

Alan B
  • 2,219
  • 4
  • 32
  • 62
  • Check out [this answer](http://stackoverflow.com/a/23063576/231316) where Bruno talks about using `AddDocument()` and passing it a `PdfReader` instance instead of going page-by-page. – Chris Haas Sep 11 '15 at 02:21
  • @Chris Hass, what if I want to merge only the first page of each PDF? – Alan B Sep 11 '15 at 03:10
  • I got it .. need to pass the parameter for pagesToKeep . Thanks – Alan B Sep 11 '15 at 03:11

1 Answers1

0

Here is the code I created to merge Portrait and Landscape docs using iTextSharp. It works rather well.

public void MergeFiles(System.Collections.Generic.List<string> sourceFiles, string destinationFile)
{
    Document document=null;
    if (System.IO.File.Exists(destinationFile))
        System.IO.File.Delete(destinationFile);
    try
    {
        PdfCopy writer = null;    
        int numberOfPages=0;
        foreach(string sourceFile in sourceFiles)
        {                    
            PdfReader reader = new PdfReader(sourceFile);
            reader.ConsolidateNamedDestinations();                 
            numberOfPages = reader.NumberOfPages; 

            if(document==null)
            {
                document = new Document(reader.GetPageSizeWithRotation(1));
                writer = new PdfCopy(document, new FileStream(destinationFile, FileMode.Create));                      
                    document.Open();
            }
            for (int x = 1;x <= numberOfPages;x++ )
            {                     
                if (writer != null)
                {
                    PdfImportedPage page = writer.GetImportedPage(reader, x);
                    writer.AddPage(page);
                }
            }
            PRAcroForm form = reader.AcroForm;
            if (form != null && writer != null)                    
                writer.CopyAcroForm(reader);                                       
        }                
    }
    finally
    {
        if (document != null && document.IsOpen())
            document.Close();
    }
}
Ross Bush
  • 14,648
  • 2
  • 32
  • 55