Monday 14, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Monday, 5 June 2017

iText 5 - Tiled Background Example

To create PDF file we need iText 5 jar. Download iText Jars from iText Website or Maven Repository

Maven Dependency

2.<dependency>
3.    <groupid>com.itextpdf</groupid>
4.    <artifactid>itextpdf</artifactid>
5.    <version>5.5.11</version>
6.</dependency>

TiledBackground.java
01.package com.javatutorialcorner.itextpdf;
02. 
03.import com.itextpdf.text.Document;
04.import com.itextpdf.text.DocumentException;
05.import com.itextpdf.text.ExceptionConverter;
06.import com.itextpdf.text.Image;
07.import com.itextpdf.text.Rectangle;
08.import com.itextpdf.text.pdf.PdfContentByte;
09.import com.itextpdf.text.pdf.PdfPCell;
10.import com.itextpdf.text.pdf.PdfPCellEvent;
11.import com.itextpdf.text.pdf.PdfPTable;
12.import com.itextpdf.text.pdf.PdfPatternPainter;
13.import com.itextpdf.text.pdf.PdfWriter;
14.  
15. 
16.import java.io.File;
17.import java.io.FileOutputStream;
18.import java.io.IOException;
19. 
20.public class TiledBackground {
21.  
22.    class TiledImageBackground implements PdfPCellEvent {
23.  
24.        protected Image image;
25.  
26.        public TiledImageBackground(Image image) {
27.            this.image = image;
28.        }
29.  
30.        public void cellLayout(PdfPCell cell, Rectangle position,
31.                PdfContentByte[] canvases) {
32.            try {
33.                PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
34.                PdfPatternPainter patternPainter = cb.createPattern(image.getScaledWidth(), image.getScaledHeight());
35.                image.setAbsolutePosition(0, 0);
36.                patternPainter.addImage(image);
37.                cb.saveState();
38.                cb.setPatternFill(patternPainter);
39.                cb.rectangle(position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight());
40.                cb.fill();
41.                cb.restoreState();
42.            } catch (DocumentException e) {
43.                throw new ExceptionConverter(e);
44.            }
45.        }
46.  
47.    }
48.  
49.    public static final String DEST = "C:/JTC/TiledBackground.pdf";
50. 
51.    public static final String IMG1 = "C:/JTC/ALxRF.png";
52.    public static final String IMG2 = "C:/JTC/bulb.gif";
53.  
54.    public static void main(String[] args) throws IOException, DocumentException {
55.        File file = new File(DEST);
56.        file.getParentFile().mkdirs();
57.        new TiledBackground().createPdf(DEST);
58.    }
59.  
60.    public void createPdf(String dest) throws IOException, DocumentException {
61.        Document document = new Document();
62.        PdfWriter.getInstance(document, new FileOutputStream(dest));
63.        document.open();
64.        PdfPTable table = new PdfPTable(2);
65.        PdfPCell cell = new PdfPCell();
66.        Image image = Image.getInstance(IMG1);
67.        cell.setCellEvent(new TiledImageBackground(image));
68.        cell.setFixedHeight(770);
69.        table.addCell(cell);
70.        cell = new PdfPCell();
71.        image = Image.getInstance(IMG2);
72.        cell.setCellEvent(new TiledImageBackground(image));
73.        cell.setFixedHeight(770);
74.        table.addCell(cell);
75.        document.add(table);
76.        document.close();
77.    }
78.}

Output
Reference : iText Website

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: iText 5 - Tiled Background Example Rating: 5 Reviewed By: eHowToNow