Results 1 to 5 of 5

Thread: Progress Bar or BackgroundWorker for my PDF merge application in VB net

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2018
    Posts
    14

    Progress Bar or BackgroundWorker for my PDF merge application in VB net

    I have this application i wrote and its almost complete. This part of the application takes every PDF that has a file number and merges all PDF pages that have the same header. For instance

    ZTEST01 Cover Sheet (1)

    ZTEST01 Cover Sheet (2)

    ZTEST01 Cover Sheet (3)

    ZTEST01 Complaint (1)

    ZTEST01 Complaint (2)

    ZTEST01 Exhibit (1)

    ZTEST01 Exhibit (2)

    ZTEST01 Military (1)

    ZTEST01 Military (2)

    ZTEST01 Military (3)

    ZTEST01 Summons (1)

    ZTEST01 Summons (2)

    Turns into:

    ZTEST01 Cover Sheet

    ZTEST01 Complaint

    ZTEST01 Exhibit

    ZTEST01 Military

    ZTEST01 Summons

    From there it takes the file number and merges the pdf's with Cover Sheet and Complaint into:

    ZTEST01 Cover-Comp Combined

    ZTEST01 Exhibit

    ZTEST01 Military

    ZTEST01 Summons

    After i wrote the application i realized that once all this is done you don't know whether something happened or not unless the user physically checks the output folder where these are supposed to go into to make sure they were actually merged or not.

    I tried first using a rich text box to show a list of the PDF's that are being merged, then i moved onto making a progress bar.

    This is the following code for the entire thing:

    Code:
    Imports System.IO
    Imports PdfSharp.Pdf
    Imports System.IO.Path
    Imports PdfSharp.Pdf.IO
    Imports System.Runtime.InteropServices
    Imports System.Deployment
    Imports Bytescout.PDFExtractor
    Imports System.Collections
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Threading.Tasks
    Imports System
    Imports System.Diagnostics
    Imports ExcelDataReader
    Imports Microsoft
    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Core
    Imports Microsoft.Office.Interop.Excel
    Imports System.Windows.Forms
    Imports System.ComponentModel
    
    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim folderDlg As New FolderBrowserDialog
    
            folderDlg.SelectedPath = "G:\Word\Department Folders\Pre-Suit\Drafts-IL\2-IL_AttyReview\"
            folderDlg.ShowNewFolderButton = True
            If (folderDlg.ShowDialog() = DialogResult.OK) Then
                TextBox1.Text = folderDlg.SelectedPath
            Else
                Return
            End If
            Call Xavier()
        End Sub
    
        Private inputdir As String = Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "unmerged") 
    
        Public Sub Xavier()
            MergeFiles("Cover Sheet", inputdir)
            MergeFiles("Complaint", inputdir)
            MergeFiles("Exhibit", inputdir)
            MergeFiles("Military", inputdir)
            MergeFiles("Summons", inputdir)
        End Sub
    
        Public Sub MergeFiles(ByVal name As String, inputdir As String)
            Try
                Dim OutputFile As String
                Dim OutputDir As String = TextBox1.Text
                Dim OutputDocument As PdfDocument
    
    
    
                For Each files As String In Directory.GetFiles(inputdir, "*" & name & "*.pdf")
                    OutputFile = "\" & GetFileNameWithoutExtension(files).Substring(0, 7) & " " & name & ".pdf"
                    If File.Exists(OutputDir & OutputFile) Then
                        OutputDocument = PdfReader.Open(OutputDir & OutputFile)
                    Else
                        OutputDocument = New PdfDocument()
                    End If
    
                    Using InputDocument As PdfDocument = PdfReader.Open(files, PdfDocumentOpenMode.Import)
                        For Each page As PdfPage In InputDocument.Pages
                            OutputDocument.AddPage(page)
                        Next
    
    
                    End Using
    
                    OutputDocument.Save(OutputDir & OutputFile)
                    OutputDocument.Dispose()
    
                Next
                For Each files In Directory.GetFiles(inputdir, "*" & name & "*.pdf")
                    File.Delete(files)
    
                Next
    
    
                Dim OutputFile2 As String
                Dim OutputDir2 As String = TextBox1.Text
                Dim OutputDocument2 As PdfDocument
                Dim Array = {"Cover Sheet", "Complaint"}
                For Each Ar In Array
                    Dim element = Ar
    
                    For Each files As IO.FileInfo In Get_Files(OutputDir2, IO.SearchOption.TopDirectoryOnly, "pdf", element)
    
                        OutputFile2 = "\" & GetFileNameWithoutExtension(files.ToString).Substring(0, 7) & " Cover-Comp Combined" & ".pdf"
    
    
    
                        RichTextBox3.AppendText(String.Format("Merged: {0}", GetFileName(files.Name) & " as " & GetFileName(OutputFile2) & vbNewLine))
                        ' Sets the starting point of the selection         
                        RichTextBox3.SelectionStart = Len(RichTextBox3.Text)
                        ' Scrolls to the caret
                        RichTextBox3.ScrollToCaret()
                        ' Select the range 
                        RichTextBox3.Select()
    
    
    
                        If File.Exists(OutputDir2 & OutputFile2) Then
                            OutputDocument2 = PdfReader.Open(OutputDir2 & OutputFile2)
                        Else
                            OutputDocument2 = New PdfDocument()
                        End If
    
                        Using InputDocument As PdfDocument = PdfReader.Open(files.ToString, PdfDocumentOpenMode.Import)
    
                            For Each page As PdfPage In InputDocument.Pages
    
                                OutputDocument2.AddPage(page)
    
                                 For Each fileinput As String In Directory.GetFiles(OutputDir2, "*.pdf")
                                ProgressBar1.Minimum = 0
                                ProgressBar1.Maximum = fileinput.Length
                                ProgressBar1.Value = 0
                                ProgressBar1.Visible = True
                                lblPercent.Visible = True
                                Label2.Visible = True
                                Label2.Text = "%"
    
    
    
    
                                For Each p In fileinput
                                ProgressBar1.Value = ProgressBar1.Value + 1
                                lbl1.Text = Int(ProgressBar1.Value * 100 / ProgressBar1.Maximum)
                                lbl1.Refresh()
    
                                Next
                              Next
    
                            Next
                        End Using
    
                        OutputDocument2.Save(OutputDir & OutputFile2)
                        OutputDocument2.Dispose()
                        For Each fileinput As String In Directory.GetFiles(OutputDir2, files.Name)
                            File.Delete(fileinput)
    
                        Next
                        'For i = 1 To Get_Files(OutputDir2, IO.SearchOption.TopDirectoryOnly, "pdf", element).Count
                        '    BackgroundWorker1.RunWorkerAsync()
                        '    lblPercent.Text = i
                        '    BackgroundWorker1.ReportProgress(i)
                        '    System.Threading.Thread.Sleep(200)
                        '    lblPercent.Refresh()
                        ' Next
                    Next
    
                Next
    
    
            Catch ex As Exception
                MsgBox(Err.Number & " " & Err.Source & "-->" & Err.Description, , "Error")
                Return
            End Try
    
        End Sub
    
    
        Private Function Get_Files(ByVal directory As String,
                               ByVal recursive As IO.SearchOption,
                               ByVal ext As String,
                               ByVal with_word_in_filename As String) As List(Of IO.FileInfo)
            Return IO.Directory.GetFiles(directory, "*" & If(ext.StartsWith("*"), ext.Substring(1), ext), recursive) _
                               .Where(Function(o) o.ToLower.Contains(with_word_in_filename.ToLower)) _
            .Select(Function(p) New IO.FileInfo(p)).ToList
        End Function
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If MsgBox("Are you sure you want to exit?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                System.Windows.Forms.Application.Exit()
                Directory.Delete(inputdir, True)
            End If
        End Sub
        Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            Dim msgboxresponse As MsgBoxResult
            If e.CloseReason = CloseReason.UserClosing Then
                msgboxresponse = MsgBox("Are you sure you want to exit?",
                                       MsgBoxStyle.Question + MsgBoxStyle.YesNo, Me.Text)
                If msgboxresponse = MsgBoxResult.Yes Then
                    e.Cancel = True
                    Directory.Delete(inputdir, True)
                    System.Windows.Forms.Application.Exit()
                    Me.Dispose()
                Else
                    Return
                End If
            End If
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Control.CheckForIllegalCrossThreadCalls = False
        End Sub
    
        'Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        '    ProgressBar1.Value = e.ProgressPercentage
        'End Sub
    
        'Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        '    MsgBox("Success")
        'End Sub
    End Class

    I have this application i wrote and its almost complete. This part of the application takes every PDF that has a file number and merges all PDF pages that have the same header. For instance

    ZTEST01 Cover Sheet (1)

    ZTEST01 Cover Sheet (2)

    ZTEST01 Cover Sheet (3)

    ZTEST01 Complaint (1)

    ZTEST01 Complaint (2)

    ZTEST01 Exhibit (1)

    ZTEST01 Exhibit (2)

    ZTEST01 Military (1)

    ZTEST01 Military (2)

    ZTEST01 Military (3)

    ZTEST01 Summons (1)

    ZTEST01 Summons (2)

    Turns into:

    ZTEST01 Cover Sheet

    ZTEST01 Complaint

    ZTEST01 Exhibit

    ZTEST01 Military

    ZTEST01 Summons

    From there it takes the file number and merges the pdf's with Cover Sheet and Complaint into:

    ZTEST01 Cover-Comp Combined

    ZTEST01 Exhibit

    ZTEST01 Military

    ZTEST01 Summons

    After i wrote the application i realized that once all this is done you don't know whether something happened or not unless the user physically checks the output folder where these are supposed to go into to make sure they were actually merged or not.

    I tried first using a rich text box to show a list of the PDF's that are being merged, then i moved onto making a progress bar.

    This is the following code for the entire thing:
    Code:
    Imports System.IO
    Imports PdfSharp.Pdf
    Imports System.IO.Path
    Imports PdfSharp.Pdf.IO
    Imports System.Runtime.InteropServices
    Imports System.Deployment
    Imports Bytescout.PDFExtractor
    Imports System.Collections
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Threading.Tasks
    Imports System
    Imports System.Diagnostics
    Imports ExcelDataReader
    Imports Microsoft
    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Core
    Imports Microsoft.Office.Interop.Excel
    Imports System.Windows.Forms
    Imports System.ComponentModel
    
    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim folderDlg As New FolderBrowserDialog
    
            folderDlg.SelectedPath = "G:\Word\Department Folders\Pre-Suit\Drafts-IL\2-IL_AttyReview"
            folderDlg.ShowNewFolderButton = True
            If (folderDlg.ShowDialog() = DialogResult.OK) Then
                TextBox1.Text = folderDlg.SelectedPath
            Else
                Return
            End If
            Call Xavier()
        End Sub
    
        Private inputdir As String = Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "unmerged") 
    
        Public Sub Xavier()
            MergeFiles("Cover Sheet", inputdir)
            MergeFiles("Complaint", inputdir)
            MergeFiles("Exhibit", inputdir)
            MergeFiles("Military", inputdir)
            MergeFiles("Summons", inputdir)
        End Sub
    
        Public Sub MergeFiles(ByVal name As String, inputdir As String)
            Try
                Dim OutputFile As String
                Dim OutputDir As String = TextBox1.Text
                Dim OutputDocument As PdfDocument
    
    
    
                For Each files As String In Directory.GetFiles(inputdir, "*" & name & "*.pdf")
                    OutputFile = "" & GetFileNameWithoutExtension(files).Substring(0, 7) & " " & name & ".pdf"
                    If File.Exists(OutputDir & OutputFile) Then
                        OutputDocument = PdfReader.Open(OutputDir & OutputFile)
                    Else
                        OutputDocument = New PdfDocument()
                    End If
    
                    Using InputDocument As PdfDocument = PdfReader.Open(files, PdfDocumentOpenMode.Import)
                        For Each page As PdfPage In InputDocument.Pages
                            OutputDocument.AddPage(page)
                        Next
    
    
                    End Using
    
                    OutputDocument.Save(OutputDir & OutputFile)
                    OutputDocument.Dispose()
    
                Next
                For Each files In Directory.GetFiles(inputdir, "*" & name & "*.pdf")
                    File.Delete(files)
    
                Next
    
    
                Dim OutputFile2 As String
                Dim OutputDir2 As String = TextBox1.Text
                Dim OutputDocument2 As PdfDocument
                Dim Array = {"Cover Sheet", "Complaint"}
                For Each Ar In Array
                    Dim element = Ar
    
                    For Each files As IO.FileInfo In Get_Files(OutputDir2, IO.SearchOption.TopDirectoryOnly, "pdf", element)
    
                        OutputFile2 = "" & GetFileNameWithoutExtension(files.ToString).Substring(0, 7) & " Cover-Comp Combined" & ".pdf"
    
    
    
                        RichTextBox3.AppendText(String.Format("Merged: {0}", GetFileName(files.Name) & " as " & GetFileName(OutputFile2) & vbNewLine))
                        ' Sets the starting point of the selection         
                        RichTextBox3.SelectionStart = Len(RichTextBox3.Text)
                        ' Scrolls to the caret
                        RichTextBox3.ScrollToCaret()
                        ' Select the range 
                        RichTextBox3.Select()
    
    
    
                        If File.Exists(OutputDir2 & OutputFile2) Then
                            OutputDocument2 = PdfReader.Open(OutputDir2 & OutputFile2)
                        Else
                            OutputDocument2 = New PdfDocument()
                        End If
    
                        Using InputDocument As PdfDocument = PdfReader.Open(files.ToString, PdfDocumentOpenMode.Import)
    
                            For Each page As PdfPage In InputDocument.Pages
    
                                OutputDocument2.AddPage(page)
    
                                 For Each fileinput As String In Directory.GetFiles(OutputDir2, "*.pdf")
                                ProgressBar1.Minimum = 0
                                ProgressBar1.Maximum = fileinput.Length
                                ProgressBar1.Value = 0
                                ProgressBar1.Visible = True
                                lblPercent.Visible = True
                                Label2.Visible = True
                                Label2.Text = "%"
    
    
    
    
                                For Each p In fileinput
                                ProgressBar1.Value = ProgressBar1.Value + 1
                                lbl1.Text = Int(ProgressBar1.Value * 100 / ProgressBar1.Maximum)
                                lbl1.Refresh()
    
                                Next
                              Next
    
                            Next
                        End Using
    
                        OutputDocument2.Save(OutputDir & OutputFile2)
                        OutputDocument2.Dispose()
                        For Each fileinput As String In Directory.GetFiles(OutputDir2, files.Name)
                            File.Delete(fileinput)
    
                        Next
                        'For i = 1 To Get_Files(OutputDir2, IO.SearchOption.TopDirectoryOnly, "pdf", element).Count
                        '    BackgroundWorker1.RunWorkerAsync()
                        '    lblPercent.Text = i
                        '    BackgroundWorker1.ReportProgress(i)
                        '    System.Threading.Thread.Sleep(200)
                        '    lblPercent.Refresh()
                        ' Next
                    Next
    
                Next
    
    
            Catch ex As Exception
                MsgBox(Err.Number & " " & Err.Source & "-->" & Err.Description, , "Error")
                Return
            End Try
    
        End Sub
    
    
        Private Function Get_Files(ByVal directory As String,
                               ByVal recursive As IO.SearchOption,
                               ByVal ext As String,
                               ByVal with_word_in_filename As String) As List(Of IO.FileInfo)
            Return IO.Directory.GetFiles(directory, "*" & If(ext.StartsWith("*"), ext.Substring(1), ext), recursive) _
                               .Where(Function(o) o.ToLower.Contains(with_word_in_filename.ToLower)) _
            .Select(Function(p) New IO.FileInfo(p)).ToList
        End Function
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If MsgBox("Are you sure you want to exit?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                System.Windows.Forms.Application.Exit()
                Directory.Delete(inputdir, True)
            End If
        End Sub
        Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            Dim msgboxresponse As MsgBoxResult
            If e.CloseReason = CloseReason.UserClosing Then
                msgboxresponse = MsgBox("Are you sure you want to exit?",
                                       MsgBoxStyle.Question + MsgBoxStyle.YesNo, Me.Text)
                If msgboxresponse = MsgBoxResult.Yes Then
                    e.Cancel = True
                    Directory.Delete(inputdir, True)
                    System.Windows.Forms.Application.Exit()
                    Me.Dispose()
                Else
                    Return
                End If
            End If
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Control.CheckForIllegalCrossThreadCalls = False
        End Sub
    
        'Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        '    ProgressBar1.Value = e.ProgressPercentage
        'End Sub
    
        'Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        '    MsgBox("Success")
        'End Sub
    End Class
    The BackgroundWorker stuff that i tried is the stuff commented out. The Progress bar i wrote in there does work... but the problem is that that its doing a progress per PDF page that is being merged so the progress bar goes from 1 to 100% almost instantly for the amount of PDF pages total that are being merged from that particular directory. My question is how would i change that for each or put in a for i = 0 to somethinglength So that the progress bar shows when all the pdf's are complete and outputted into the final directory they're supposed to be in?

    My approach and thinking was this... this application is merging lets say 20 pdf's from inputdir which is just a folder on the users desktop called "unmerged"

    It then merges those pdf's accordingly by File# and their headers and outputs them to folder OutputDir which is "merged" folder.

    From there it takes the PDF's with Cover Sheet and Complaint, merges those 2 together and puts them in OutputDir2 which is still the same "merged" folder from above.

    i was thinking that i could use the list of files that will be merged from inputdir as an array (20 pdf objects), and use that as the array.length to do a

    For each PDF in array.length
    or

    For PDF = 1 to array.length -1
    and i couldn't find a way to make the directory of files an array length or count to do this with.

    I was thinking that in my code, every time the original PDF's that were used to merge are completed, they get deleted from the original directory (all 20 PDF's in the unmerged folder get deleted).

    So from above all the Cover Sheets 1 thru 3 and complaints 1 thru 2, etc... get deleted from their directory with this code:

    Code:
    For Each files In Directory.GetFiles(inputdir, "*" & name & "*.pdf")
                    File.Delete(files)
    
                Next
    And afterward, The original ZTEST01 Cover sheet and ZTEST01 Complaint get deleted once those 2 are merged into "ZTEST01 Cover-Comp Combined" with this code:

    Code:
     For Each fileinput As String In Directory.GetFiles(OutputDir2, files.Name)
                            File.Delete(fileinput)
    
                        Next
    Is it a stretch to try to combine

    Directory.GetFiles(inputdir, "*" & name & "*.pdf")
    with

    Directory.GetFiles(OutputDir2, files.Name)
    as merging 2 arrays; the first being the original 20 PDF objects that were deleted, and then 2 more PDF objects that are deleted once they're merged (ZTEST01 Cover Sheet & ZTEST01 Complaint) and set the combination of both those arrays (22 pdf's or objects) as the .Length of the entire array i want to do a For loop with to give me a progress bar to show the user when the entire application is done?

    Also, with the inclusion of the progress bar, is there anyway to throw a msgBox that shows the user that the entire application is complete once everything is done?

    I tried putting Call Complete() at the end of all this code in a last sub in the for as

    Public Sub Complete
    MsgBox("Complete")
    End Sub
    But this application calls to that way too early before all the merging is even complete.
    Last edited by Pr0x1mo; Oct 12th, 2018 at 09:32 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,927

    Re: Progress Bar or BackgroundWorker for my PDF merge application in VB net

    The ProgressBar will rapidly fill and repeat, because your entire work with it is inside the deepest level of nested loops:
    Code:
               For Each Ar In Array
                    ...
                    For Each files As IO.FileInfo In Get_Files(OutputDir2, IO.SearchOption.TopDirectoryOnly, "pdf", element)
                        ...
                            For Each page As PdfPage In InputDocument.Pages
                                ...
                                For Each fileinput As String In Directory.GetFiles(OutputDir2, "*.pdf")
                                   ProgressBar1.Minimum = 0
    ...and to make it worse, that is within a routine which is called 5 times.

    A very simple way to set it up would be to only work with the ProgressBar in the caller:
    Code:
        Public Sub Xavier()
            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = 5
            MergeFiles("Cover Sheet", inputdir)
            ProgressBar1.Value = ProgressBar1.Value + 1
            MergeFiles("Complaint", inputdir)
            ProgressBar1.Value = ProgressBar1.Value + 1
            MergeFiles("Exhibit", inputdir)
            ProgressBar1.Value = ProgressBar1.Value + 1
            MergeFiles("Military", inputdir)
            ProgressBar1.Value = ProgressBar1.Value + 1
            MergeFiles("Summons", inputdir)
        End Sub
    ..but this will be updated infrequently, so isn't dramatically better than not using a ProgressBar at all.

    To improve it, you need to:
    1. set it up inside Xavier (with a much larger range than 5), and pass parameters into MergeFiles which specify what range of values to set the ProgressBar to (or just how much to increment it by in total).
    2. work out the total number of "steps" that happen inside MergeFiles, including all the loops where you want it... so if you only want it to run for the outer two loops (which is what I would recommend initially), that will be 2 * Get_Files(OutputDir2,...).Count
    3. increment the ProgressBar value at the appropriate point (just before the end of the files loop is probably best).
    Last edited by si_the_geek; Oct 10th, 2018 at 02:27 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2018
    Posts
    14

    Re: Progress Bar or BackgroundWorker for my PDF merge application in VB net

    Quote Originally Posted by si_the_geek View Post
    The ProgressBar will rapidly fill and repeat, because your entire work with it is inside the deepest level of nested loops:
    ...and to make it worse, that is within a routine which is called 5 times.

    A very simple way to set it up would be to only work with the ProgressBar in the caller:
    Code:
        Public Sub Xavier()
            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = 5
            MergeFiles("Cover Sheet", inputdir)
            ProgressBar1.Value = ProgressBar1.Value + 1
            MergeFiles("Complaint", inputdir)
            ProgressBar1.Value = ProgressBar1.Value + 1
            MergeFiles("Exhibit", inputdir)
            ProgressBar1.Value = ProgressBar1.Value + 1
            MergeFiles("Military", inputdir)
            ProgressBar1.Value = ProgressBar1.Value + 1
            MergeFiles("Summons", inputdir)
        End Sub
    ..but this will be updated infrequently, so isn't dramatically better than not using a ProgressBar at all.

    To improve it, you need to:
    1. set it up inside Xavier (with a much larger range than 5), and pass parameters into MergeFiles which specify what range of values to set the ProgressBar to (or just how much to increment it by in total).
    2. work out the total number of "steps" that happen inside MergeFiles, including all the loops where you want it... so if you only want it to run for the outer two loops (which is what I would recommend initially), that will be 2 * Get_Files(OutputDir2,...).Count
    3. increment the ProgressBar value at the appropriate point (just before the end of the files loop is probably best).
    So this just dawned upon me last night. I'm going about the problem all wrong.

    What i realized is that all the files will be in "inputdir" and when they're merged accordingly they will be put into the destination folder the user chooses: OutPutDir...

    So, for example, if there's 2mb of PDF's in inputdir, then by the end of all this there should be 2mb of PDF's in the destination folder, OutPutDir

    Therefore, shouldn't i do something where inputdir.count is going to equal outputdir.count?

    like FinalOutPutDir = inputdir.count

    For i = 0 to FinalOutPutDir.Count

    ProgressBar1.Minimum = 0
    ProgressBar1.Maximum = FinalOutPutDir.Count


    ??? i don't know, im going to try this now

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,927

    Re: Progress Bar or BackgroundWorker for my PDF merge application in VB net

    If I understand you correctly, you know at the start of Xavier the final number of files you should end up with (which at that point is outputdir.count).

    If that is the case, you can simply have Xavier like this:
    Code:
        Public Sub Xavier()
            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = outputdir.count
            MergeFiles("Cover Sheet", inputdir)
            MergeFiles("Complaint", inputdir)
            MergeFiles("Exhibit", inputdir)
            MergeFiles("Military", inputdir)
            MergeFiles("Summons", inputdir)
        End Sub
    ...and then inside MergeFiles (when you finish a file), have this:
    Code:
            ProgressBar1.Value += 1

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2018
    Posts
    14

    Re: Progress Bar or BackgroundWorker for my PDF merge application in VB net

    Quote Originally Posted by si_the_geek View Post
    If I understand you correctly, you know at the start of Xavier the final number of files you should end up with (which at that point is outputdir.count).

    If that is the case, you can simply have Xavier like this:
    Code:
        Public Sub Xavier()
            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = outputdir.count
            MergeFiles("Cover Sheet", inputdir)
            MergeFiles("Complaint", inputdir)
            MergeFiles("Exhibit", inputdir)
            MergeFiles("Military", inputdir)
            MergeFiles("Summons", inputdir)
        End Sub
    ...and then inside MergeFiles (when you finish a file), have this:
    Code:
            ProgressBar1.Value += 1
    Omg yes, this is what i need now. So what i tried first was try to get the total file size in the directory where all the pdf's that needed work to be done were.. but for some reason the extraction/merge process would give me a few digits off so initially it would be 160kb of PDF's... but then the output would be 158kb so their sizes, and they weren't matching up. I said screw it and instead did it by Page count. Initially, its 3 PDF's im doing this with, 18 pages total. The output, is still 18 pages total even though now its 11 PDF's....

    So im going to use what you just wrote, plus this part i found out how to do to get pdf page counts:

    Code:
    Dim _files As Integer = 0
            Dim _filesPdf As Integer = 0
            Dim _pages As Integer = 0
    
            Dim filesTest As String() = IO.Directory.GetFiles(inputdir, "*.pdf")
            For Each filename As String In filesTest
    
    
                If Directory.Exists(filename) Then
                    Dim nestedFilenames As String() = Directory.GetFiles(filename, "*.pdf")
    
                End If
    
                _files += 1
                If New FileInfo(filename).Extension.ToLower() <> ".pdf" Then Continue For
                _filesPdf += 1
                Dim pages As Integer = Count(filename)
                _pages += pages
            Next
            MsgBox(_files.ToString())
            MsgBox(_filesPdf.ToString())
            MsgBox(_pages.ToString())
    I was just using the MsgBox's to test if this worked...

    So initially it would read:

    3 files
    3 pdf files
    18 pages total

    after the splitting and merging the final of it after the whole process

    11 files total
    11 pdf's
    18 pages total

    So yeah, number of pages in always equals number of pages out and i could use that as my length.

    You're awesome man thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width