Java Code Examples for com.itextpdf.text.pdf.PdfWriter#setPageEvent()

The following examples show how to use com.itextpdf.text.pdf.PdfWriter#setPageEvent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColorParagraphBackground.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testParagraphBackgroundEventListener() throws DocumentException, FileNotFoundException
{
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "document-with-paragraph-backgrounds.pdf")));
    ParagraphBackground border = new ParagraphBackground();
    writer.setPageEvent(border);
    document.open();
    document.add(new Paragraph("Hello,"));
    document.add(new Paragraph("In this document, we'll add several paragraphs that will trigger page events. As long as the event isn't activated, nothing special happens, but let's make the event active and see what happens:"));
    border.setActive(true);
    document.add(new Paragraph("This paragraph now has a background. Isn't that fantastic? By changing the event, we can even draw a border, change the line width of the border and many other things. Now let's deactivate the event."));
    border.setActive(false);
    document.add(new Paragraph("This paragraph no longer has a background."));
    document.close();
}
 
Example 2
Source File: DynamicFooter.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/43610868/how-to-add-dynamic-variable-to-footer-without-calling-document-newpage-in-itex">
 * How to add dynamic variable to footer without calling document.newPage() in iText 5
 * </a>
 * <p>
 * generator method of the OP
 * </p>
 * @see #testDynamicFooterLikeAyZagen()
 */
public static void createPdf(ArrayList<String> htmlStrings, FooterTable footerEvt, String destinationPath)
        throws IOException, DocumentException {
    Document document = new Document(PageSize.A4);
    document.setMargins(68, 85, 75, 85);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destinationPath));
    if (footerEvt != null)
        writer.setPageEvent(footerEvt);
    document.open();

    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper
            .getCSS(new ByteArrayInputStream(/*readCSS("resources/content.min.css").getBytes()*/ "".getBytes()));
    cssResolver.addCss(cssFile);

    XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    fontProvider.register(/*"resources/ARIAL.TTF"*/ "c:/Windows/Fonts/arial.ttf");

    CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    int i = 0;
    for (String htmlfile : htmlStrings) {
        i++;
        footerEvt.setTitleIndex("" + i);//or FooterTable.setTitleIndex("" + i);
        ByteArrayInputStream stream = new ByteArrayInputStream(htmlfile.getBytes("UTF-8"));
        p.parse(stream, Charset.forName("UTF-8"));
    }
    document.close();
}
 
Example 3
Source File: PdfReportBuilder.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
public Document createDocument(ReportTitle reportTitle, OutputStream out) throws Exception {
	Document doc = new Document();
	PdfWriter writer = PdfWriter.getInstance(doc, out);
	if (reportTitle.isShowPageNo()) {
		PdfReportPageNumber event = new PdfReportPageNumber(chineseFont);
		writer.setPageEvent(event);
	}
	doc.open();
	Paragraph paragraph = this.createReportTitle(reportTitle);
	doc.add(paragraph);
	return doc;
}