HTML to PDF converter for Java and .NET

HOME   FEATURES   PRODUCTS   DOWNLOADS   BUY NOW!   SUPPORT
<< 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://old.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." );
	}
}
In versions v380-v385 PDF parser of PD4ML has a number of limitations: it cannot parse linearized PDFs and edited PDFs (which have multiple xref tables). Starting with v390 most of PDF parser issues are resolved. Also new PD4Document class makes possible to merge arbitrary PDFs, not related to a HTML-to-PDF conversion.

PD4Document doc1 = new PD4Document(new URL("file:c:/pdfs/doc1.pdf"), null);
int pagenum = doc1.getNumberOfPages();
...
PD4Document doc2 = new PD4Document(new URL("file:c:/pdfs/doc2.pdf"), null);
pagenum = doc2.getNumberOfPages();
...			
doc2.append(doc1);
pagenum = doc2.getNumberOfPages();
...			
File f = new File("c:/pdfs/result.pdf");			
FileOutputStream fos = new FileOutputStream(f);
doc2.write(fos);
 
Copyright ©2004-24 zefer|org. All rights reserved. Bookmark and Share