Super Updated Version, Part 1

I’m realizing how difficult it is to go step-by-step, documenting everything! I’ve made some big changes and added a bunch of functions. I’ll explain everything in detail, and of course, you can download the code at the end.

Here are the application settings. They are bound to the controls of some of the forms.

The form’s constructor just has the call to InitializeComponent.

Now, if the user clicks on the Load Folder button, this code executes. As you can see, we start the dialog with any path entered into the toolFolderName textbox. The cool thing is that we don’t have to worry about errors. If it’s an invalid path, the dialog just ignores it. Because it’s a dialog box, we have to dispose of it manually. Now let’s look at the loadPDFs method.

private void toolLoadFolder_Click(object sender, EventArgs e)
{
    var dialog = new CommonOpenFileDialog();
    dialog.IsFolderPicker = true;
    dialog.InitialDirectory = toolFolderName.Text;
    if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
    {
        toolFolderName.Text = dialog.FileName;
        loadPDFs();
    }
    dialog.Dispose();
    toolStripStatusLabel1.Text = filesDataGridView.Rows.Count.ToString() + " Files";
}

Here’s the loadPDFs method. I don’t think it actually changed from last time. We clear the data table, reset the autoincrement, and then create an enumerable collection of FileInfo entries from the text in the toolFolderName. Then we limit the files to visible PDFs and we sort them by name. Then we loop through this limited collection to set the data table rows appropriately.

private void loadPDFs()
{
    dataSet1.Files.Clear();
    dataSet1.Files.RecIDColumn.AutoIncrementStep = -1;
    dataSet1.Files.RecIDColumn.AutoIncrementSeed = -1;
    dataSet1.Files.RecIDColumn.AutoIncrementStep = 1;
    dataSet1.Files.RecIDColumn.AutoIncrementSeed = 1;
    toolStripStatusLabel1.Text = "Reading Files in Folder";
    var dir = new DirectoryInfo(toolFolderName.Text);
    IOrderedEnumerable<FileInfo> files = dir.GetFiles().Where(s => s.Extension.ToLower() == ".pdf" && (s.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden).OrderBy(s => s.Name);
    foreach (FileInfo file in files)
    {
        DataRow dr = dataSet1.Files.NewRow();
        dr.SetField(dataSet1.Files.FileNameColumn, file.Name);
        dr.SetField(dataSet1.Files.PageCountColumn, 0);
        dr.SetField(dataSet1.Files.FullNameColumn, file.FullName);
        dataSet1.Files.AddFilesRow(dr as DataSet1.FilesRow);
    }
    toolStripStatusLabel1.Text = string.Empty;
}

Our last known version could count the number of pages in each PDF, and produce a CSV file with the page ranges. Now we add more functions.

Here is toolMakeEven. If you are going to print a set of PDFs double-sided, you must make sure that every PDF with an odd number of pages has a page added at the end. That is to prevent the situation where the last page of one PDF ends on the front side of a paper sheet, and the first page of the next PDF ends up on the back side of that same sheet of paper. In other words, every document needs to begin on the front side of a paper sheet. This tool does that. We loop through the data table as usual, this time looking for files with an odd number of pages — see how we modulus the number of pages by 2 to see if there’s a 1 left over: proof the document has an odd number of pages. If so, we add a blank page and close the file.

private void toolMakeEven_Click(object sender, EventArgs e)
{
    foreach (DataRow dr in dataSet1.Files.Rows)
    {
        string tempFile = Path.GetTempFileName();
        string pdfFile = dr.Field<string>(dataSet1.Files.FullNameColumn);
        var pdf = new PdfDocument(new PdfReader(pdfFile), new PdfWriter(tempFile));
        if (pdf.GetNumberOfPages() % 2 == 1)
        {
            pdf.AddNewPage(pdf.GetDefaultPageSize());
            pdf.Close();
            File.Copy(tempFile, pdfFile, true);
            File.Delete(tempFile);
            dr.SetField(dataSet1.Files.PageCountColumn, dr.Field<int>(dataSet1.Files.PageCountColumn) + 1);
        }
    }
}

The next function is to insert slipsheets. You may know them by another name. These are the cover sheets to exhibits that say, Exhibit 1, Exhibit 2, etc. It’s not hard to do manually, but when you have forty exhibits, there’s gotta be a better way! We will look at that next.

Leave a Reply

Your email address will not be published. Required fields are marked *