Saturday, 9 January 2010

more on iText

Have to be able to merge pages - so playing more with iText and the following worked (eventually)

>> require 'rjb'
=> ["RjbConf"]
>> Rjb::load('/var/www/html/renaissance/lib/iText.jar')
=> nil
>> FileOutputStream = Rjb::import('java.io.FileOutputStream')
=> #
>> Color = Rjb::import('java.awt.Color')
=> #
>> Element = Rjb::import('com.itextpdf.text.Element')
=> #
>> Document = Rjb::import('com.itextpdf.text.Document')
=> #
>> Font = Rjb::import('com.itextpdf.text.Font')
=> #
>> FontFactory = Rjb::import('com.itextpdf.text.FontFactory')
=> #
>> PageSize = Rjb::import('com.itextpdf.text.PageSize')
=> #
>> Paragraph = Rjb::import('com.itextpdf.text.Paragraph')
=> #
>> Phrase = Rjb::import('com.itextpdf.text.Phrase')
=> #
>> BaseFont = Rjb::import('com.itextpdf.text.pdf.BaseFont')
=> #
>> ColumnText = Rjb::import('com.itextpdf.text.pdf.ColumnText')
=> #
>> PdfPageEvent = Rjb::import('com.itextpdf.text.pdf.PdfPageEvent')
=> #
>> PdfPCell = Rjb::import('com.itextpdf.text.pdf.PdfPCell')
=> #
>> PdfContentByte = Rjb::import('com.itextpdf.text.pdf.PdfContentByte')
=> #
>> PdfPTable = Rjb::import('com.itextpdf.text.pdf.PdfPTable')
=> #
>> PdfWriter = Rjb::import('com.itextpdf.text.pdf.PdfWriter')
(irb):18: warning: already initialized constant NAME
=> #
>> PdfReader = Rjb::import('com.itextpdf.text.pdf.PdfReader')
=> #
>> pdfTest = PdfReader.new('/tmp/test.pdf')
=> #<#:0x2b0282c08e38>
>> pdfTest2 = PdfReader.new('/tmp/test2.pdf')
=> #<#:0x2b0282c03ff0>
>> document = Document.new
=> #<#:0x2b0282c00f80>

>> pdf_writer = PdfWriter.getInstance(document, FileOutputStream.new("/tmp/merge.pdf"))
=> #<#:0x2b0282bf55b8>
>> document.open
=> nil
>> cb = pdf_writer.getDirectContent()
=> #<#:0x2b0282bef898>
>> document.newPage
=> true
>> page1 = pdf_writer.getImportedPage(pdfTest, 1)
=> #<#:0x2b0282be2b20>
>> cb.addTemplate(page1, 0, 0)
=> nil
>> document.newPage
=> true
>> page2 = pdf_writer.getImportedPage(pdfTest2, 1)
=> #<#:0x2b0282bd7a18>
>> cb.addTemplate(page2, 0, 0)
=> nil
>> document.close
=> nil
>>


NOw have a two page pdf - hurrah (again)

No comments:

Post a Comment