0

im not that good in Java but because of the fact, that Talend Studio doesnt support PDF-splitting and merging I had to search of another solution.

To explain my situation:

The task here is to split a PDF with n-pages into n-PDF files. (One for each page). After that, I need to merge each of there PDF-Files with another Fixed PDF. (creating Letters with individual beginnings and fixed followings)

The PDF, i want to split, is automatically created in Talend using "TJasperReportExec" with a concrete Template which isnt supporting (afaik) the splitting of PDF-Files as well.

After some research i found that Code:

package pdfsplitter;

import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

public class SplitPDFFile {
/**
 * @param args
 */
public static void main(String[] args) {

    try {
        String inFile = args[0].toLowerCase();
        System.out.println ("Reading " + inFile);
        PdfReader reader = new PdfReader(inFile);
        int n = reader.getNumberOfPages();
        System.out.println ("Number of pages : " + n);
        int i = 0;
        while ( i < n ) {
            String outFile = inFile.substring(0, inFile.indexOf(".pdf"))
                + "-" + String.format("%03d", i + 1) + ".pdf";
            System.out.println ("Writing " + outFile);
            Document document = new Document(reader.getPageSizeWithRotation(1));
            PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
            document.open();
            PdfImportedPage page = writer.getImportedPage(reader, ++i);
            writer.addPage(page);
            document.close();
            writer.close();
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    /* example :
        java SplitPDFFile d:\temp\x\tx.pdf

        Reading d:\temp\x\tx.pdf
        Number of pages : 3
        Writing d:\temp\x\tx-001.pdf
        Writing d:\temp\x\tx-002.pdf
        Writing d:\temp\x\tx-003.pdf
     */

}
}

The Error here seems to be the Value of args[0] after the definition of the name of the String.

After searching for a solution and trying to fix it in different ways the error didnt got fixed once. Some advice or a link with a proper solution would be nice.

Thanks in advance,

Ulonis

Ulonis
  • 1
  • 1
  • `args[0]` is the value of the first parameter given to the `java` executable when launching this class, did you provide one? – Aaron Jan 30 '17 at 09:46
  • when you run the java code, you need to pass arguments at run time. There is nothing in args[] and hence array out of bound . Take this for example - http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java – voucher_wolves Jan 30 '17 at 09:47
  • Answered to the Problem right now. Still a big Thank you for you both. :) – Ulonis Jan 30 '17 at 14:04

1 Answers1

0

Found the Solution for my problem a few minutes ago. Like Aaron & yashpandey already told: There was no value (in this case no pdf file) given to split.

My solution here is:

public static void main(String[] args) {

        try {
            String s = "C:\\Users\\User\\PDFFile.pdf";
            String inFile = s.toLowerCase();
            System.out.println ("Reading " + inFile);
            PdfReader reader = new PdfReader(inFile);

I deleted the args[0] and created another String with a Hardcoded Link to this file. In this case also possible because this job will be automatically started on an server and the Name of this File can be everytime the same. Normally I would have prefered some softcoding here to get the Link but whatever. It works and it does what it has to.

Thanks for the suggestions. If I hadn't found the solution for myself I wouldve got it after your comments.

Ulonis

Ulonis
  • 1
  • 1