|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +// The MIT License (MIT) |
| 3 | +// |
| 4 | +// Copyright (c) 2020 Tim Stair |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +//////////////////////////////////////////////////////////////////////////////// |
| 24 | + |
| 25 | +using System; |
| 26 | +using System.Diagnostics; |
| 27 | +using System.IO; |
| 28 | +using System.Windows.Forms; |
| 29 | +using PdfSharp.Pdf; |
| 30 | +using PdfSharp.Pdf.IO; |
| 31 | +using Support.UI; |
| 32 | + |
| 33 | +namespace PdfConstruct.Merge |
| 34 | +{ |
| 35 | + public class PDFMerge |
| 36 | + { |
| 37 | + private QueryPanel zQuery; |
| 38 | + const string FILE_ONE = "1"; |
| 39 | + const string FILE_TWO = "2"; |
| 40 | + const string OPEN_RESULT = "OPEN"; |
| 41 | + |
| 42 | + private const string PDF_FILTER_STRING = "PDF files (*.pdf)|*.pdf"; |
| 43 | + |
| 44 | + public void SetupPanel(Panel zPanel) |
| 45 | + { |
| 46 | + zQuery = new QueryPanel(zPanel, 80, false); |
| 47 | + zQuery.AddLabel("The first page will be taken from File 1", 20); |
| 48 | + SetupDragDropFile(zQuery.AddFileBrowseBox("File 1", string.Empty, PDF_FILTER_STRING, FILE_ONE)); |
| 49 | + SetupDragDropFile(zQuery.AddFileBrowseBox("File 2", string.Empty, PDF_FILTER_STRING, FILE_TWO)); |
| 50 | + zQuery.AddCheckBox("Open Result", false, OPEN_RESULT); |
| 51 | + zQuery.FinalizeControls(); |
| 52 | + } |
| 53 | + |
| 54 | + private void SetupDragDropFile(TextBox textBox) |
| 55 | + { |
| 56 | + textBox.AllowDrop = true; |
| 57 | + textBox.DragEnter += (sender, e) => |
| 58 | + { |
| 59 | + e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) |
| 60 | + ? DragDropEffects.Link |
| 61 | + : DragDropEffects.None; |
| 62 | + }; |
| 63 | + textBox.DragDrop += (sender, e) => |
| 64 | + { |
| 65 | + if (e.Data.GetDataPresent(DataFormats.FileDrop)) |
| 66 | + { |
| 67 | + var arrayFiles = (string[])e.Data.GetData(DataFormats.FileDrop, false); |
| 68 | + if(arrayFiles.Length > 0) ((TextBox) sender).Text = arrayFiles[0]; |
| 69 | + } |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + public void SetupPDFMerge(string sFileOut) |
| 74 | + { |
| 75 | + var sFile1 = zQuery.GetString(FILE_ONE); |
| 76 | + var sFile2 = zQuery.GetString(FILE_TWO); |
| 77 | + |
| 78 | + if (!File.Exists(sFile1) || !File.Exists(sFile2)) |
| 79 | + { |
| 80 | + MessageBox.Show("Input Files Must Exist", "Please select existing files to merge.", |
| 81 | + MessageBoxButtons.OK, MessageBoxIcon.Exclamation); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (string.IsNullOrWhiteSpace(sFileOut)) |
| 86 | + { |
| 87 | + MessageBox.Show("Output Files Must Be Specified", "Please specify an output file.", |
| 88 | + MessageBoxButtons.OK, MessageBoxIcon.Exclamation); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + try |
| 93 | + { |
| 94 | + MergePDFs(sFile1, sFile2, sFileOut); |
| 95 | + MessageBox.Show("Export Complete", "Export Complete", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 96 | + } |
| 97 | + catch (Exception e) |
| 98 | + { |
| 99 | + MessageBox.Show(e.Message, "PDF Merge Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); |
| 100 | + return; |
| 101 | + } |
| 102 | + if (zQuery.GetBool(OPEN_RESULT)) |
| 103 | + { |
| 104 | + Process.Start(sFileOut); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + static void MergePDFs(string sFile1, string sFile2, string sOutFile) |
| 109 | + { |
| 110 | + var zDoc1 = PdfReader.Open(sFile1, PdfDocumentOpenMode.Import); |
| 111 | + var zDoc2 = PdfReader.Open(sFile2, PdfDocumentOpenMode.Import); |
| 112 | + var zDocOutput = new PdfDocument(); |
| 113 | + var nDocIdx = 0; |
| 114 | + while (nDocIdx < zDoc1.PageCount && nDocIdx < zDoc2.PageCount) |
| 115 | + { |
| 116 | + zDocOutput.AddPage(zDoc1.Pages[nDocIdx]); |
| 117 | + zDocOutput.AddPage(zDoc2.Pages[nDocIdx]); |
| 118 | + nDocIdx++; |
| 119 | + } |
| 120 | + |
| 121 | + // swap in doc2 as doc1 (if needed) and flush the rest of the pages |
| 122 | + if (nDocIdx >= zDoc1.PageCount) |
| 123 | + { |
| 124 | + zDoc1 = zDoc2; |
| 125 | + } |
| 126 | + while (nDocIdx < zDoc1.PageCount) |
| 127 | + { |
| 128 | + zDocOutput.AddPage(zDoc1.Pages[nDocIdx]); |
| 129 | + nDocIdx++; |
| 130 | + } |
| 131 | + zDocOutput.Save(sOutFile); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments