Quantcast
Channel: Web Dynpro Java
Viewing all articles
Browse latest Browse all 45

How to export table data to PDF file

$
0
0

Hi All,

 

Introduction

 

This tutorial how to implement the download table data into a PDF format by using Web Dynpro Java application:

 

I achieve by using IText Jar file, which you can find in the below link.

 

http://www.java2s.com/Code/Jar/i/Downloaditext13jar.htm

 

1. You can create an external library DC and put this JAR in it and create public parts out of it. Use these public parts in your web dynpro DC to convert the context node data into a PDF file.

 

 

2. My Example Code:-

 

 

import com.lowagie.text.Document;

import com.lowagie.text.Font;

import com.lowagie.text.PageSize;

import com.lowagie.text.Phrase;

import com.lowagie.text.pdf.PdfPTable;

import com.lowagie.text.pdf.PdfWriter;

 

 

public void DownloadtoPDF( java.lang.String tableName, com.sap.tc.webdynpro.progmodel.api.IWDView currentView, java.lang.String fileName, com.sap.tc.webdynpro.progmodel.api.IWDNode dataSource )

  {

    //@@begin DownloadtoPDF()

    try

  {

  if(!dataSource.isEmpty())

  {

  String column_type = "";

  String column_visib = "";

  String tableData="";

  int curr_ld = dataSource.getLeadSelection();

      //Collecting the Node attributes name of the Table

  ColumnInfo = new ArrayList();

  ArrayList cellEditors = new ArrayList();

  ArrayList columnpros = new ArrayList();

  ArrayList used_column_ID = new ArrayList();

 

  IWDTable tableTobeExportedToPDF = (IWDTable)currentView.getElement(tableName);

  IWDAbstractTableColumn tableColumns[] = tableTobeExportedToPDF.getGroupedColumns();

 

  ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();

 

  Document document = new Document();

  PdfWriter writer = PdfWriter.getInstance(document, arrayOutputStream);

 

  PdfPTable pdfTable = new PdfPTable(tableColumns.length);

 

  document.clearTextWrap();

  document.setPageSize(PageSize.B4);

  document.open();

 

  pdfTable.setWidthPercentage(100);

  pdfTable.setHeaderRows(1);

 

  Phrase myPhrase[]  = new Phrase[tableColumns.length];

 

  int tableColum_Size =tableColumns.length;

  for(int HeadCol=0;HeadCol<tableColum_Size;HeadCol++)

  {

  IWDTableColumn column = (IWDTableColumn)currentView.getElement(tableColumns[HeadCol].getId());

  column_type = column.getTableCellEditor().getClass().getName();

  column_type = column_type.substring(column_type.lastIndexOf(".")+1,column_type.length());

 

  if(column.getVisible() == WDVisibility.VISIBLE)

  {

  if((column_type.equalsIgnoreCase("TextView") || column_type.equalsIgnoreCase("InputField")) || column_type.equalsIgnoreCase("LinkToAction"))

  {

  used_column_ID.add(used_column_ID.size(),column.getId());

  cellEditors.add(cellEditors.size(),column.getTableCellEditor().getId());

  columnpros.add(columnpros.size(),column_type);

  myPhrase[HeadCol] = new Phrase(tableColumns[HeadCol].getHeader().getText(), new Font(Font.getStyleValue(AxisUtilConstant.FONT_STYLE), 8, Font.BOLD));

  pdfTable.addCell(myPhrase[HeadCol]);

  }

  }

  }

 

  Phrase dataPhrase[][]  = new Phrase[dataSource.size()][tableColumns.length];

 

  for(int Row=0;Row<dataSource.size();Row++)

  {

  dataSource.setLeadSelection(Row);

  for(int Col=0;Col<used_column_ID.size();Col++)

  {

  IWDTableColumn column = (IWDTableColumn)currentView.getElement(used_column_ID.get(Col).toString());

  if(column.getVisible() == WDVisibility.VISIBLE)

  {

  if((columnpros.get(Col).equals("TextView"))||(columnpros.get(Col).equals("InputField"))||(columnpros.get(Col).equals("LinkToAction")))

  {

  IWDTextView txtview = (IWDTextView)currentView.getElement(cellEditors.get(Col).toString());

  dataPhrase[Row][Col] = new Phrase(txtview.getText(), new Font(Font.getStyleValue(AxisUtilConstant.FONT_STYLE), 8, Font.NORMAL));

  pdfTable.addCell(dataPhrase[Row][Col]);

 

  }

  }

  }

  }

 

  document.add(pdfTable);

  document.close();

 

       //writing the collected string in to ByteArrayOutputStream tabledata_byte=arrayOutputStream.toByteArray();

       IWDResource Resource= WDResourceFactory.createResource(new ByteArrayInputStream(tabledata_byte),fileName,WDWebResourceType.PDF,true);

       wdComponentAPI.getWindowManager().createNonModalExternalWindow(Resource.getUrl(WDFileDownloadBehaviour.ALLOW_SAVE.ordinal()),                   Resource.getResourceName()).show();

  }

  else

  {

       msgManager.reportException("The Table is empty",true);

  }

  }

  catch (Exception e)

  {

       msgManager.reportException("Exception:"+e,true);

  }

    //@@end

  }

 

 

I hope this can help.

 

Regards,

Durga Rao.


Viewing all articles
Browse latest Browse all 45

Latest Images

Trending Articles





Latest Images