Monday 14 January 2013

Print HTML with Header & Footer on Each Page

Method 1: Add a page break at the bottom of each page

Layout onePageOfLayout = new VLayout(){{
            :
            : 
           final ListGrid contentGrid;
           contentGrid= new ListGrid () {{
              setDataSource(reportDataSource);
              setHeight("65%");
              setPrintWrapCells(false);
              setPrintAutoFit(false);

              setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
              ListGridHelper.setDataSourceFields(this,
                   lineItemColumns
                   );
         }};


            setPadding(0);

            //Layout's addMember(...) function's argument can be
            // Label, DynamicForm, ListGrid
            addMember(headerForm);
            addMember(ContentForm);
            addMember(footerForm);
           
            if(isNotLastPage){
                HTMLPane pageBreakElement = new HTMLPane(){{
                    setLayoutAlign(VerticalAlignment.BOTTOM);
                }};
                pageBreakElement.setContents("<div style='page-break-after:always'></div>");
                addMember(pageBreakElement);
            }

           
        }};


Method 2: Print documents in PDF format (1)

import java.io.FileOutputStream;
import java.util.Date;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
:
: 

private static String FILE = "c:\example1.pdf";
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMA
Document document = new Document();
      PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open(); 
// add a paragraph to the document
Paragraph para = new Paragraph();
para .add(new Paragraph( 
        "This paragraph describes something which is very important ",
        new Font(Font.FontFamily.TIMES_ROMAN, 16,
                 Font.BOLD))); 
:
: 
document.add(para);
// Start a new page
document.newPage();
 
document.close();
 

Method 2: Print documents in PDF format (2)

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;
import java.util.TreeSet;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
:
 
public void createPdf(String filename)
        throws IOException, DocumentException, SQLException {

        // step 1
        Document document = new Document(PageSize.A4, 36, 36, 54, 36);
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, 
                                                             new FileOutputStream("example2.pdf"));
        TableHeader event = new TableHeader();
        writer.setPageEvent(event);
        // step 3 - fill in the document
        document.open();

        event.setHeader("Hello!");
        document.add(new Paragraph("Testing."));
        document.newPage();
        event.setHeader("There!");
        document.add(new Paragraph("Testing."));

        document.close();
    } 
Line Spacing
Paragraph objects knows how to add line spacing if the added text exceeds the right edge of the document. Line spacing is measured in user units. There are 72 units per inch. The default spacing is 1.5 times the font height. You can change the line spacing by passing spacing as a parameter to the Paragraph constructor, like this:
    Paragraph paragraph = new Paragraph(50);

Spacing Before and After Paragraph
You can also set the spacing before and after a paragraph. Here is how you do that in code:
   paragraph.setSpacingAfter(50); 
   paragraph.setSpacingBefore(50);

Alignment
You can set the alignment of the paragraph using the setAlignment() method. This sets what side of the page the text is aligned to. Here is an example:
    paragraph.setAlignment(Element.ALIGN_LEFT);   
    paragraph.setAlignment(Element.ALIGN_CENTER); 
    paragraph.setAlignment(Element.ALIGN_RIGHT);

Indentation
You can set the left and right indentation of the paragraph. This moves the paragraph content further away from the edges of the page. Here is the code to do that:
    paragraph.setIndentationLeft(50); 
    paragraph.setIndentationRight(50);

Method 3: HTML Print Header & Footer

<div class="divFooter">UNCLASSIFIED</div>
CSS:
<style type="text/css">
    @media screen {
        div.divFooter {
            display: none;
        }
    }
    @media print {
        div.divFooter {
            position: fixed;
            bottom: 0;
        }
    }
</style>
 
----------------------------------------------------------
 
<span class="printspan">UNCLASSIFIED</span>

And in your CSS, do something like this:
<style type="text/css" media="screen"> .printspan { display: none; } </style> <style type="text/css" media="print"> .printspan { display: inline; font-family: Arial, sans-serif; font-size: 16 pt; color: red; } </style>
 
---------------------------------------------------------- 
 
Use page breaks to define the styles in CSS:
@media all
  {
  #page-one, .footer, .page-break { display:none; }
  }

@media print
  {
  #page-one, .footer, .page-break   
    { 
    display: block;
    color:red; 
    font-family:Arial; 
    font-size: 16px; 
    text-transform: uppercase; 
    }
  .page-break
    {
    page-break-before:always;
    } 
}
Then add the markup in the document at the appropriate places:
<h2 id="page-one">unclassified</h2>
<!-- content block -->
<h2 class="footer">unclassified</h2>
<h2 class="page-break">unclassified</h2>
<!-- content block -->
<h2 class="footer">unclassified</h2>
<h2 class="page-break">unclassified</h2>
<!-- content block -->
<h2 class="footer">unclassified</h2>
<h2 class="page-break">unclassified</h2>
<!-- content block -->
<h2 class="footer">unclassified</h2>
<h2 class="page-break">unclassified</h2>
 

No comments:

Post a Comment