Skip to content

Instantly share code, notes, and snippets.

@thegrubbsian
Created January 14, 2013 01:29
Show Gist options
  • Save thegrubbsian/4527171 to your computer and use it in GitHub Desktop.
Save thegrubbsian/4527171 to your computer and use it in GitHub Desktop.
Sample using iText via JRuby to render a PDF with layers.
require "java"
Dir["#{File.dirname(__FILE__)}/itext-5.3.5/\*.jar"].each { |jar| require jar }
import com.itextpdf.text.BaseColor
import com.itextpdf.text.Document
import com.itextpdf.text.Paragraph
import com.itextpdf.text.Element
import com.itextpdf.text.pdf.ColumnText
import com.itextpdf.text.pdf.PdfWriter
import com.itextpdf.text.pdf.PdfContentByte
import com.itextpdf.text.pdf.PdfLayer
import java.io.FileOutputStream
document = Document.new
paragraph_1 = Paragraph.new "This is Layer 1"
paragraph_2 = Paragraph.new "This is Layer 2"
writer = PdfWriter.getInstance(document, FileOutputStream.new("pdf_demo.pdf"))
writer.setPdfVersion(PdfWriter::VERSION_1_5)
document.open
cb = writer.getDirectContent
layer_1 = PdfLayer.new "Layer 1", writer
cb.beginLayer layer_1
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, paragraph_1, 50, 775, 0)
cb.endLayer
layer_2 = PdfLayer.new "Layer 2", writer
cb.beginLayer layer_2
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, paragraph_2, 100, 800, 0)
cb.endLayer
document.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment