Spliting pdf file by Mb size

prati

Board Regular
Joined
Jan 25, 2021
Messages
51
Office Version
  1. 2019
Platform
  1. Windows
Hey friends,
I wonder if there is a way to split pdf file through VBA into multiple pdf files so that each part will not be larger than 8Mb

Im using PDFtk server to split pdf file by defining the exact rang of pages. It splits the file into multiple files but doesn't take into account the file size

1621269802959.png

if FileLen(File1) / 1000000 > 8 then 'this check if the file is larger than 8mb
if totalPages < 100 then 'check the total page numbers
Wsh.Run ("cmd /c PDFtk " & "C:\Temp\oldfile.pdf" & " cat 1-50 output C:\Temp\newfilePart1.pdf"""), 0, True
Wsh.Run ("cmd /c PDFtk " & "C:\Temp\oldfile.pdf" & " cat 51-end output C:\Temp\newfilePart2.pdf"""), 0, True
Elseif totalPages < 150 then
Wsh.Run ("cmd /c PDFtk " & "C:\Temp\oldfile.pdf" & " cat 1-50 output C:\Temp\newfilePart1.pdf"""), 0, True
Wsh.Run ("cmd /c PDFtk " & "C:\Temp\oldfile.pdf" & " cat 51-100 output C:\Temp\newfilePart2.pdf"""), 0, True
Wsh.Run ("cmd /c PDFtk " & "C:\Temp\oldfile.pdf" & " cat 101-end output C:\Temp\newfilePart3.pdf"""), 0, True
Elseif totalPages < 200 then
....
Elseif totalPages < 250 then
...
and so on.

The code above dosen't help me because it split the files by number of pages and not by the size of the file as i want it to..

I found a program caled UnityPdf - it is a free program that can do the job easily, but i don't know if there is a way to write a vba command using UnityPdf in order to split the pdf file

View attachment 38890
 
Great!
Works Indeed.
You always have the right solution
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
If you've properly installed PDFtk Server in a specific location (and not just copied pdftk.exe there) then as part of the installation steps it asks if you want to add the installation folder to the path environment variable. If you answered 'Yes' to that question then the code should work exactly as before without any changes.

As a test, I haven't installed PDFtk Server, but have changed the code to specify the full path to pdftk.exe as "C:\Program Files (x86)\PDFtk Server\bin\PDFtk.exe". Therefore change "C:\Program Files (x86)\PDFtk Server\bin\PDFtk.exe" to "C:\PdfPrograms\pdftk.exe" in the following code and see if it works for you.

