Hi Please find attached Java file and Application generated PDF file.
The java program is able to generate all the
data(fetched from teh DB) in the PDF file but when I
tried to add footer, Its not displaying in the PDF.
mean while I have checked in sever log and the
application is not throwing any error too.
Please have a look to the java file and suggest any
modification if required.
Footer is being displayed inside following method of
TBReportsAction.java file
public void getPDFContent (Element
inputXml,TenderBoxVenderOpeningForm
tbaForm,HttpServletResponse
response,HttpServletRequest request )
{
.....
...
//sample code
doc.open();
HeaderFooter footer = new HeaderFooter(new
Phrase("This is page "),true);
footer.setAlignment(com.lowagie.text.Element.ALIGN_CENTER);
BVLog.error ("*Pankaj:footer"+footer);
doc.setFooter(footer);
doc.add(new Paragraph("Hello world"));
BVLog.error ("*Pankaj:doc"+doc);
} catch(DocumentException de) {
System.err.println("pankaj: Error
message:"+de.getMessage());
}
}.
Thnaks
Pankaj
_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.yahoo.com
ssss.pdf
Description: ssss.pdf
/*
* Created on Sep 24, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
* This class will fetch TenderNo and based upon the tender No. it will access
* all the details from DB and it will create a xml object which will be passes along
with XSL to
* create a PDF document.
*
*
*/
package com.nr.apps.bo.action;
import com.doc.*;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.html.HtmlParser;
import com.nr.apps.bo.form.TenderBoxVenderOpeningForm;
import org.dom4j.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.broadvision.demand.action.login.DemandRegisteredVisitorAction;
import com.broadvision.webapp.visitor.SessionVisitor;
import com.broadvision.util.BVLog;
import com.broadvision.bvjobject.BVWComponentException;
import com.broadvision.components.bv.content.BVI_Content;
import com.broadvision.components.bv.content.BVI_ContentList;
import com.broadvision.components.bv.content.BVI_ContentManager;
/**
* @author pankajsinha
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TBReportsAction extends DemandRegisteredVisitorAction {
BVI_ContentManager cntmgr = new BVI_ContentManager();
BVI_ContentList contentList =null;
BVI_Content bvicontent= null;
public TBReportsAction() {
super();
}
/* (non-Javadoc)
* @see
com.broadvision.demand.action.login.DemandRegisteredVisitorAction#securePerform(org.apache.struts.action.ActionMapping,
org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest,
javax.servlet.http.HttpServletResponse, com.broadvision.webapp.visitor.SessionVisitor)
*/
public ActionForward securePerform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response,
SessionVisitor sessionVisitor) throws IOException,
ServletException {
/*
* GET COMPARATIVE AND TABULAR DATA FROM DB AND CREATE A XML STRUCTURE.
*/
TenderBoxVenderOpeningForm tbaForm=(TenderBoxVenderOpeningForm)form;
Element compStatement= null;
Element tabularStatement= null;
String tenderFor=null;
tenderFor=tbaForm.getTenderFor();
BVLog.error ( "pankaj:TBReportsAction >>TenderFor: "+tenderFor
+"ReportType:"+tbaForm.getReportType()) ;
// For works show comparative statements and for others show tabular
statements.
if(tenderFor.equalsIgnoreCase("works")){
compStatement= getComparativeStatement(tbaForm.getRfqOid());
getPDFContent(compStatement,tbaForm,response,request);
}else{
tabularStatement=
getTabularStatement(tbaForm.getRfqOid(),tbaForm.getReportType());
getPDFContent(tabularStatement,tbaForm,response,request);
}
return null;
}
public Element getComparativeStatement(String tenderNumber)
{
BVLog.error ( "pankaj:TBReportsAction: Inside
getComparativeStatement()") ;
Element compStatement=null;
Element statements=null;
Element statement=null;
Element itemNo=null;
Element description=null;
Element consignee=null;
Element vender=null;
Element unit=null;
Element rate=null;
Element amount=null;
Element tender=null;
int tenderNo=Integer.parseInt(tenderNumber);
try{
BVI_ContentManager cntmgr = new BVI_ContentManager();
BVI_ContentList contentList =null;
BVI_Content bvicontent= null;
/* pass MR_PRICED_ITEMS.hold_reason=1..when u successfully insert
this bit in the opening mode(buyer end)
* at present paasses "0"..just for checking the avaialbility of data.
* */
String query ="SELECT MR_PRICED_ITEMS.oid,
MR_PRICED_ITEMS.item_number,MR_PRICED_ITEMS.supplier_id, MR_PRICED_ITEMS.mr_uom,
MR_PRICED_ITEMS.item_description, MR_PRICED_ITEMS.consignee_name,
MR_PRICED_ITEMS.unit_price, MR_PRICED_ITEMS.installation_amt FROM MR_PRICED_ITEMS,
MR_ORDERS where MR_ORDERS.oid = MR_PRICED_ITEMS.oid AND
MR_PRICED_ITEMS.oid="+tenderNo+"AND MR_PRICED_ITEMS.hold_reason=0";
BVLog.error (query) ;
cntmgr = new BVI_ContentManager();
contentList = cntmgr.contentByQuery(query);
bvicontent= null;
compStatement =DocumentHelper.createElement("compStatements");
statements = DocumentHelper.createElement("statements");
tender =DocumentHelper.createElement("tender");
for(int i=0; i<contentList.getLength();i++){
bvicontent=contentList.get(i);
statement =
DocumentHelper.createElement("statement");
itemNo = DocumentHelper.createElement("itemNo");
description =
DocumentHelper.createElement("description");
consignee =
DocumentHelper.createElement("consignee");
vender = DocumentHelper.createElement("vender");
unit = DocumentHelper.createElement("statements");
rate = DocumentHelper.createElement("unit");
amount = DocumentHelper.createElement("amount");
itemNo.addText(bvicontent.get("item_number").getStringValue()) ;
description.addText(bvicontent.get("item_description").getStringValue()) ;
consignee.addText(bvicontent.get("consignee_name").getStringValue()) ;
vender.addText(bvicontent.get("supplier_id").getStringValue());
unit.addText(bvicontent.get("mr_uom").getStringValue()) ;
rate.addText(bvicontent.get("unit_price").getStringValue()) ;
amount.addText(bvicontent.get("installation_amt").getStringValue()) ;
statement.add(itemNo);
statement.add(description);
statement.add(consignee);
statement.add(vender);
statement.add(unit);
statement.add(rate);
statement.add(amount);
BVLog.error
("pankaj:TBReportsAction:getComparativeStatement()" +statement.asXML());
statements.add(statement);
}
tender.addText(tenderNumber) ;
compStatement.add(tender);
compStatement.add(statements);
}catch(BVWComponentException ex){
BVLog.error ( "TBReportsAction:Exception::" +ex);
}
return compStatement;
}
public Element getTabularStatement (String tenderNumber,String reportType)
{
Element statements=null;
Element tabularStatements=null;
Element tabularStatement=null;
Element tenderNo=null;
Element nameOfTenderers=null;
Element earnestMoney=null;
Element basicRate=null;
Element exciseDuty=null;
Element salesTax=null;
Element packingCharges=null;
Element forwardingCharges=null;
Element FOR =null;
Element freight=null;
Element total=null;
Element deliveryPeriod=null;
Element validity =null;
Element remarks =null;
Element distribution=null;
Element reportsType=null;
Element item=null;
Element consignee=null;
tabularStatements =DocumentHelper.createElement("tabularStatements");
statements = DocumentHelper.createElement("statements");
reportsType = DocumentHelper.createElement("reportsType");
try{
BVLog.error ( "pankaj:TBReportsAction: reportType:>> "
+reportType);
// pass RFQ name from variable in where clause
//String query ="SELECT MR_PRICED_ITEMS.oid,
MR_PRICED_ITEMS.item_number,MR_PRICED_ITEMS.item_name,
MR_PRICED_ITEMS.salex_tax_amt,MR_PRICED_ITEMS.packing,
MR_PRICED_ITEMS.forwarding,MR_PRICED_ITEMS.freight,
MR_PRICED_ITEMS.for_fot,MR_PRICED_ITEMS.basic_rate,
MR_PRICED_ITEMS.consignee_name,MR_PRICED_ITEMS.adjusted_price,
MR_PRICED_ITEMS.supp_resp_note,MR_PRICED_ITEMS.valid_end_date FROM MR_PRICED_ITEMS,
MR_ORDERS WHERE MR_ORDERS.oid=MR_PRICED_ITEMS.oid And
MR_PRICED_ITEMS.oid="+tenderNumber+ "AND MR_PRICED_ITEMS.hold_reason=0";
String query="";
//query for supplier wise, item wise, consignee wise
if(reportType.equalsIgnoreCase("SupplierWise"))
{
query="SELECT
MR_PRICED_ITEMS.consignee_name,MR_PRICED_ITEMS.UNIT_PRICE,MR_PRICED_ITEMS.oid,
MR_PRICED_ITEMS.item_number,MR_PRICED_ITEMS.item_name,
MR_PRICED_ITEMS.salex_tax_amt,MR_PRICED_ITEMS.packing,
MR_PRICED_ITEMS.forwarding,MR_PRICED_ITEMS.freight,
MR_PRICED_ITEMS.for_fot,MR_PRICED_ITEMS.basic_rate,
MR_PRICED_ITEMS.consignee_name,MR_PRICED_ITEMS.adjusted_price,
MR_PRICED_ITEMS.supp_resp_note,MR_PRICED_ITEMS.valid_end_date FROM MR_PRICED_ITEMS,
MR_ORDERS WHERE MR_ORDERS.oid=MR_PRICED_ITEMS.oid AND MR_PRICED_ITEMS.oid=
"+tenderNumber+ "AND MR_PRICED_ITEMS.hold_reason=0 ORDER BY
MR_PRICED_ITEMS.supplier_id ASC, MR_PRICED_ITEMS.item_number ASC,
MR_PRICED_ITEMS.consignee_name ASC, MR_PRICED_ITEMS.UNIT_PRICE DESC";
}else if(reportType.equalsIgnoreCase("ItemWise")){
//query for item wise, consignee wise and supplier wise
query="SELECT
MR_PRICED_ITEMS.consignee_name,MR_PRICED_ITEMS.UNIT_PRICE,MR_PRICED_ITEMS.oid,
MR_PRICED_ITEMS.item_number,MR_PRICED_ITEMS.item_name,
MR_PRICED_ITEMS.salex_tax_amt,MR_PRICED_ITEMS.packing,
MR_PRICED_ITEMS.forwarding,MR_PRICED_ITEMS.freight,
MR_PRICED_ITEMS.for_fot,MR_PRICED_ITEMS.basic_rate,
MR_PRICED_ITEMS.consignee_name,MR_PRICED_ITEMS.adjusted_price,
MR_PRICED_ITEMS.supp_resp_note,MR_PRICED_ITEMS.valid_end_date FROM MR_PRICED_ITEMS,
MR_ORDERS WHERE MR_ORDERS.oid=MR_PRICED_ITEMS.oid AND MR_PRICED_ITEMS.oid=
"+tenderNumber+ "AND MR_PRICED_ITEMS.hold_reason=0 ORDER BY
MR_PRICED_ITEMS.item_number ASC,MR_PRICED_ITEMS.consignee_name ASC,
MR_PRICED_ITEMS.UNIT_PRICE DESC,MR_PRICED_ITEMS.supplier_id ASC";
}else if(reportType.equalsIgnoreCase("ConsigneeWise")){
//consignee wise, item wise, cost wise, supplier wise
query="SELECT
MR_PRICED_ITEMS.consignee_name,MR_PRICED_ITEMS.UNIT_PRICE,MR_PRICED_ITEMS.oid,
MR_PRICED_ITEMS.item_number,MR_PRICED_ITEMS.item_name,
MR_PRICED_ITEMS.salex_tax_amt,MR_PRICED_ITEMS.packing,
MR_PRICED_ITEMS.forwarding,MR_PRICED_ITEMS.freight,
MR_PRICED_ITEMS.for_fot,MR_PRICED_ITEMS.basic_rate,
MR_PRICED_ITEMS.consignee_name,MR_PRICED_ITEMS.adjusted_price,
MR_PRICED_ITEMS.supp_resp_note,MR_PRICED_ITEMS.valid_end_date FROM MR_PRICED_ITEMS,
MR_ORDERS WHERE MR_ORDERS.oid=MR_PRICED_ITEMS.oid AND MR_PRICED_ITEMS.oid=
"+tenderNumber+ "AND MR_PRICED_ITEMS.hold_reason=0 ORDER BY
MR_PRICED_ITEMS.consignee_name ASC,MR_PRICED_ITEMS.item_number ASC,
MR_PRICED_ITEMS.UNIT_PRICE DESC,MR_PRICED_ITEMS.supplier_id ASC";
}
BVLog.error ( "pankaj:TBReportsAction:" +query);
cntmgr = new BVI_ContentManager();
contentList = cntmgr.contentByQuery(query);
BVI_Content bvicontent= null;
for(int i=0; i<contentList.getLength();i++){
bvicontent=contentList.get(i);
tabularStatement =
DocumentHelper.createElement("tabularStatement");
tenderNo = DocumentHelper.createElement("tenderNo");
nameOfTenderers=DocumentHelper.createElement("nameOfTenderers");
earnestMoney = DocumentHelper.createElement("earnestMoney");
basicRate=DocumentHelper.createElement("basicRate");
exciseDuty=DocumentHelper.createElement("exciseDuty");
salesTax=DocumentHelper.createElement("salesTax");
packingCharges =
DocumentHelper.createElement("packingCharges");
forwardingCharges=DocumentHelper.createElement("forwardingCharges");
FOR=DocumentHelper.createElement("FOR");
freight=DocumentHelper.createElement("freight");
total = DocumentHelper.createElement("total");
deliveryPeriod=DocumentHelper.createElement("deliveryPeriod");
validity=DocumentHelper.createElement("validity");
remarks=DocumentHelper.createElement("remarks");
distribution=DocumentHelper.createElement("distribution");
item=DocumentHelper.createElement("item");
consignee=DocumentHelper.createElement("consignee");
//tenderNo
tenderNo.addText(tenderNumber) ;
//nameOfTenderers
nameOfTenderers.addText("") ;
//earnestMoney
earnestMoney.addText("") ;
//basicRate
basicRate.addText(bvicontent.get("basic_rate").getStringValue()) ;
//exciseDuty
exciseDuty.addText("") ;
//salesTax
salesTax.addText(bvicontent.get("salex_tax_amt").getStringValue()) ;
//packingCharges
packingCharges.addText(bvicontent.get("packing").getStringValue()) ;
//forwardingCharges
forwardingCharges.addText(bvicontent.get("forwarding").getStringValue()) ;
//for
FOR.addText(bvicontent.get("for_fot").getStringValue()) ;
//freight
freight.addText(bvicontent.get("freight").getStringValue()) ;
//total
total.addText(bvicontent.get("adjusted_price").getStringValue()) ;
//deliveryPeriod
deliveryPeriod.addText("") ;
String st ="";
// distribution
distribution.addText("") ;
//validity
try
{
st = bvicontent.get("valid_end_date").getStringValue();
}catch(Exception ex)
{
st="";
}
validity.addText(st);
//remarks
remarks.addText(bvicontent.get("supp_resp_note").getStringValue()) ;
item.addText(bvicontent.get("item_name").getStringValue()) ;
consignee.addText(bvicontent.get("consignee_name").getStringValue()) ;
tabularStatement.add(tenderNo);
tabularStatement.add(nameOfTenderers);
tabularStatement.add(earnestMoney);
tabularStatement.add(basicRate);
tabularStatement.add(exciseDuty);
tabularStatement.add(salesTax);
tabularStatement.add(packingCharges);
tabularStatement.add(forwardingCharges);
tabularStatement.add(FOR);
tabularStatement.add(freight);
tabularStatement.add(total);
tabularStatement.add(deliveryPeriod);
tabularStatement.add(validity);
tabularStatement.add(remarks);
tabularStatement.add(distribution);
tabularStatement.add(item);
tabularStatement.add(consignee);
BVLog.error (tabularStatement.asXML()) ;
// Add tabular Statements to its parent element.
tabularStatements.add(tabularStatement);
}
//add tabularStatements to its parent and root element.
statements.add(tabularStatements);
if(!reportType.equalsIgnoreCase(""))
{
BVLog.error ("pankaj::1") ;
statements.add(reportsType.addText(reportType));
}else{
BVLog.error ("pankaj::2") ;
statements.add(reportsType.addText(""));
}
BVLog.error (statements.asXML()) ;
}catch(BVWComponentException ex){
BVLog.error ( "pankaj:TBReportsAction: Exception Tabular
Report" +ex) ;
}
return statements;
}
public void getPDFContent (Element inputXml,TenderBoxVenderOpeningForm
tbaForm,HttpServletResponse response,HttpServletRequest request )
{
try{
String xslPathComp="";
String xmlPath="";
boolean flag=false;
GenerateHDoc obj = new GenerateHDoc();
String path=request.getContextPath();
if(tbaForm.getTenderFor().equalsIgnoreCase("works")){
xslPathComp= new String
("c:/inetpub/bvdocroot/pdfdocs/xsl/CompStatement.xsl");
flag=true;
}else{
xslPathComp= new String
("c:/inetpub/bvdocroot/pdfdocs/xsl/NewTabularStatement.xsl");
}
xmlPath= new String
("c:/inetpub/bvdocroot/pdfdocs/xml/Statements.xml");
BVLog.error ( "***pankaj:**XSL:"+xslPathComp+"XML: "+xmlPath);
java.io.PrintWriter out = new java.io.PrintWriter(new
java.io.FileOutputStream(xmlPath));
out.print(inputXml.asXML());
out.close();
// obj.generatePDF(xmlPath,xslPathComp);
ByteArrayOutputStream
byteArrayOut=obj.mergeXmlXsl(xmlPath,xslPathComp);
ByteArrayOutputStream responseStream=new ByteArrayOutputStream();
com.lowagie.text.Document doc = new Document();
// To create document in landscape mode.
doc.setPageSize(PageSize.A3.rotate());
response.setContentType("application/pdf");
//PankAJ Added for Footer
/*boolean doNotGeneratePageNumberInFooter = false;
BVLog.error ("*Pankaj:111111:Footer:setFooter()");
String xmlPath1= new String ("C:\\BV1TO1_7.1\\Footer.xml");
BVLog.error ("*Pankaj:xmlPath1"+xmlPath1);
String xslPath1= new String ("C:\\BV1TO1_7.1\\Footer.xsl");
BVLog.error ("*Pankaj:xslPath1"+xslPath1);
doc.setFooter(generateFooter(xmlPath1, xslPath1,
doNotGeneratePageNumberInFooter));
doc.open();
BVLog.error ("*Pankaj:222222:Footer:setFooter()Generated");*/
// we define a header and a footer
try{
// we open the document for writing
doc.open();
HeaderFooter footer = new HeaderFooter(new Phrase("This is page
"),true);
footer.setAlignment(com.lowagie.text.Element.ALIGN_CENTER);
BVLog.error ("*Pankaj:footer"+footer);
doc.setFooter(footer);
doc.add(new Paragraph("Hello world"));
BVLog.error ("*Pankaj:doc"+doc);
} catch(DocumentException de) {
System.err.println("pankaj: Error message:"+de.getMessage());
}
// Panjkaj Added for Footer
// create PDF file at specified location.
if(flag){
//FileOutputStream fileOutputStream= new
FileOutputStream("C:\\BV1TO1_7.1\\ComparativeStatement-"+tbaForm.getRfqOid()+".pdf");
FileOutputStream fileOutputStream= new
FileOutputStream("c:/inetpub/bvdocroot/pdfdocs/ComparativeStatement-"+tbaForm.getRfqOid()+".pdf");
com.lowagie.text.pdf.PdfWriter.getInstance(doc, fileOutputStream);
}else{
//FileOutputStream fileOutputStream= new
FileOutputStream("C:\\BV1TO1_7.1\\NewTabularStatement-"+tbaForm.getRfqOid()+".pdf");
FileOutputStream fileOutputStream= new
FileOutputStream("c:/inetpub/bvdocroot/pdfdocs/NewTabularStatement-"+tbaForm.getRfqOid()+".pdf");
com.lowagie.text.pdf.PdfWriter.getInstance(doc,
fileOutputStream);
}
com.lowagie.text.pdf.PdfWriter.getInstance(doc, responseStream);
InputSource iSource = new InputSource(new
ByteArrayInputStream(byteArrayOut.toByteArray()));
HtmlParser.parse(doc, iSource);
doc.close();
responseStream.writeTo(response.getOutputStream());
response.getOutputStream().flush();
BVLog.error ("*Pankaj:: ************Finished");
}catch(Exception e)
{
BVLog.error ( "pankaj: getPDFContent():Exception=" +e) ;
}
}
// FOR Footer
private HeaderFooter generateFooter(String documentXml, String footerXsl,
boolean doNotGeneratePageNumberInFooter) throws
TransformerConfigurationException, TransformerException,
FileNotFoundException,SAXException,ParserConfigurationException,IOException {
BVLog.error ("*Pankaj:333333:Inside generateFooter()");
GenerateHDoc generateHDoc = new GenerateHDoc();
ByteArrayOutputStream oStream = generateHDoc.mergeXmlXsl(documentXml,
footerXsl);
BVLog.error ("*Pankaj:4444:Inside generateFooter()"+oStream);
return getFooter(oStream, doNotGeneratePageNumberInFooter);
}
public HeaderFooter getFooter(ByteArrayOutputStream output,
boolean doNotGeneratePageNumberInFooter) {
BVLog.error ("*Pankaj:555555:Inside getFooter()");
Phrase phrase = new Phrase(output.toString());
BVLog.error ("*Pankaj:666666:Inside phrase()"+phrase);
HeaderFooter footer = new HeaderFooter(phrase,
!doNotGeneratePageNumberInFooter);
BVLog.error ("*Pankaj:7777777:footer:"+footer);
return footer;
}
}
