Somebody uses the BIRT API to generate a REport [message #709651] |
Thu, 04 August 2011 02:18  |
Eclipse User |
|
|
|
Hello Eclipse Community
I am looking for somebody who has some experience using the BIRT API and can help me.
I want to create a printable TOC with maintopic and subtopic like this
1. maintopic
1.2 subtopic
The API provides a lot of Objects to create a TOC but I just don´t get it how they are working together.
Another possibility to get a TOC is to use bookmnarks and to add them children this works fine on iText but in BIRT there is no way (so far as I know) to add children to a bookmark.
I posted this problem in other Forums too but no answer I am hopeing here is someone who knows about BIRT.
Is there any tutorial of how to use the API?
Thanks a lot
Dude
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Somebody uses the BIRT API to generate a REport [message #716229 is a reply to message #716136] |
Tue, 16 August 2011 13:47   |
Eclipse User |
|
|
|
Attached is the modified class. It handles recursive toc entries now
and applies a little styling.
We are looking into adding something like this in the pdf emitter. Do
you see a need for a toc component for other output format?
Jason
On 8/16/2011 10:19 AM, Dude wrote:
> Okay thanks for the informations i hope that this feature will be
> implemented.
> Autimatic generation of a TOC as an option would be very nice i guess
> lot of people would appreciate it Can you post the link for your blog
> for the or send me your modified code?
>
> Dude
>
>
package REAPI;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.IRenderTask;
import org.eclipse.birt.report.engine.api.IReportDocument;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import com.lowagie.text.Chunk;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfArray;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.SimpleBookmark;
import com.lowagie.text.pdf.SimpleNamedDestination;
import com.lowagie.text.pdf.draw.DottedLineSeparator;
public class GenerateFirstPageTocPDF {
private final static int tocperpage = 40;
private int toccounter=0;
private int pagecnt=1;
private PdfStamper stamper = null;
private ColumnText column = null;
private PdfPTable table = null;
private HashMap bookmarkPages = null;
private Rectangle rect = null;
private Chunk separator = null;
private Color tocColor = new Color(245,245,245);
public void insertPages(List<HashMap<String, Object>> list){
for (HashMap<String, Object> bookmark : list) {
this.toccounter++;
if( this.toccounter > this.tocperpage){
this.pagecnt++;
this.stamper.insertPage(this.pagecnt, this.rect);
this.toccounter=0;
}
List<HashMap<String, Object>> clist = (List) bookmark.get("Kids");
if(clist != null){
insertPages(clist);
}
}
}
public void handleEntries( List<HashMap<String, Object>> list, int level){
PdfPCell cell = null;
Chunk link;
PdfAction action;
for (HashMap<String, Object> bookmark : list) {
link = new Chunk((String)bookmark.get("Title"));
action =PdfAction.gotoLocalPage((String) bookmark.get("Title"), false);
link.setAction(action);
String bookmarkString = (String) bookmarkPages.get((String) bookmark.get("Title"));
String[] pga =bookmarkString.split(" "); //pga[0] contains page number
Phrase ph = new Phrase(" ");
ph.setFont( new Font(Font.TIMES_ROMAN, 8));
ph.add(new Chunk(link));
cell = new PdfPCell(ph);
cell.setBorder(PdfPCell.NO_BORDER);
cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
cell.setBackgroundColor(this.tocColor);
cell.setIndent(level*4);
this.table.addCell(cell);
cell = new PdfPCell(ph);
cell.setBorder(PdfPCell.NO_BORDER);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
cell.setBackgroundColor(this.tocColor);
//cell.setBorderColor(Color.BLACK);
//cell.setBorderWidthBottom(1);
cell.addElement(this.separator);
this.table.addCell(cell);
ph = new Phrase(pga[0]);
ph.setFont( new Font(Font.TIMES_ROMAN, 8));
cell = new PdfPCell(ph);
cell.setBorder(PdfPCell.NO_BORDER);
cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
cell.setBackgroundColor(this.tocColor);
Color cc = new Color(level, level, level, level);
//cell.setBorderColor(Color.BLACK);
//cell.setBorderWidthBottom(1);
this.table.addCell(cell);
this.toccounter++;
if( this.toccounter > this.tocperpage){
this.pagecnt = this.pagecnt+1;
this.column.addElement(table);
try {
this.column.go();
} catch (DocumentException e) {
e.printStackTrace();
}
this.column = new ColumnText(this.stamper.getOverContent(this.pagecnt));
this.column.setSimpleColumn(rect.getLeft(36), rect.getBottom(36),
this.rect.getRight(36), this.rect.getTop(36));
float[] colsWidth = {0.5f, 2f, 0.2f};
this.table = new PdfPTable(colsWidth);
this.table.setWidthPercentage(85);
this.toccounter=0;
}
List<HashMap<String, Object>> clist = (List) bookmark.get("Kids");
if( clist != null){
level++;
handleEntries( clist, level);
level--;
}
}
}
public void createTOC(ByteArrayOutputStream baos, String outpath){
try {
PdfReader reader = new PdfReader(baos.toByteArray());
PdfDictionary pdfd = reader.getCatalog();
this.rect = reader.getPageSizeWithRotation(1);
this.stamper = new PdfStamper(reader, new FileOutputStream(outpath));
stamper.setViewerPreferences(PdfWriter.PageModeUseOutlines);
stamper.insertPage(1, rect);
PdfOutline pdfo = stamper.getWriter().getRootOutline();
this.column = new ColumnText(stamper.getOverContent(1));
this.column.setSimpleColumn(this.rect.getLeft(36), this.rect.getBottom(36),
this.rect.getRight(36), this.rect.getTop(36));
this.separator = new Chunk(new DottedLineSeparator());
PdfPTable header = new PdfPTable(1);
Phrase ph = new Phrase("Table Of Contents");
ph.setFont( new Font(Font.TIMES_ROMAN, 8));
PdfPCell cell = new PdfPCell(ph);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setBackgroundColor(this.tocColor);
cell.setBorder(PdfPCell.NO_BORDER);
header.addCell(cell);
header.setWidthPercentage(85);
this.column.addElement(header);
List<HashMap<String, Object>> list = SimpleBookmark.getBookmark(reader);
this.toccounter=0;
this.pagecnt=1;
insertPages(list);
this.toccounter=0;
this.pagecnt=1;
//put this before insertPage to get the orignal page count
//using it here gets the updated bookmark page numbers
this.bookmarkPages = SimpleNamedDestination.getNamedDestination(reader, false);
SimpleBookmark.shiftPageNumbers(list, reader.getNumberOfPages(), null);
float[] colsWidth = {0.5f, 2f, 0.2f};
this.table = new PdfPTable(colsWidth);
this.table.setWidthPercentage(85);
if( list != null){
handleEntries( list,0);
}
this.column.addElement(table);
this.column.go();
this.stamper.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
public void runReport() throws EngineException
{
IReportEngine engine=null;
EngineConfig config = null;
try{
config = new EngineConfig( );
config.setBIRTHome("C:\\birt\\birt-runtime-2_6_1\\birt-runtime-2_6_1\\ReportEngine");
config.setLogConfig(null, Level.OFF);
Platform.startup( config );
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
engine = factory.createReportEngine( config );
}catch( Exception ex){
ex.printStackTrace();
}
IReportRunnable design, design2 = null;
//Open the report design
design = engine.openReportDesign("Reports/TOC.rptdesign");
//Create task to run and render the report,
IRunTask task = engine.createRunTask(design);
task.run("output/resample/toc.rptdocument");
task.close();
IReportDocument document = null;
//Open the report design
document = engine.openReportDocument("output/resample/toc.rptdocument");
IRenderTask rtask = engine.createRenderTask(document);
PDFRenderOption options = new PDFRenderOption();
ByteArrayOutputStream fso= new ByteArrayOutputStream();
options.setOutputStream(fso);
//options.setOutputFileName("c:/test5/jj.pdf");
options.setOutputFormat("pdf");
rtask.setRenderOption(options);
rtask.render();
rtask.close();
createTOC(fso, "c:/test/final.pdf");
//createToc(fso, "c:/test5/what2.pdf");
engine.destroy();
Platform.shutdown();
System.out.println("Finished");
}
/**
* @param args
*/
public static void main(String[] args) {
try
{
GenerateFirstPageTocPDF ex = new GenerateFirstPageTocPDF( );
ex.runReport();
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
|
|
|
|
|
|
|
|
|
|
Re: Somebody uses the BIRT API to generate a REport [message #768972 is a reply to message #713175] |
Wed, 21 December 2011 02:06  |
Eclipse User |
|
|
|
Hi Dude,
Need some help, in this thread, you have stated that you faced this error :
Stacktrace:
java.lang.NullPointerException
at org.eclipse.birt.report.engine.api.impl.ReportEngine$EngineExtensionManager.<init>(ReportEngine.java:804)
And you were later able to fix it.Can you please let me know how you fixed this issue.
Thanks,
Amar.
|
|
|
Powered by
FUDForum. Page generated in 0.55219 seconds