VBA Code:
Public Sub PDFtk_Split_PDF_By_Size2()

    Dim Wsh As Object 'WshShell
    Dim command As String
    Dim PDFinputFullName As String, PDFfolder As String, PDFfile As String
    Dim maxFileSizeKB As Long
    Dim pageFile As String
    Dim page As Long
    Dim totalFileSizeKB As Single, thisFileSizeKB As Single
    Dim pageFiles As String
    Dim part As Long
   
    'PDF file to be split into multiple parts
   
    PDFinputFullName = "C:\path\to\file.pdf"
   
    'Maximum size of each part in kilobytes
   
    maxFileSizeKB = 2048
   
    Set Wsh = CreateObject("WScript.Shell")  'New WshShell
   
    If Dir(PDFinputFullName) <> vbNullString Then
   
        PDFfolder = Left(PDFinputFullName, InStrRev(PDFinputFullName, "\"))
        PDFfile = Mid(PDFinputFullName, InStrRev(PDFinputFullName, "\") + 1)
       
        'Delete existing _Page_nnn.pdf and _Part_nnn.pdf files for the input file
       
        command = "DEL " & Q & Replace(PDFinputFullName, ".pdf", "_Page_*.pdf") & Q
        Debug.Print command
        Wsh.Run "cmd /c " & command, 0, True
        command = "DEL " & Q & Replace(PDFinputFullName, ".pdf", "_Part_*.pdf") & Q
        Debug.Print command
        Wsh.Run "cmd /c " & command, 0, True
       
        'Run PDFtk burst command to create multiple _Page_nnn.pdf files, one for each page in the input PDF

        command = Q & "C:\Program Files (x86)\PDFtk Server\bin\PDFtk.exe" & Q & " " & Q & PDFinputFullName & Q & " burst output " & Q & Replace(PDFinputFullName, ".pdf", "_Page_%03d.pdf") & Q
        Debug.Print command
        Wsh.Run command, 0, True
       
        'Loop through the _Page_nnn.pdf files in order and create _Part_nnn.pdf files whose size is less than the maximum file size.
       
        totalFileSizeKB = 0
        pageFiles = ""
        page = 0
        part = 0
        Do
            page = page + 1
            'Get the next _Page_nnn.pdf file
            pageFile = Dir(Replace(PDFinputFullName, ".pdf", "_Page_" & Format(page, "000") & ".pdf"))
            If pageFile <> vbNullString Then
                thisFileSizeKB = FileLen(PDFfolder & pageFile) / 1024
                Debug.Print pageFile; " size " & thisFileSizeKB, "total size " & totalFileSizeKB
                'Is this PDF page file size plus the current total file size less than the maximum file size?
                If totalFileSizeKB + thisFileSizeKB <= maxFileSizeKB Then
                    'Yes, so add this PDF page file to the string of files and increment the current total file size
                    pageFiles = pageFiles & Q & pageFile & Q & " "
                    totalFileSizeKB = totalFileSizeKB + thisFileSizeKB
                Else
                    'No, so run PDFtk cat command to catenate the current PDF page files to the next PDF file named _Part_nnn.pdf
                    If pageFiles <> "" Then
                        part = part + 1
                        command = "CD /D " & Q & PDFfolder & Q & " & " & Q & "C:\Program Files (x86)\PDFtk Server\bin\PDFtk.exe" & Q & " " & pageFiles & "cat output " & Q & Replace(PDFfile, ".pdf", "_Part_" & Format(part, "000") & ".pdf") & Q
                        Debug.Print command
                        Wsh.Run "cmd /c " & command, 0, True
                    End If
                    'Initialise the PDF page files with this PDF file and the total file size
                    pageFiles = Q & pageFile & Q & " "
                    totalFileSizeKB = thisFileSizeKB
                End If
            End If
        Loop Until pageFile = vbNullString
       
        'If the current PDF page files isn't empty then run PDFtk cat command to catenate them to the next PDF file named _Part_nnn.pdf
       
        If pageFiles <> "" Then
            part = part + 1
            command = "CD /D " & Q & PDFfolder & Q & " & " & Q & "C:\Program Files (x86)\PDFtk Server\bin\PDFtk.exe" & Q & " " & pageFiles & "cat output " & Q & Replace(PDFfile, ".pdf", "_Part_" & Format(part, "000") & ".pdf") & Q
            Debug.Print command
            Wsh.Run "cmd /c " & command, 0, True
        End If
       
        'Delete all _Page_nnn.pdf files for the input file created by PDFtk burst command above
       
        command = "DEL " & Q & Replace(PDFinputFullName, ".pdf", "_Page_*.pdf") & Q
        Debug.Print command
        Wsh.Run "cmd /c " & command, 0, True
       
        'Delete doc_data.txt file created by PDFtk burst command above
       
        If Dir(PDFfolder & "doc_data.txt") <> vbNullString Then Kill PDFfolder & "doc_data.txt"
       
        MsgBox "Done"
                   
    Else
   
        MsgBox "Error opening PDF file " & PDFinputFullName
   
    End If
   
End Sub
Great!
Works Indeed.
You always have the right solution
 
Upvote 0
I haven't installed PDFtk Server, but have changed the code to specify the full path to pdftk.exe as "C:\Program Files (x86)\PDFtk Server\bin\PDFtk.exe"
Correction: I installed PDFtk Server to its default location, "C:\Program Files (x86)\PDFtk Server\" and changed the code to specify the full path to pdftk.exe as "C:\Program Files (x86)\PDFtk Server\bin\PDFtk.exe".
 
Upvote 0
Hi is there a way to split multiple files in a folder with this script ?
 
Upvote 0
Hi is there a way to split multiple files in a folder with this script ?
Yes, you could modify PDFtk_Split_PDF_By_Size2() to accept an argument, PDFtk_Split_PDF_By_Size2(PDFinputFullName As String), and remove the first If (Dir ...) Then .... End If block. Have a main procedure with a Dir loop which calls PDFtk_Split_PDF_By_Size2 for each PDF file in the folder.

Please start a new thread if you need more help.
 
Upvote 0

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top