0

I am new to iText pdf library. Can any one guide me how to add background color to entire page (not for chunk or paragraph) using iText pdf in java.

Jens
  • 67,715
  • 15
  • 98
  • 113
Arun
  • 15
  • 1
  • 7

2 Answers2

1

This code will do the job:

Rectangle pageSize = new Rectangle(216, 720);
pageSize.setBackgroundColor(new BaseColor(0xFF, 0xFF, 0xDE));
Document document = new Document(pageSize);
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Thank you so much.But error shows as color can not converted to base color at line " pageSize.setBackgroundColor(new java.awt.Color(0xFF, 0xFF, 0xDE)); " – Arun May 13 '15 at 05:25
  • @Arunjk wich version of Itext do you use? have you imported the correct objects? – Jens May 13 '15 at 05:31
  • ooops my mistake. i did not imported. Could you please tell me how to add background color to alternate pages? I tried but couldn't get through. Thanks in advance. – Arun May 13 '15 at 05:57
  • @Arunjk Sorry can't get you. Do you want to have e.g. first page yellow. second page red and so on? – Jens May 13 '15 at 05:59
  • I want only first two pages in blue color and rest of pages in white color. – Arun May 13 '15 at 06:05
  • 1
    In this case you have to use PageEvents. Look [here](http://what-when-how.com/itext-5/adding-page-events-to-pdfwriter-itext-5/) – Jens May 13 '15 at 06:17
0

Use this code:

Rectangle pageSize = new Rectangle(400, 720); 
pageSize.setBackgroundColor(BaseColor.YELLOW);
Document document = new Document(pageSize);
Dada
  • 6,313
  • 7
  • 24
  • 43
Krishna
  • 1
  • 3