HTML Page breaks

Tags: XHTMLXML Workerpage breakiText 5

Introducing page breaks in HTML.

Files: 
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/27843338/converting-repeater-control-to-pdf-using-itextsharp-every-repeated-contents-shou
 */
package sandbox.xmlworker;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class HtmlPageBreaks {
    public static final String DEST = "results/xmlworker/page_breaks.pdf";
    public static final String HTML = "resources/xml/page_breaks.html";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new HtmlPageBreaks().createPdf(DEST);
    }
 
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML));
        // step 5
        document.close();
    }
}
File nameRaw URLUpdated
HtmlPageBreaks.javaHtmlPageBreaks.java2015-11-08 7:29 pm
Resources: 
<h1>Information about California</h1>
<table style="border: solid 1pt; page-break-after: always" cellspacing="0">
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Name:</td>
<td style="width:240.5pt;border: solid 0.5pt;" colspan="2">CALIFORNIA</td>
<td style="width:114pt;border: solid 0.5pt;">Abbreviation:</td>
<td style="width:37pt;border: solid 0.5pt;">CA</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Capital:</td>
<td style="width:128pt;border: solid 0.5pt;">Sacramento</td>
<td style="width:113pt;border: solid 0.5pt;">Most populous city:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">Los Angeles</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Population:</td>
<td style="width:128pt;border: solid 0.5pt;">36,961,664</td>
<td style="width:113pt;border: solid 0.5pt;">Square miles:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">163,707</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Timezones:</td>
<td style="width:128pt;border: solid 0.5pt;">PT (UTC-8)</td>
<td style="width:113pt;border: solid 0.5pt;">&nbsp;</td>
<td style="width:114pt;border: solid 0.5pt;">Daylight Savings:</td>
<td style="width:37pt;border: solid 0.5pt;">YES</td>
</tr>
 
</table><h1>Information about New York</h1>
<table style="border: solid 1pt;" cellspacing="0">
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Name:</td>
<td style="width:240.5pt;border: solid 0.5pt;" colspan="2">NEW YORK</td>
<td style="width:114pt;border: solid 0.5pt;">Abbreviation:</td>
<td style="width:37pt;border: solid 0.5pt;">NY</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Capital:</td>
<td style="width:128pt;border: solid 0.5pt;">Albany</td>
<td style="width:113pt;border: solid 0.5pt;">Most populous city:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">New York</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Population:</td>
<td style="width:128pt;border: solid 0.5pt;">19,541,453</td>
<td style="width:113pt;border: solid 0.5pt;">Square miles:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">54,475</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Timezones:</td>
<td style="width:128pt;border: solid 0.5pt;">EST (UTC-5)</td>
<td style="width:113pt;border: solid 0.5pt;">&nbsp;</td>
<td style="width:114pt;border: solid 0.5pt;">Daylight Savings:</td>
<td style="width:37pt;border: solid 0.5pt;">YES</td>
</tr>
</table>
 
<h1 style="page-break-before: always">Information about Massachusetts</h1>
<table style="border: solid 1pt;" cellspacing="0">
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Name:</td>
<td style="width:240.5pt;border: solid 0.5pt;" colspan="2">MASSACHUSETTS</td>
<td style="width:114pt;border: solid 0.5pt;">Abbreviation:</td>
<td style="width:37pt;border: solid 0.5pt;">MA</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Capital:</td>
<td style="width:128pt;border: solid 0.5pt;">Boston</td>
<td style="width:113pt;border: solid 0.5pt;">Most populous city:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">Boston</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Population:</td>
<td style="width:128pt;border: solid 0.5pt;">6,593,587</td>
<td style="width:113pt;border: solid 0.5pt;">Square miles:</td>
<td style="width:150.5pt;border: solid 0.5pt;" colspan="2">10,555</td>
</tr>
<tr style="height:14pt">
<td style="width:69pt;border: solid 0.5pt;">Timezones:</td>
<td style="width:128pt;border: solid 0.5pt;">EST (UTC-5)</td>
<td style="width:113pt;border: solid 0.5pt;">&nbsp;</td>
<td style="width:114pt;border: solid 0.5pt;">Daylight Savings:</td>
<td style="width:37pt;border: solid 0.5pt;">YES</td>
</tr>
</table>
File nameRaw URLUpdated
page_breaks.htmlpage_breaks.html2015-11-08 7:30 pm
Results: 
File nameRaw URLUpdated
cmp_page_breaks.pdfcmp_page_breaks.pdf2015-11-08 7:31 pm