Jump to content
Changes to the Jaspersoft community edition download ×

Combine PDF Files


Recommended Posts

By: Renardo Tyner - rtyner

Combine PDF Files

2003-10-02 09:49

Help! My application creates 7 different pdf files using the method runReportToPdf(), which returns byte[]. Each pdf file is based on a different IReport designed template. At runtime, I would like to be able to create 1 pdf that consists of 7 different pdf files. Is this possible with Jasper or is there utility can do this programmatically.

 

Thanks In Advance!

 

 

 

 

By: Gregory A. Swarthout - gswarthout

RE: Combine PDF Files

2003-10-02 09:57

You can do with with iText programmatically, the library JasperReports uses to generate PDF output.

 

 

 

 

By: Arun Kumar PG - pgarunkumar

RE: Combine PDF Files

2003-10-02 21:12

Dear Renardo,

 

You can combine all PDF files (7 in your case) into a single PDF file by using iText API directly.Here is a method that I have written for appending PDF files.You need to pass two byte[] arrays to this method.The second byte[] array parameter will be appended to the first one and the resulting byte[] array will be returned.Before calling this method get an inputstream to the generated PDF files and convert it into a byte[] array and then pass 2-byte arrays at a time to this method and loop until all files are read.

You are required to have iText library installed in your system.Download the iText JAR from the below given website or if just use iText JAR which you will find in the $JASPER_HOME$/LIB directory.

 

 

import java.io.*;

import java.lang.*;

import java.util.*;

import com.lowagie.text.*;

import com.lowagie.text.pdf.*;

 

public class PDFAppend extends java.lang.Object implements java.io.Serializable {

 

 

class XYZ{

 

public byte[] appendFiles(byte[] src1, byte[] src2) throws Exception {

try {

int f = 0;

// Create a reader for a certain document

PdfReader reader = new PdfReader(src1);

 

// Retrieve the total number of pages

int n = reader.getNumberOfPages();

 

 

// step 1: creation of a document-object

Document document = new Document(reader.getPageSizeWithRotation(1));

// step 2:Create a writer that listens to the document

ByteArrayOutputStream baos = new ByteArrayOutputStream();

PdfWriter writer = PdfWriter.getInstance(document, baos);

 

// step 3: Open the document

document.open();

PdfContentByte cb = writer.getDirectContent();

 

PdfImportedPage page;

int rotation;

// step 4: Add content

{

int i = 0;

while (i < n) {

i++;

document.setPageSize(reader.getPageSizeWithRotation(i));

document.newPage();

page = writer.getImportedPage(reader, i);

rotation = reader.getPageRotation(i);

if (rotation == 90 || rotation == 270) {

cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).height());

} else {

cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);

}

//System.err.println("Processed page " + i);

}

reader = new PdfReader(src2);

n = reader.getNumberOfPages();

}

{

int i = 0;

while (i < n) {

i++;

document.setPageSize(reader.getPageSizeWithRotation(i));

document.newPage();

page = writer.getImportedPage(reader, i);

rotation = reader.getPageRotation(i);

if (rotation == 90 || rotation == 270) {

cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).height());

} else {

cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);

}

}

}

// step 5: we close the document

document.close();

return baos.toByteArray();

} catch (Exception e) {

System.err.println(e.getClass().getName() + ": " + e.getMessage());

}

return null;

}

 

}

 

 

Cheers !

Arun

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...