Hi Colleagues,

 

I have some code from internet and I try to adopt it to my requirements.

I’m new in transforming xml into PDF. I decided to use FOP.

I would like to ask you to verify this code.

In this code I have following problems:

1.  I get ArrayIndexOutOFBoundsException. –My comment is in red color in the code.

2.  I am not sure whether I deliver xml file in proper way. This file is to be transforming into PDF.

3.  I’m not sure in which place we get PDF file. I wanted to save this file at hard disk as follow or in any other way.

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Test.pdf"));

 

CODE IS BELLOW.

Thank you very much for your help!

 

Regards,

Rafal

 

 

 

 

import

 

org.xml.sax.helpers.DefaultHandler;

import

 

org.apache.fop.apps.Fop;

public

 

class MainPDF {

 

private static final String RESOURCE_PATH = "no/arrive/pdf/";

 

 

private static final String xmlFileSource = "/" + RESOURCE_PATH + "ReceiptData.xml"

 

public static void main(String args[]) {

 

 

 

try

{

ByteArrayOutputStream fopout =

new ByteArrayOutputStream();

FileOutputStream outfile =

new

FileOutputStream(args[2]);  //WHEN i RUN PROJECT I GET IN THIS LINE

java.lang.ArrayIndexOutOfBoundsException

 

FopFactory fopFactory = FopFactory.newInstance();

Fop fop = fopFactory.newFop(MimeConstants.

MIME_PDF, fopout);

 

Transformer transformer = TransformerFactory.newInstance().newTransformer(

new StreamSource(newFile(args[1])));

 

//Adding xml file which iwill be converted into PDF - I'm not sure if it is OK

 String in ="";

in.getClass().getResourceAsStream(

xmlFileSource);

fopFactory.setUserConfig(in); 

 

// transformer.transform(new StreamSource(new File(args[0])),new SAXResult(fop.getDefaultHandler()));  //When I use this line I get syntax error on getDefaultHandler() at this method is not defined for FOP and that's why I have used null argument as bilow: 

transformer.transform(

new StreamSource(new File(args[0])), null);

PdfReader reader =

newPdfReader(fopout.toByteArray());

 

int n = reader.getNumberOfPages();

Document document =

new Document(reader.getPageSizeWithRotation(1));

 

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

 

// PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Test.pdf"));

 

document.open();

 

writer.setEncryption(PdfWriter.

STRENGTH40BITS, "pdf", null,PdfWriter.AllowCopy

);

document.open();

PdfContentByte cb = writer.getDirectContent();

PdfImportedPage page;

 

int

rotation;

 

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.

 

out.println("Processed page "

+ i);

}

document.close();

}

 

catch

( Exception e) {

 

 


--
You received this message because you are subscribed to the Google
Groups "Java EE (J2EE) Programming with Passion!" group.
To post to this group, send email to
java-ee-j2ee-programming-with-passion@googlegroups.com
To unsubscribe from this group, send email to
java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en

Reply via email to