Monthly Archives: March 2010

Adding text to PDF after JFreeChrart – iText

I heard about a problem of adding text to the PDF after the JFreeChart, using iText.  It’s really a puzzled question?

Generally we will use the method “document.add(txtElement)” to add the text into PDF. It is clear that we are adding the text as an element. Suppose if you add five elements to the PDF, it will add them in the order from the top. But here you have not added the JFreeChart as an Element. It’s a Pdf Template adding to the PDF document as a Pdf content. iText will render the Pdf Content from the bottom left corner. You have to position it in the document to the exact place you want. So, your “document.add(txtElement)” wont work here.

Hence, You have to add the text too as a PdfContent. Here is the code to solve the problem,

PdfContentByte txtContent = writer.getDirectContent();
txtContent.beginText();

BaseFont font = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);

String text = "This is the text I am going to add to the pdf at the bottom of the JFreeChart".

int xPos = 10;    //To Changed according to your need
int yPos = 100;  // To Changed according to your need

txtContent.setFontAndSize(font,10);

txtContent.showTextAligned(0, text, xPos, yPos, 0f);

txtContent.endText();

I hope the above code will solve your problem.

How to code OpenGL programs on Ubuntu?

After searching for a long time, I’ve finally found a solution on how to use Code::Blocks to work with OpenGL APIs. 

Code::Blocks is an IDE just like Visual Studio on Windows. Install Code::blocks from the Ubuntu Repository by typing this at the terminal.

sudo apt-get install codeblocks

Once you’re done, you need to download files that support OpenGL. You need to install the following package for it to work.
freeglut3 & freeglut3-dev – For GLUT Library. It installs some files into your /usr/include/GL/ folder.
libsdl1.2-dev – For SDL Library. It installs some files into your /usr/include/SDL/ folder.
To install these type the following at the terminal.

sudo apt-get install freeglut3

sudo apt-get install freeglut3-dev 

sudo apt-get install libsdl1.2-dev

Once you’re done with this library installation, you should tell the Code::Blocks to link to the correct library for it to be able to execute GLUT/SDL programs. This is how you do it.
1. Open Code:Blocks (You can find this in Applications -> Programming -> Code::Blocks or type codeblocks at your terminal)
2. Go to Settings -> Compiler and Debugger.
3. Open Linker Settings tab. And under “Link Libraries” add “glut” for GLUT, “SDL“, “GLU” and “GL” for SDL. 
4. Click OK and you’re now done with it. Now your CodeBlocks should be able to compile OpenGL – GLUT/SDL code. Compile a running code to check.
If it didn’t work then please comment on this post so that i can help you. 
PS: The whole procedure is to install both GLUT and SDL Libraries. But it is always any one, either GLUT or SDL, that is used. So if you want to install any one of them then skip the instructions that are for the other one. 

Adding Jfreechart to PDF using iText

JFreechart and IText are two most popular third-party sources. Now I am going to tell you how these two technologies work together to draw charts directly on to the PDF. It will really generate a fantastic output which will really impress your boss. Before we are going to know how to do it, here is a small introduction to these two technologies.

JFreechart:

JFreeChart is a free java chart library. Which will helps you to create several types of charts with ease. Here is a list of charts can be created using JFreeChart.

  • Pie Chart (2D, 3D)
  • Ring Chart
  • Bar Chart (2D, 3D, Horizontal and Vertical bars)
  • Stacked Bar Chart (2D, 3D, Horizontal and Vertical bars)
  • Line Charts (With and Without Points)
  • Scatter Plots
  • Step Charts
  • Area Charts
  • Web Charts (With and Without Fill)
  • Time Series Charts
  • Gantt Charts
  • Candlestick Plots
  • Thermometers
  • Dials  and many more…..

iText:

iText is a free and open source library for creating PDF files in Java. Using iText you can add text, images, charts and so on. iText provides support for most advanced features such as PKI-based signatures, 40-bit and 128-bit encryption, Color Correction..etc

Here is how to add a JFreeChart to PDF,

As JFreeChart draws charts using the methods defined by Graphics 2D class it can generate the output to any target that provides a Graphics 2D implementation. As iText also using Graphics2D to write the content to PDF, it is easy to write a JFreeChart on to PDF.

Creating a JFreeChart:

public JFreeChart getPieChart()
{
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("One", new Double(43.2));
dataset.setValue("Two", new Double(10.0));
dataset.setValue("Three", new Double(27.5));
dataset.setValue("Four", new Double(17.5));
dataset.setValue("Five", new Double(11.0));
dataset.setValue("Six", new Double(19.4));
JFreeChart chart = ChartFactory.createPieChart("Pie Chart", dataset, true, true, false);
return chart;
}

The below code will explain you how to add the chart to PDF using iText,

public void addChartoPDF(File file, JFreeChart chart, int width, int height)
{
OutputStream os = new BufferedOutputSteam(new FileOutputSteam(file));
Rectangle pageSize = new Rectangle(width,height);
Document doc = new Document(paperSize, 25, 25, 50,50);
try
{
PdfWriter writer = PdfWriter.getInstance(doc,os);
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate temp = contentByte.createTemplate(width,height);
Graphics2D g2d = temp.createGraphics(width,height, new DefaultFontMapper());
Rectangle2D r2d = new Rectangle2D.Double(0,0,width,height);
chart.draw(g2d,r2d);
g2d.dispose();
contentByte.addTemplate(temp,0f,0f);
}
catch(Exception e)
{
e.printStackTrace();
}
document.close();
}
public static void main(String args[])
{
JFreePdf jpdf = new JFreePdf();
JFreeChart chart = jpdf.getPieChart();
File file = new File("C:/JFree.pdf");
jpdf.addChartoPDF(file, chart,800, 600);
}