72 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.ComponentModel;
 | |
| using System.Data;
 | |
| using System.Drawing;
 | |
| using System.Text;
 | |
| using System.Windows.Forms;
 | |
| using System.IO;
 | |
| using iTextSharp.text.pdf;
 | |
| using iTextSharp.text;
 | |
| 
 | |
| namespace MergePDFs
 | |
| {
 | |
| 	public partial class frmMergePDFs : Form
 | |
| 	{
 | |
| 		public string Status 
 | |
| 		{
 | |
| 			get { return tsslStatus.Text; }
 | |
| 			set { tsslStatus.Text = value; Application.DoEvents(); }
 | |
| 		}
 | |
| 		public frmMergePDFs()
 | |
| 		{
 | |
| 			InitializeComponent();
 | |
| 		}
 | |
| 
 | |
| 		private void btnFolder_Click(object sender, EventArgs e)
 | |
| 		{
 | |
| 			fbd.SelectedPath = tbFolder.Text;
 | |
| 			if (fbd.ShowDialog() == DialogResult.OK)
 | |
| 				tbFolder.Text = fbd.SelectedPath;
 | |
| 		}
 | |
| 		private void btnMerge_Click(object sender, EventArgs e)
 | |
| 		{
 | |
| 			if (sfd.ShowDialog() == DialogResult.OK)
 | |
| 			{
 | |
| 				Document doc = new Document();
 | |
| 				PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create));
 | |
| 				//writer.PageEvent = new MyPageHelper("Merge2", new Color(System.Drawing.Color.CornflowerBlue));
 | |
| 				doc.Open();
 | |
| 				PdfContentByte canvas = writer.DirectContent;
 | |
| 				DirectoryInfo pdfFolder = new DirectoryInfo(tbFolder.Text);
 | |
| 				FileInfo[] myFiles = pdfFolder.GetFiles("*.pdf");
 | |
| 				foreach (FileInfo myFile in myFiles)
 | |
| 				{
 | |
| 					PdfReader reader = new PdfReader(myFile.FullName);
 | |
| 					int numPages = reader.NumberOfPages;
 | |
| 					int currentPageNumber = 0;
 | |
| 					PdfOutline outline = null;
 | |
| 					do
 | |
| 					{
 | |
| 						currentPageNumber += 1;
 | |
| 						Status = string.Format("Processing {0} page {1}", myFile.Name, currentPageNumber);
 | |
| 						doc.SetPageSize(PageSize.LETTER);
 | |
| 						doc.NewPage();
 | |
| 						PdfImportedPage page = writer.GetImportedPage(reader, currentPageNumber);
 | |
| 						PdfDestination dest = new PdfDestination(PdfDestination.FIT);
 | |
| 						if (currentPageNumber == 1)
 | |
| 							outline = new PdfOutline(canvas.RootOutline, dest, myFile.Name, false);
 | |
| 						else
 | |
| 							new PdfOutline(outline, dest, "Page " + currentPageNumber.ToString(), false);
 | |
| 						canvas.AddTemplate(page, 0, 0);
 | |
| 						//DrawOutlines(canvas);
 | |
| 					} while (currentPageNumber < numPages);
 | |
| 				}
 | |
| 				doc.Close();
 | |
| 				if (MessageBox.Show("Do you want to open the merged file?", "Merging Complete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
 | |
| 					System.Diagnostics.Process.Start(sfd.FileName);
 | |
| 			}
 | |
| 			Status = "Processing Complete";
 | |
| 		}
 | |
| 	}
 | |
| } |