// Created with MonoDevelop
//
//    Copyright (C) 2009 Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System.Collections.Generic;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;


namespace Hiperscan.PdfLib
{
    public static partial class Pdf
    {
        public static void AppendPdfFile(string file_name, string file_to_append)
        {
            string[] files = new string[] { file_name, file_to_append };
            Pdf.AppendPdfFiles(files, file_name);
        }

        public static void AppendPdfFiles(IEnumerable<string> input_files, string output_file)
        {
            PdfDocument output_pdf = new PdfDocument();
            foreach (string file in input_files)
            {
                using (PdfDocument input_pdf = PdfReader.Open(file, PdfDocumentOpenMode.Import))
                {
                    Pdf._CopyPages(input_pdf, output_pdf);
                }
            }

            if (output_pdf.PageCount > 0)
            {
                output_pdf.Save(output_file);
            }
        }

        private static void _CopyPages(PdfDocument from_document, PdfDocument to_document)
        {
            for (int i = 0; i < from_document.PageCount; i++)
            {
                to_document.AddPage(from_document.Pages[i]);
            }
        }
    }
}
