Created
April 27, 2012 05:43
-
-
Save rirakkumya/2506245 to your computer and use it in GitHub Desktop.
iTextとJFreeChartとscala-ioでpdfにグラフを出力しちゃうコード(日本語対応)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libraryDependencies += "com.github.scala-incubator.io" %% "scala-io-core" % "0.3.0" | |
libraryDependencies += "com.github.scala-incubator.io" %% "scala-io-file" % "0.3.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalax.io._ | |
import scalax.file.Path | |
import Path.string2path | |
import java.awt.{Font => jaFont} | |
import java.awt.geom.Rectangle2D | |
import com.itextpdf.text.Font | |
import com.itextpdf.text.Document | |
import com.itextpdf.text.Paragraph | |
import com.itextpdf.text.PageSize | |
import com.itextpdf.text.pdf.PdfPTable | |
import com.itextpdf.text.pdf.PdfPCell | |
import com.itextpdf.text.pdf.PdfWriter | |
import com.itextpdf.text.pdf.BaseFont | |
import com.itextpdf.awt.PdfGraphics2D | |
import com.itextpdf.awt.DefaultFontMapper | |
import com.itextpdf.awt.FontMapper | |
import org.jfree.chart.ChartFactory | |
import org.jfree.chart.StandardChartTheme | |
import org.jfree.chart.title.TextTitle | |
import org.jfree.chart.plot.PlotOrientation | |
import org.jfree.chart.JFreeChart | |
import org.jfree.data.category.DefaultCategoryDataset | |
object PdfWriterOpt { | |
implicit def str2Elem(s:String)(implicit font:Font) = new Paragraph(s,font) | |
def A4PdfWriter(outputFileName:String)(f:(Font) => PdfWriter => Document => Unit) { | |
val defaultFont = new Font( | |
BaseFont.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-H",BaseFont.NOT_EMBEDDED),15) | |
outputFileName outputStream() foreach {stream => | |
val doc = new Document(PageSize.A4,50,50,50,50) | |
val writer = PdfWriter.getInstance(doc,stream) | |
doc.open | |
f(defaultFont)(writer)(doc) | |
doc.close | |
} | |
} | |
case class JapaneseFontMapper(bfont:BaseFont) extends FontMapper { | |
def awtToPdf(font:jaFont) = bfont | |
def pdfToAwt(font:BaseFont, size:Int) = new jaFont("dialog",jaFont.PLAIN,size) | |
} | |
def GraphWriter(size:(Float,Float) = (400,200),position:(Float,Float) = (0,0))( | |
f: => JFreeChart)(implicit writer:PdfWriter) = { | |
val (width,height) = size | |
val cb = writer.getDirectContent() | |
val template = cb.createTemplate(width,height) | |
val defaultFont = BaseFont.createFont( | |
"HeiseiKakuGo-W5", "UniJIS-UCS2-H", BaseFont.NOT_EMBEDDED); | |
val g = new PdfGraphics2D( | |
template, width, height, JapaneseFontMapper(defaultFont)); | |
val rect = new Rectangle2D.Double(0,0,width,height) | |
f.draw(g,rect) | |
g.dispose() | |
cb.addTemplate(template, position._1, position._2) | |
} | |
def addEmptyLines(lines:Int)(implicit doc:Document,font:Font) = | |
for(_ <- (1 to lines)) doc.add(" ") | |
} | |
object Itext extends App { | |
import PdfWriterOpt._ | |
A4PdfWriter( | |
//"../itextView/public/images/sample.pdf" | |
"sample.pdf" | |
){implicit font => implicit writer => implicit doc => | |
doc.add("itext + JFreeChartのsample") | |
GraphWriter((500,220),(50,510)){ | |
val current = Array( | |
215.3,152.8,129.9,119.2,133.1,156.4,176.2,171.2,246.8,165.1,233.7,256.8) | |
val last = Array( | |
244.4,167.5,129.1,115.7,120.9,165.6,223.8,175.3,222.1,159.3,223.4,249.4) | |
val label = (1 to 12) map ("%02d".format(_)) | |
val ds = new DefaultCategoryDataset() | |
(current zip label) map {case (num,label) => ds.addValue(num,"今年の雨量",label)} | |
(last zip label) map {case (num,label) => ds.addValue(num,"去年の雨量",label)} | |
ChartFactory.createBarChart( | |
"雨量グラフ", "2012年", "雨量", ds, PlotOrientation.VERTICAL, true, true, false) | |
} | |
addEmptyLines(13) | |
doc.add("表示位置決めるのが面倒") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://sourceforge.net/projects/jfreechart/files/latest/download?source=files
http://sourceforge.net/projects/jfreechart/files/3.%20JCommon/1.0.17/jcommon-1.0.17.zip/download
http://sourceforge.net/projects/itext/files/iText/iText5.2.1/itext-5.2.1.zip/download
http://sourceforge.net/projects/itext/files/extrajars/extrajars-2.2.zip/download
をダウンロードして、"itext-asian.jar", "itextpdf-5.2.1.jar","jfreechart-1.0.14.jar","jcommon-1.0.17.jar"
を取り出し"lib/"に突っ込む
"sbt run"でcurrentにsample.pdfが作成される