using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ExpertPdf.MergePdf;

namespace WinForms_MergePdfDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            PDFMerge pdfMerger = new PDFMerge();

            //pdfMerger.LicenseKey = "put your license key here";

            // set the merged document info
            pdfMerger.DocumentInfo.AuthorName = "ExpertPDF Merge for .NET";
            // set the merged document security and rights
            if (textBoxPassword.Text.Trim().Length > 0)
                pdfMerger.SecurityOptions.UserPassword = textBoxPassword.Text.Trim();
            pdfMerger.SecurityOptions.CanPrint = true;

            // append the first PDF document
            string pdfFile1Path = textBoxPdfFile1Path.Text.Trim();
            int startPageIndex1 = int.Parse(textBoxStartPageIndex1.Text.Trim());
            int length1 = int.Parse(textBoxLength1.Text.Trim());
            if (length1 < 0)
            {
                // append all pages starting from index
                pdfMerger.AppendPDFFile(pdfFile1Path, 0);
            }
            else
            {
                // append length pages from the start index
                pdfMerger.AppendPDFFile(pdfFile1Path, 0, length1);
            }

            // append the second PDF document
            string pdfFile2Path = textBoxPdfFile2Path.Text.Trim();
            int startPageIndex2 = int.Parse(textBoxStartPageIndex2.Text.Trim());
            int length2 = int.Parse(textBoxLength2.Text.Trim());

            if (length2 < 0)
            {
                // append all pages starting from index
                pdfMerger.AppendPDFFile(pdfFile2Path, 0);
            }
            else
            {
                // append length pages from the start index
                pdfMerger.AppendPDFFile(pdfFile2Path, 0, length2);
            }

            // generate the merged document bytes
            byte[] bytes = pdfMerger.RenderMergedPDFDocument();

            string outFile = System.IO.Path.Combine(Application.StartupPath, "Merged.pdf");

            // save the merged document in a file
            // the code below can be replaced by pdfMerger.SaveMergedPDFToFile(outFile);
            System.IO.FileStream fs = null;
            fs = new System.IO.FileStream(outFile, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.Read);
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();

            // open the saved document in an external PDF viewer
            if (MessageBox.Show("Open the saved PDF file in an external viewer?", "Open PDF File",
                MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                System.Diagnostics.Process.Start(outFile);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBoxPdfFile1Path.Text = System.IO.Path.Combine(Application.StartupPath, @"..\..\htmltopdf.pdf");
            textBoxPdfFile2Path.Text = System.IO.Path.Combine(Application.StartupPath, @"..\..\google.pdf");
        }

        private void btnSelectFile1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBoxPdfFile1Path.Text = fd.FileName;
            }
        }

        private void btnSelectFile2_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBoxPdfFile2Path.Text = fd.FileName;
            }
        }
    }
}