<< back

PD4ML: PDF Merge

 

PD4ML allows to append (or prepend) a just converted document with PDF pages, taken from a static PDF file.

package samples;

import java.awt.Insets;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.InvalidParameterException;

import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;

public class PdfMerge {
	protected int topValue = 10;
	protected int leftValue = 20;
	protected int rightValue = 10;
	protected int bottomValue = 10;
	protected int userSpaceWidth = 1300;

	public static void main(String[] args) {
		try {
			PdfMerge jt = new PdfMerge();
			jt.doConversion3("http://pd4ml.com/sample.htm", "c:/pd4ml.pdf", 
					"c:/appendix.pdf");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doConversion3( String url, String outputPath, String pdfToMerge ) 
					throws InvalidParameterException, MalformedURLException, IOException {
			File output = new File(outputPath);
			java.io.FileOutputStream fos = new java.io.FileOutputStream(output);

			PD4ML pd4ml = new PD4ML();

			pd4ml.setHtmlWidth(userSpaceWidth); 
			pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
			pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));
			
			pd4ml.merge(
					new FileInputStream(pdfToMerge), // PDF file input stream 
					1,  // starting PDF page number to merge
					-1, // last PDF page to merge ("-1" means "till the document end")
					true ); // "true" - append, 
							// "false" - prepend the generated document with the static pages

			pd4ml.render(new URL(url), fos); // actual document conversion from URL to file
			fos.close();
			
			System.out.println( outputPath + "\ndone." );
	}
}
Note: currently (v380) PDF parser of PD4ML has a number of limitations: it cannot parse linearized PDFs and edited PDFs (which have multiple xref tables).