Menu

Command Line / file list

kjd
2020-04-12
2024-01-30
  • kjd

    kjd - 2020-04-12

    It would be useful to be able to populate the initial file list with a set of filenames
    Obviously passing a single name would be a minimum for say splitting but it gets more complicated to specify ranges etc

    I suggest a plain text file could initially be used to preload the first tab
    c:\path\filename.pdf
    d:\path\second file.pdf

    optionally without making it too complicated for page ranges etc

    Actions.txt

    PDFIN c:\path\filename.pdf
    PDFFILE2 d:\path\second file.pdf
    PDFOUT e:\path\outfile.pdf

     
  • David King

    David King - 2020-04-20

    If you need a free GUI PDF merge tool that directly supports loading PDF documents from a list (.txt file), I recommend you consider PDFsam Basic (pdfsam.org). It does exactly what you are asking for, even allowing the user to drag the .txt file containing the file list directly onto the window of its merge tool.

    The ability to accept an external list of files seems to be rare among PDF applications. To support reloading documents, most applications appear to favor internally generated recently used documents lists, lists of prior sessions, or recently used source document folders. Using a list rather than interactively selecting the files raises the possibility that a file in the list may been misspelled or may have been deleted, renamed, or moved in the interim. I noticed that when PDFsam Basic processes a list containing a file it cannot find, it skips the file but provides no warning to the user that it did so and that the skipped file will therefore be missing from the merged output PDF. Importing page ranges from an external list would be even more problematic.

    I have been able to use a list of files (indirectly) with PDFtk Builder by first running a PowerShell script to copy the PDF files in the list to a destination folder and adding a sequence number prefix to the file names. The PDF files in the destination folder can then be selected all at once and dragged and dropped onto the PDFtk Builder "Join Files' window with the sequence numbers ensuring the original file order in the list is preserved. However, not all users may be able to run a PowerShell script and I suspect the amount of use of a file list option would not warrant its incorporation programmatically into PDFtk Builder.

    PDFsam Basic is a Java app (requires JRE) using a much more recent PDF engine than the one underlying PDFtk and performs merge tasks noticeably quicker than PDFtk Builder. However, for the occasional user, its user interface and available options seem overly complex.

    If you download PDFsam Basic from pdfsam.org using their Windows downloader, please note that there is an option to uncheck the offer to also install the trial version of PDFsam Enhanced, which is a completely different program, not an enhanced version of PDFsam Basic.

     
    • kjd

      kjd - 2020-04-20

      David

      Thanks for the suggestion to use PDFsam, however I am always avoiding Java coded applications.
      I can drag and drop a group of files into SumatraPDF TOC editor to generate a merged PDF, albeit the output currently drops any bookmarks (one usage for pdftk-builder-enhanced) I will look into your suggestion to renumber the files. Thanks
       

      Command Line / file list


      Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/pdftk-builder-enhanced/discussion/general/

       




       
  • David King

    David King - 2020-04-21

    Attached below is a copy of copyfiles.ps1, a Windows PowerShell script that I wrote to copy a list of files to a common destination directory with a 2-digit sequence number prefix added to each file name to maintain file order. This script can be used to populate the 'Join Files' window of PDFtk Builder Enhanced and any other PDF merge tool that supports drag and drop, with files from a list. The following are instructions on how to set up the script and use it.

    1. copyfiles.ps1: Copy the text of PowerShell script below and save it in a file named copyfiles.ps1.

    2. copyfiles.bat: Also copy the following text of the following three-line batch file to run the script in a text file named copyfiles.bat.

    @ECHO OFF
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command ".\copyfiles"
    %SYSTEMROOT%\explorer.exe %TEMP%\copyfiles
    
    1. copyfiles-list.txt: Create your list of .pdf files (full file name including path and ext without quotes) and save it in a text file named copyfiles-list.txt. Note: To capture the text for the full path of an individual file in Windows Explorer, select the file, shift right click, and select the Copy as Path option which will copy the full path to the clipboard. Then paste the copied text into a new line of the list file. Delete the beginning and ending quotation marks.

    2. Place the three files (copyfiles.ps1, .bat, .txt) in a folder of your choice. I suggest using the same folder where PDFtk Builder Enhanced is located. The script will also write a log file (copyfiles-log.txt) to the same folder each time it is run. If the script did not encounter any files not found, the list file and log file should be identical; however, if a file specified in the list cannot be found on the system, the file not found error will be indicated in the log file.

    3. To run the script: Simply double-click on the copyfiles.bat file. The script can also be executed separately by right-clicking on the copyfile.ps1 and selecting "Run with PowerShell". (This step assumes user and system are configured to be able to run a PS script)

    4. Destination folder: The files specified in the list will be copied to the destination folder %TEMP%/copyfiles, which would typically be C:\users\<username>\AppData\Local\Temp\copyfiles. The script creates the copyfiles destination folder if it does exist. Each time it is run, it deletes any .pdf files that had been left in the folder before copying the new set of files to the folder.</username>

    5. Inputting the files to PDFtk Builder: The copyfiles.bat folder will automatically open the destination folder when the script completes. Select all the copied files (ctrl-A) and drag and drop them onto PDFtk Builder's 'Join Files' window. If the drag point where you grabbed the selected files was not on the first file (01-<filename>.pdf), click on "Sort" button in PDFtk Builder to restore the original sequence of the files in the list. </filename>

    Notes:

    a. The copyfiles.bat file can be launched from within PDFtk Builder (Tool Set 4 - Launch) by including an entry for an external PDF app in the PDftkBuuilder.ini file like the following:

    App1Item=1 - Run CopyFiles Script
    App1Exe="C:\Util\PDFtk Builder\copyfiles.bat"
    App1Arg=
    

    In the above example, the files for copyfiles (.bat, .ps1 and .txt) are located in a same folder housing the files for PDFtk Builder.

    b. The # at the start of a line in the PowerShell script below denotes a comment. The script was tested on Windows 10 Pro (x64), version 1909.

    c. Regarding your mention of the TOC Editor in the SumatraPDF pre-release, PDFTK v2.02 which is used by PDFtk Builder Enhanced preserves bookmarks (PDF outlines) when merging full files; PDFTK versions before 2.00 do not. I tried using PDFTK Builder Enhanced to merge multiple PDF files specified in a list and gathered by the copyfiles script as described above and the files were successfully merged and the bookmarks worked in the combined PDF. However, since PDFTK represents bookmarks by level, title and goto page number only, an action such as scrolling to a specific location on a page will become only a goto page number in the merged PDF.

    --

    # copyfiles.ps1 - Copies a list of files to the destination folder, prepending a sequence number to each file name
    
    # Other files
    # Input:  copyfiles-list.txt contains full file names (including path) without quotes of source files in desired file order
    # Output: copyfiles-log.txt  contains full file names of source files processed, should be identical to -list.txt if no errors occurred
    
    # Usage with PDFTK Builder: Multi-select all .pdf files in the destination folder and drag and drop them enmasse onto a clean 
    # Join Files list.  Sort them to restore sequential order (if not grabbed from file 01 when dragging) 
    
    # Specify your destination folder (e.g., %TEMP%\copyfiles)
    # $destdir = 'C:\Temp\copyfiles'
    $destdir = $env:temp + '\copyfiles'
    
    # Create destination folder if it does not already exist
    if (-NOT [IO.Directory]::Exists($destdir)) {mkdir $destdir}
    
    # Delete any existing PDFs left in the destination folder from previous run
    del $destdir\*.pdf
    
    # Create array from new list of files with full path info and then copy each file to destination
    # with a 2-digit sequence number prepended to the file name.  Log error if source file not found.
    
    $i = 0
    $filenames = Get-Content copyfiles-list.txt
    echo $null > copyfiles-log.txt
    foreach ( $file in $filenames ) {
        $i++
        $index = $i.ToString("00") 
        $filepath = Get-ChildItem $file
        $newfile = $destdir + '\' + $index + '-' + $filepath.Name
        echo $newfile
        Copy-Item $file $newfile
        # Log success/fail of each move
        if (Test-Path -LiteralPath $file -PathType Leaf) {echo $file >> copyfiles-log.txt} else {echo "<<< ERROR >>> Source File Not Found: $file" >> copyfiles-log.txt}
    }
    

    --

    Text file copyfiles.ps1 attached

     

    Last edit: David King 2020-04-21
  • Mesmeric

    Mesmeric - 2024-01-30

    I must admit I like the basic premise of the suggestion, but in deference to it not being taken up can I ask something rather simple, that PDFTK could if possible just detect a single file in the command-line startup and open it in Join mode. This would allow the program to be invoked from SumatraPDF as a custom menu item to open the currently-viewing file for extracting pages, or be invoked from explorer on some selected file. It's a bit cumbersome to do without it, and like the original author I tend to avoid Java-y things!

     

Log in to post a comment.