1

I have searched a few solutions but cannot really find an answer. Within my app i save reports to pdf using IText, but now i would like to merge all the files within the folder as I complete the report.

The issue is each folder contains different number of files so I cannot just hardcode them in.

Any advice will be appreciated.

  • Define "merge" and what's the issue with a variable number of files? – Henry Nov 07 '16 at 08:45
  • Possible duplicate of [How to merge two PDF files into one in Java?](http://stackoverflow.com/questions/3585329/how-to-merge-two-pdf-files-into-one-in-java) – Gimby Nov 07 '16 at 08:45
  • @Henry - "merge" as in combine multiple files. the number can be anything from 2 files to 100+ – Danie van der Bank Nov 07 '16 at 09:08
  • @Gimby as I stated in the question, hardcoding is not an option. – Danie van der Bank Nov 07 '16 at 09:09
  • Still, you haven't clarified what your issue is. Finding the PDFs in the folder or merging them? In either case, what have you tried, what has not worked? – Carsten Nov 07 '16 at 09:13
  • @DanievanderBank there is nothing about hardcoding anywhere. It is quite easy to do a directory listing of PDF files using regular JDK file system APIs. – Gimby Nov 07 '16 at 09:16
  • @Carsten as the request came in this morning, i figured i would ask advice I am busy with different features so i figured i would pick fellow coders' brains. I have not started coding this piece yet. – Danie van der Bank Nov 07 '16 at 09:27
  • @Gimby looking at that example in the link it appears that i had to list each file – Danie van der Bank Nov 07 '16 at 09:28

1 Answers1

5

Here is a working example. I have used ITEXT

Dependencies :

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.10</version>
</dependency>



import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfSmartCopy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;

/**
 * Created by RGOVIND on 11/7/2016.
 */
public class MergePDF {
    static public void main(String[] args) throws Exception{
        mergePDF("C:\\XX\\PDF","mergedFile.pdf");
    }
    public static void mergePDF(String directory, String targetFile) throws DocumentException, IOException {
        File dir = new File(directory);
        File[] filesToMerge = dir.listFiles(new FilenameFilter() {
            public boolean accept(File file, String fileName) {
                //System.out.println(fileName);
                return fileName.endsWith(".pdf");
            }
        });
        Document document = new Document();
        FileOutputStream outputStream = new FileOutputStream("C:\\DevelopmentTools\\PDF\\"+targetFile);
        PdfCopy copy = new PdfSmartCopy(document, outputStream);
        document.open();

        for (File inFile : filesToMerge) {
            System.out.println(inFile.getCanonicalPath());
            PdfReader reader = new PdfReader(inFile.getCanonicalPath());
            copy.addDocument(reader);
            reader.close();
        }
        document.close();
    }
}
Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24