views:

249

answers:

2

I have a situation where I need to increase the space between a table and the header on a PDF that has already been transformed from an XSL template. I need to insert an address in the newly created space. This part is easy enough and I can do that using a stamper and a new table.

However, I am struggling to find a solution to move the grid down to make the space.

Basically I am using FOP to create the PDF from an XSL template using code similar to the following:

OutputStream out = new java.io.FileOutputStream(pdf);
Driver driver = new Driver();
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsl));
StringReader xmlStream = new StringReader(xmlData);
Source xmlSource = new StreamSource(xmlStream);
Result res = new SAXResult(driver.getContentHandler());
transformer.transform(xmlSource, res);

Is it even possible to access the PDF in a way to add the new space? If so, what are my options? I should mention that I don’t know at the time the transformation is happening that I will need the extra space. I only know I need it once I get a page count of the PDF.

Any help is greatly appreciated!

A: 

It's not possible to add "new space" per se, but it is possible to get the co-ordinates of an object on the page and then re-draw that object somewhere else. Unfortunately there's no quick and easy solution and you will need a third-party SDK to do this.

PDF isn't a word processor format, so it's not possible to simply add a couple of carriage returns, as you might in MS Word.

Try iText, it's written in Java and has a decent amount of functionality for manipulating PDFs.

Rowan
A: 

It seems it is not possible (at least from everything I have tried and read) to move transformed objects around once the PDF has already been generated.

Since I was already using iText and the PdfStamper class I was able to insert a new page and insert a new table with the current address info. I did this with the following code:

//add new page
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(file);
stamper.insertPage(pageNumber,reader.getPageSizeWithRotation(1));

//add new table with data
BaseFont base = BaseFont.createFont(BaseFont.HELVETICA,"",BaseFont.NOT_EMBEDDED);
over.setFontAndSize(base,fontSize);
PdfPTable table = new PdfPTable(1);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.addCell(data);
table.setTotalWidth(150f);
table.writeSelectedRows(0, -1, 73, 650, over);

This is not the answer to my question by a viable solution I thought I would share in case others get hung up on the same issue.

northpole