Hello! I've been studing the iText library and have successfully created a servlet that outputs the report to a pdf. However, I have not been successful in putting/adding an image or a watermark to the report. I've tried everything I can thing of without luck.
Thank you for your consideration.
How can I do this with what I have?
package tas2;
// import pdf for java package
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import com.lowagie.text.pdf.*;
import java.net.MalformedURLException;
import java.net.URL;
import com.lowagie.text.pdf.PdfContentByte;
import java.awt.Toolkit;
import com.lowagie.text.html.HtmlWriter;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import oracle.jdbc.driver.*;
import java.text.*; //for the DateFormat & DecimalFormat
public class activeRetiredAgentCount extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException
{
Connection con = null;
Statement stmt = null;
try
{
DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection
(tasFormat.DATABASE);
String market = req.getParameter
("market");
String monthYear = req.getParameter
("MonthYear");
String firstDoM = null;
String mmDDyyyy = null;
String sql = null;
String today = null;
// get today
sql = " SELECT " +
"
TO_CHAR( SYSDATE, 'MM/DD/YYYY HH24:MI') " +
" FROM " +
"
DUAL ";
//create scrollable resultset
stmt = con.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//execute query
ResultSet rs = stmt.executeQuery(sql);
// assign the first day of Month to
String
while (rs.next())
{
today = rs.getString( 1 ) ;
}// end while
// get the first day of this month
sql = " SELECT " +
"
TO_CHAR( ( SELECT TO_DATE ( ' " + monthYear + " ' " + ", 'Month
YYYY') FROM DUAL ), 'MM/DD/YYYY') " +
" FROM " +
"
DUAL ";
//create scrollable resultset
stmt = con.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//execute query
rs = stmt.executeQuery(sql);
// assign the first day of Month to
String
while (rs.next())
{
firstDoM = rs.getString( 1 ) ;
}// end while
// get the string mmDDyyyy
sql = " SELECT " +
" TO_CHAR
( ( SELECT TO_DATE ( ' " + monthYear + " ' " + ", 'Month YYYY') FROM
DUAL ), 'MM/DD/YYYY') " +
" FROM " +
" DUAL ";
//create scrollable resultset
stmt = con.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//execute query
rs = stmt.executeQuery(sql);
// assign the first day of Month to
String
while (rs.next())
{
mmDDyyyy = rs.getString( 1 ) ;
}//end while
// Query for Part 1 of report All
Agents who were active for MonthYear
// select all agents and their
corresponding supervisors who have a NCS date
// less than or = to the month and Year
we are looking at AND are active
// UNION SELECT all agents who were
retired for the month year we are
// looking at EXCEPT for those who were
RETIRED on the FIRST day of the
// month.
sql = " SELECT" +
"
INITCAP(agent.last_name || ', ' || agent.first_name) AS AGENTNAME, " +
" INITCAP
(ACD_SPVSR.LAST_NAME || ', ' || ACD_SPVSR.FIRST_NAME) AS SPVSRNAME " +
" FROM " +
"
agent, " +
"
ACD_SPVSR " +
" WHERE " +
"
agent.compy_code = '" + market + "' " +
" AND " +
"
agent.retired = 'N' " +
" AND " +
"
TRUNC(agent.NCS_DATE) <= LAST_DAY(TO_DATE('" + mmDDyyyy + " ' "
+ ", 'mm/dd/yyyy') ) " +
" AND " +
"
agent.SPVSR_USER_NUM = ACD_SPVSR.SPVSR_USER_NUM " +
" UNION
SELECT " +
"
INITCAP(agent_hist.NEW_last_name || ', ' || agent_hist.NEW_first_name)
AS AGENTNAME, " +
" INITCAP
(ACD_SPVSR.LAST_NAME || ', ' || ACD_SPVSR.FIRST_NAME) AS SPVSRNAME " +
" FROM " +
"
agent_hist, " +
"
ACD_SPVSR " +
" WHERE " +
"
agent_hist.new_compy_code = '" + market + "' " +
" AND " +
"
agent_hist.TRAN_FLAG = 'U' " +
" AND " +
"
TRUNC(agent_hist.MOD_DATE) BETWEEN TO_DATE( '" + mmDDyyyy + " ' "
+ ", 'mm/dd/yyyy' ) " +
"AND " +
" LAST_DAY
(TO_DATE('" + mmDDyyyy + " ' " + ", 'mm/dd/yyyy') ) " +
" AND " +
"
agent_hist.MOD_DATE <> TO_DATE( '" + firstDoM + "' "
+ ", 'mm/dd/yyyy' ) " +
" AND " +
"
agent_hist.NEW_RETIRED = 'Y' " +
" AND " +
"
agent_hist.NEW_SPVSR_USER_NUM = ACD_SPVSR.SPVSR_USER_NUM" +
" ORDER BY " +
"
AGENTNAME ";
//create scrollable resultset
stmt = con.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//execute query
rs = stmt.executeQuery(sql);
rs.last();
// Jump to last row
int rsRecordCount = rs.getRow(); //
get the row number (record count)
rs.beforeFirst() ;
// move to first row OR rs.beforeFirst() depending on how you
are processing the data
Font titleFont = FontFactory.getFont(
FontFactory.HELVETICA, 14, Font.BOLD, new Color(0, 124, 191) ) ;
Font recordCountFont =
FontFactory.getFont( FontFactory.HELVETICA, 10, Font.BOLD, new Color(0, 124,
191) ) ;
Font headerFont = FontFactory.getFont(
FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 255, 255) );
Font cellFont = FontFactory.getFont(
FontFactory.HELVETICA, 10, Font.NORMAL, new Color(0, 0, 0) );
Font headerFooterFont =
FontFactory.getFont( FontFactory.HELVETICA, 8, Font.NORMAL, new Color(0, 0,
0) );
// step 1 - will be a pdf doc
Document document = new Document();
// set content type as pdf and get
PdfWriter
res.setContentType("application/pdf");
PdfWriter writer = PdfWriter.getInstance
(document, res.getOutputStream());
HeaderFooter header = new HeaderFooter
(new Phrase("TAS Mania Report - Generated "+ today, headerFooterFont ), false);
header.setAlignment
(Element.ALIGN_CENTER);
header.setBorder(Rectangle.BOTTOM);
//header.setBorderColor( 255, 213,
154 ); // orange color
document.setHeader(header);
// we add a Footer that will show up on
PAGE 1
HeaderFooter footer = new HeaderFooter
(new Phrase("Page: ", headerFooterFont ), true);
footer.setAlignment
(Element.ALIGN_CENTER);
footer.setBorder(Rectangle.TOP);
//footer.setBorderColor( 255, 213,
154 ); //light orange color
// 255, 128, 0 dark orange
color!
document.setFooter(footer);
/*// we add a Watermark that will show
up on PAGE 1
try {
Watermark watermark = new
Watermark(Image.getInstance("watermark.jpg"), 200, 420);
document.add(watermark);
}
catch(Exception e) {
System.err.println("Are you
sure you have the file 'watermark.jpg' in the right path?");
}
*/
// opent the document for writing
document.open
();
Paragraph pReportTitle = new Paragraph
("Active Agent Count\n" + "Market Code: " + market + "\n" + "Active During: " +
monthYear, titleFont);
document.add(pReportTitle);
//document.add(jpeg);
//document.addImage(image);
// step 4: we create a table and add it
to the document
PdfPTable aTable = new PdfPTable(2);
// 2 = # columns in this table
aTable.getDefaultCell().setBorderWidth(1);
aTable.getDefaultCell
().setHorizontalAlignment(Element.ALIGN_LEFT);
PdfPCell cell = new PdfPCell(new Phrase
( "Agent Name", headerFont ) );
cell.setBackgroundColor(new Color(0,
124, 191));
aTable.addCell(cell);
cell = new PdfPCell(new Phrase
( "Supervisor Name", headerFont ) );
cell.setBackgroundColor(new Color(0,
124, 191));
aTable.addCell(cell);
aTable.setHeaderRows(1); // this is
the end of the table header
// load records into page table for
part 1 Active Agents
while (rs.next())
{
cell = new PdfPCell(new Phrase
( rs.getString(1), cellFont ) );
cell.setBackgroundColor(new
Color(253, 254, 237)); // lgt yellow
aTable.getDefaultCell
().setBorderWidth(1);
aTable.getDefaultCell
().setBorderColor(new Color(150,150, 150)); // lgt. gray
aTable.addCell(cell);
cell = new PdfPCell(new Phrase
( rs.getString(2), cellFont ) );
cell.setBackgroundColor(new
Color(253, 254, 237)); // lgt yellow
aTable.getDefaultCell
().setBorderWidth(1);
aTable.getDefaultCell
().setBorderColor(new Color(150,150, 150)); // lgt. gray
aTable.addCell(cell);
}//end while
// add recordcount
Paragraph pRecordCount = new Paragraph
("Active Agent Count: " + rsRecordCount + "\n" , recordCountFont);
document.add(pRecordCount);
// add the table to the document
document.add(aTable);
// we trigger a page break
document.newPage();
// reset variable
rsRecordCount = 0;
// Part 2 of Report:
// Query for Part 2 of report All
Agents who were Retired for MonthYear
sql = " SELECT " +
" INITCAP(agent_hist.NEW_last_name || ', ' ||
agent_hist.NEW_first_name) AS AGENTNAME, " +
" INITCAP(ACD_SPVSR.LAST_NAME || ', ' || ACD_SPVSR.FIRST_NAME) AS
SPVSRNAME " +
" FROM " +
" agent_hist, " +
" ACD_SPVSR " +
" WHERE " +
" agent_hist.new_compy_code = '" + market + "' " +
" AND " +
" agent_hist.TRAN_FLAG = 'U' " +
" AND " +
" TRUNC(agent_hist.MOD_DATE) BETWEEN TO_DATE( '" +
mmDDyyyy + " ' " + ", 'mm/dd/yyyy' ) " +
"AND " +
" LAST_DAY(TO_DATE('" + mmDDyyyy + " ' " + ", 'mm/dd/yyyy') ) " +
" AND " +
" agent_hist.NEW_RETIRED = 'Y' " +
" AND " +
" agent_hist.NEW_SPVSR_USER_NUM = ACD_SPVSR.SPVSR_USER_NUM " +
" ORDER BY " +
" AGENTNAME " ;
//create scrollable resultset
stmt = con.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//execute query
rs = stmt.executeQuery(sql);
rs.last();
// Jump to last row
rsRecordCount = rs.getRow();
// get the row number (record count)
rs.beforeFirst();
// move to first row OR rs.beforeFirst() depending on
how you are processing the data
pReportTitle = new Paragraph
("Retired Agent Count\n" + "Market Code: " + market + "\n" + "Retired During: "
+ monthYear, titleFont);
document.add(pReportTitle);
//create a new 2nd table
aTable = new PdfPTable(2);
// 2 = # columns in this table
aTable.getDefaultCell
().setBorderWidth(1);
aTable.getDefaultCell
().setHorizontalAlignment(Element.ALIGN_LEFT);
cell = new PdfPCell(new Phrase
( "Agent Name", headerFont ) );
cell.setBackgroundColor(new
Color(0, 124, 191));
aTable.addCell(cell);
cell = new PdfPCell(new Phrase
( "Supervisor Name", headerFont ) );
cell.setBackgroundColor(new
Color(0, 124, 191));
aTable.addCell(cell);
aTable.setHeaderRows(1); //
this is the end of the table header
// load records into page table
for part 2 Retired Agents
while (rs.next())
{
cell = new PdfPCell(new
Phrase( rs.getString(1), cellFont ) );
cell.setBackgroundColor
(new Color(253, 254, 237));
// lgt yellow
aTable.getDefaultCell
().setBorderWidth(1);
aTable.getDefaultCell
().setBorderColor(new Color(150,150, 150)); // lgt. gray
aTable.addCell(cell);
cell = new PdfPCell(new
Phrase( rs.getString(2), cellFont ) );
cell.setBackgroundColor
(new Color(253, 254, 237));
// lgt yellow
aTable.getDefaultCell
().setBorderWidth(1);
aTable.getDefaultCell
().setBorderColor(new Color(150,150, 150)); // lgt. gray
aTable.addCell(cell);
}//end while
// add recordcount
pRecordCount = new Paragraph
("Retired Agent Count: " + rsRecordCount + "\n" , recordCountFont);
document.add(pRecordCount);
// add the table to the document
document.add(aTable);
// close the document
document.close();
} // end of Servlet try
catch(NullPointerException ne)
{
PrintWriter pw = res.getWriter
();
pw.println ("Your session has
timed out. <a href='http://" + tasFormat.serverURL() + "/index.html'>Click Here
to Login again</a>");
pw.close();
}// end catch
catch(Exception e)
{
PrintWriter pw = res.getWriter
();
pw.println (tasFormat.errorStatement(e) );
pw.close();
}// end catch
finally
{
if (con != null)
{
try
{
stmt.close();
con.close();
}//end try
catch (Exception ignored)
{
// do nothing
}// end catch
}//end if
}//end finally
} //end of doPost
} //end of servlet class
-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
digital self defense, top technical experts, no vendor pitches,
unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions
