About us | News | Contact
iText ® Licenses Support   
You are here: Home > Forums > iText and iTextSharp Support > Creating PDF > Low level PDF creation > Direct content
User login
  • Request new password

Borders not set

Submitted by ironstar on Wed, 03/02/2011 - 04:21

I'm trying to create a simple report in a servlet with a header and footer that are created by page events. I'd like to enclose the rectangle that makes up the header and footer in a box border but the border does not appear. I tried to set a background color for the rectangle but that doesn't appear either. Here's the Java code from my first attempt using this API:

public class InvSummaryReportPDFAction extends PicsAction
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
String methodName = "InvSummaryReportPDFAction.execute";
HttpSession session = request.getSession();
ArrayList invList =PicsCacheManager.getListInvCache(session.getId());
SessionValsWrapper sessionValsWrapper = getSessionValsWrapper(request);
String reportType = sessionValsWrapper.getReportType();

if (Logger.getInstance().isInfoOn())
{
Logger.getInstance().logMessage(methodName,
"There are/is " + (!PicsUtilities.isEmpty(invList) ? invList.size() : "0") + " invoice(s) in this report.",
Logger.LOG_INFO);
}

try
{
//Document document = new Document(PageSize.A4);
Document document = new Document(PageSize.A4, 36f, 36f, 72f, 72f);

System.out.println("Doc Left margin= " + document.leftMargin());
System.out.println("Doc Right margin= " + document.rightMargin());
System.out.println("Doc Top margin= " + document.topMargin());
System.out.println("Doc Bottom margin= " + document.bottomMargin());
System.out.println("Doc Left= " + document.left());
System.out.println("Doc Right= " + document.right());
System.out.println("Doc Top= " + document.top());
System.out.println("Doc Bottom= " + document.bottom());

try
{
response.setContentType("application/pdf");
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());

PicsConfigurator config = PicsConfigurator.getInstance();
String companyName = config.getParamValue(PicsConstants.COMP_NAME);
String reportTitle = reportType + " Invoice Summary Report";
PicsDateFormat df = new PicsDateFormat();
String reportDate = df.toMMDDYYYY();

Phrase[] headerText = new Phrase[3];
headerText[0] = new Phrase(companyName);
headerText[1] = new Phrase(reportTitle);
headerText[2] = new Phrase(reportDate);

Phrase footerText = new Phrase("Company Confidential");
InvPdfPageEventHelper invPdfEvt = new InvPdfPageEventHelper(headerText, footerText);
writer.setPageEvent(invPdfEvt);
document.open();

if (PicsUtilities.isEmpty(invList))
{
document.add(new Paragraph("No invoices found that match your search criteria."));
}
else
{
for(InvBean invBean : invList)
{
document.add(new Paragraph("Found Invoice: " + invBean.getInvHeaderBean().getInvNumber()));
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
document.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
class InvPdfPageEventHelper extends PdfPageEventHelper
{
private int pageNumber;
private Phrase[] header = new Phrase[3];
private Phrase footerMsg;

public InvPdfPageEventHelper(Phrase[] headerText, Phrase footerText)
{
header = headerText;
footerMsg = footerText;
}

public void onOpenDocument(PdfWriter writer, Document document)
{
pageNumber = 0;
}
public void onStartPage(PdfWriter writer, Document document)
{
pageNumber++;
}
public void onEndPage(PdfWriter writer, Document document)
{
// This rectangle represents the header portion of the page
float docllx = document.left() + 5;
float doclly = document.top() + 20;
float docurx = document.right() - 24;
float docury = document.top() + 60;

Rectangle rect = new Rectangle(docllx, doclly, docurx, docury);
rect.setBackgroundColor(BaseColor.RED);
rect.setBorderWidth(30f);
rect.setBorder(Rectangle.BOX);

System.out.println("Header left= "+ rect.getLeft());
System.out.println("Header right= "+ rect.getRight());
System.out.println("Header top= "+ rect.getTop());
System.out.println("Header bottom= "+ rect.getBottom());
System.out.println("Header border= " +rect.getBorder());
System.out.println("Header has borders= " + rect.hasBorders());
System.out.println("Header border width= " + rect.getBorderWidth());

ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, header[0], rect.getWidth()/2,
rect.getTop(), 0);

ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, header[1], rect.getWidth()/2,
rect.getTop()-15f, 0);

ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, header[2], rect.getWidth()/2,
rect.getTop()-30f, 0);

// This rectangle represents the footer portion of the page
Rectangle footer = new Rectangle(docllx, 5, docurx, 72);
footer.setBackgroundColor(BaseColor.RED);
footer.setBorder(Rectangle.BOX);

System.out.println("Footer left= "+ footer.getLeft());
System.out.println("Footer right= "+ footer.getRight());
System.out.println("Footer top= "+ footer.getTop());
System.out.println("Footer bottom= "+ footer.getBottom());
System.out.println("Footer border= " +footer.getBorder());
System.out.println("Footer has borders= " + footer.hasBorders());

ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, footerMsg, footer.getWidth()/2,
footer.getTop()-10f, 0);

Phrase pageNumPhrase = new Phrase("Page Number: " + pageNumber);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, pageNumPhrase, footer.getRight()-72f,
footer.getTop()-10f, 0);

}
}
}

The debugging output from this class is:

Doc Left margin= 36.0
Doc Right margin= 36.0
Doc Top margin= 72.0
Doc Bottom margin= 72.0
Doc Left= 36.0
Doc Right= 559.0
Doc Top= 770.0
Doc Bottom= 72.0
Header left= 41.0
Header right= 535.0
Header top= 830.0
Header bottom= 790.0
Header border= 15
Header has borders= true
Header border width= 30.0
Footer left= 41.0
Footer right= 535.0
Footer top= 72.0
Footer bottom= 5.0
Footer border= 15
Footer has borders= false
(This is for the second page of the report)
Header left= 41.0
Header right= 535.0
Header top= 830.0
Header bottom= 790.0
Header border= 15
Header has borders= true
Header border width= 30.0
Footer left= 41.0
Footer right= 535.0
Footer top= 72.0
Footer bottom= 5.0
Footer border= 15
Footer has borders= false

Can anyone see if I'm doing anything wrong? I'm using iText 5 under Windows 7 and Java 1.6.

Thanks in advance for your help...

Wrong measurements when printed ›
  • Login to post comments

Where are you adding the rectangles?

Submitted by Bruno Lowagie on Wed, 03/02/2011 - 09:50.

Hello,
pasting code that can't be executed as a standalone example is somewhat problematic, but I've taken a quick look at it, and I see that you create Rectangle instances rect and footer, but I don't see you adding these objects to the content anywhere.

I think you're forgetting:
PdfContentByte content = writer.getDirectContentUnder();
content.rectangle(rect);
content.rectangle(footer);

See table 14.4 in the book.

Also: why are you using ColumnText? Wouldn't it be easier to create a PdfPTable with the appropriate alignment, borders, background colors, etc...?
You can use writeSelectedRows() to add such a table at the right coordinates.

  • Login to post comments
Content © 2010 1T3XT BVBA