Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multiplies pages inside a PDF file, requires iText 2.0.8
/*
* To compile and run:
*
* javac -classpath MultiplyPDF.java
* java -classpath iText-2.0.8.jar MultiplyPDF
*
*/
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MultiplyPDF {
public static void main(String[] args) {
// Process arguments
if(args.length < 3) {
System.err.println("ERROR: Insufficient arguments!");
System.err.println("USAGE: [input] [output] [multiply factor]");
System.exit(1);
}
String strInput = args[0];
String strOutput = args[1];
int intFactor = new Integer(args[2]).intValue();
try {
// Setup files
PdfReader inputReader = new PdfReader(strInput);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int maxPageNum = inputReader.getNumberOfPages();
// Print info
System.out.println("Transforming " + strInput + " to " + strOutput);
System.out.println("Factor = " + intFactor);
// Open output
Document document = new Document(inputReader.getPageSize(maxPageNum-1));
PdfCopy copier = new PdfCopy(document, outputStream);
document.open();
// Transform
for (int page=1;page<=maxPageNum;page++){
for(int j=1;j<=intFactor;j++) {
copier.addPage(copier.getImportedPage(inputReader, page));
System.out.println("Page= " + page + ", j= " + j);
}
}
// Write output
document.close();
FileOutputStream output = new FileOutputStream(strOutput);
output.write(outputStream.toByteArray());
output.close();
}catch(Exception ex){
ex.printStackTrace();
System.exit(1);
}
// Finish
System.out.println("done!");
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.