Shashi Kanth Karnati
Adding Image to Header and footer at generating Itext PDF
4 Answer(s)      3 years and 11 months ago
Posted in : Java Beginners

i want to add Image to Header , am generating the PDF letter am able to add the text as showing below :

 HeaderFooter header = new HeaderFooter(new Phrase("This is page: ", new Font(bf_courier)), true);

 header.setAlignment(Element.ALIGN_CENTER);
 document.setHeader(header);

   HeaderFooter footer = new HeaderFooter(new Phrase(
    "This is page: ", new Font(bf_courier)), true);

   footer.setBorder(Rectangle.NO_BORDER);
    footer.setAlignment(Element.ALIGN_CENTER);
    document.setFooter(footer);

But i need to add the Image at header.

Advertisement
View Answers

August 3, 2011 at 4:38 PM


import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class CreatePDF{
    public static void main(String arg[])throws Exception{
      try{
                Document document=new Document();
                FileOutputStream fos=new FileOutputStream("C:/header-footer.pdf");
                PdfWriter writer = PdfWriter.getInstance(document, fos);
                document.open();
                Image image1 = Image.getInstance("C:/image1.jpg");
                Image image2 = Image.getInstance("C:/image2.jpg");

                image1.setAbsolutePosition(0, 0);
                image2.setAbsolutePosition(0, 0);

                PdfContentByte byte1 = writer.getDirectContent();
                PdfTemplate tp1 = byte1.createTemplate(600, 150);
                tp1.addImage(image2);

                PdfContentByte byte2 = writer.getDirectContent();
                PdfTemplate tp2 = byte2.createTemplate(600, 150);
                tp2.addImage(image1);

                byte1.addTemplate(tp1, 0, 715);
                byte2.addTemplate(tp2, 0, 0);

                Phrase phrase1 = new Phrase(byte1 + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 7, Font.NORMAL));
                Phrase phrase2 = new Phrase(byte2 + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 7, Font.NORMAL));

                HeaderFooter header = new HeaderFooter(phrase1, true);
                HeaderFooter footer = new HeaderFooter(phrase2, true);
                document.setHeader(header);
                document.setFooter(footer);
                document.close();
                System.out.println("File is created successfully showing header and footer.");
                }
                catch (Exception ex){
                    System.out.println(ex);

                }
            }
        }


August 4, 2011 at 5:56 PM


Hi I got with below code ,

thank you for the same. Code is :

Image logo = Image.getInstance("/image.gif");
logo.setAlignment(Image.MIDDLE);
logo.scaleAbsoluteHeight(20);
logo.scaleAbsoluteWidth(20);
logo.scalePercent(100);
Chunk chunk = new Chunk(logo, 0, -45);
HeaderFooter header = new HeaderFooter(new Phrase(chunk), false);
header.setAlignment(Element.ALIGN_CENTER);
header.setBorder(Rectangle.NO_BORDER);
document.setHeader(header);


July 11, 2012 at 9:01 PM


Hi, I have a similar question related to HeaderFooter class, I want to display three Phrases on Footer of the Page. First Phrase should be aligned left,Second phrase on the right and Third phrase in the middle. How can we acheive this? is it possible ? If so Please suggest me.



July 12, 2012 at 11:33 AM


HI, Here go through the below one.

This might be help you. or else you create different para's and then add with different alignments.

Font footFont = new Font(Font.TIMES_ROMAN, 8, Font.NORMAL); // Font declaration

Paragraph footerLine = new Paragraph("---------------", footBold); // Bottom Line

footerLine.setAlignment(Element.ALIGN_CENTER); // ALIGN CENTER
Paragraph footerPara = new Paragraph("Company Name", footBold);
footerPara.setAlignment(Element.ALIGN_LEFT); //ALIGN LEFT

Paragraph footerHeadOfficeAdd = new Paragraph("Divisional Office: XYZ divistion,Mumbai, Tel:", footFont); // Phrase 1

Paragraph footerRegisteredAdd = new Paragraph( "Registered Office: XYZ Building,Dadar(W), Mumbai-400 028. ", footFont);

footerRegisterOfficeAdd.setAlignment(Element.ALIGN_LEFT);

Paragraph footPara = new Paragraph();

footPara.add( footerLine ); // Bottom Line

footPara.add( footerPara );  // Company Name

footPara.add( footerRegisteredAdd ); // Office Address

footPara.add( footerHeadOfficeAdd ); // Head office address

HeaderFooter footer = new HeaderFooter(new phrase (footPara), false);

footer.setBorder(Rectangle.NO_BORDER);

footer.setAlignment(Element.ALIGN_LEFT);

document.setHeader(header);










Related Tutorials/Questions & Answers:
Adding Image to Header and footer at generating Itext PDF
Adding Image to Header and footer at generating Itext PDF  i want to add Image to Header , am generating the PDF letter am able to add the text...); document.setFooter(footer); But i need to add the Image at header
Adding images in itext pdf
Adding images in itext pdf  Hi, How to add image in pdf file using itext? Thanks   Hi, You can use following code... image in the pdf file. Thanks
Advertisements
generating itext pdf from java application - Java Beginners
generating itext pdf from java application  hi, Is there any method in page events of itext to remove page numbers from pdf generated frm.../java/itext/index.shtml
Using HeaderFooter class of iText API , Can we display multiple phrases in the footer section of PDF ,each with different alignment.
Using HeaderFooter class of iText API , Can we display multiple phrases in the footer section of PDF ,each with different alignment.  Hi, I have... on Footer of the Page. First Phrase should be aligned left,Second phrase
itext pdf
itext pdf  i am generating pdf using java i want to do alignment in pdf using java but i found mostly left and right alignment in a row. i want to divide single row in 4 parts then how can i do
alignment for header and footer
alignment for header and footer  header and footer alignment is not fit like footer is not fit in bottom some white space is there in jsf, what to do
How to set the pdfptable as pdf page header when generating the pdf from jsp page. - JSP-Servlet
How to set the pdfptable as pdf page header when generating the pdf from jsp page.  how to set the pdfptable column headers as a header of the documet.i need to display the column headers for every pdf page if pdf documt is 20
How to set iText pdf document background image - Java Beginners
How to set iText pdf document background image  Hi, I have created the pdf document using the iText classes. but i need to set the background image for this pdf document. there is way to set the background color, but i
itext pdf - Java Interview Questions
itext pdf  sample program to modify the dimensions of image file in itext in java  HiIf you want to know deep knowledge then click here and get more information about itext pdf program.http://www.roseindia.net/java
Generating pdf in Spring
Generating pdf in Spring  Sir/Madam, I need your help in generating a pdf by fetching the data form database and by using unicode in spring framework
How to access the image file from project folder itself while writing to pdf using itext?
How to access the image file from project folder itself while writing to pdf using itext?  I am writing a pdf using itext where i add a image in the beginning of the document. When i use image = Image.getInstance("C
Read Header/Footer Margin size using Java
Read Header/Footer Margin size using Java  Hi, Is it possible to read the "Header from Top" and "Footer from bottom" properties of a MS word document in Java? I have been searching the web for hours now, POI's and several other
Read Header/Footer Margin size using Java
Read Header/Footer Margin size using Java  Hi, Is it possible to read the "Header from Top" and "Footer from bottom" properties of a MS word document in Java? I have been searching the web for hours now, POI's and several other
Generating PDF reports - JSP-Servlet
Generating PDF reports  Hello everyone i have submitted several... Question I am try to generate a pdf report using jsp .... i want to export values stored in sql server DB on to a pdf files so please do reply its very
filling pdf by itext
filling pdf by itext  I read the pdf that contain of text field and I want to fill this field in the same pdf file that it oppened? what the sutible..., DocumentException { // read an existing pdf file PdfReader reader1 = new PdfReader
make header and footer and attache it with excel sheet.
 make header and footer and attache it with excel sheet... header and footer and attaché it with excel sheet.  Code description ...; class is used to create the header and footer. We create the object of header
PDF to Image
PDF to Image  Java code to convert PDF to Image
regarding the pdf table using itext
regarding the pdf table using itext  if table exceeds the maximum width of the page how to manage
Displaying html code in pdf with itext - Java Beginners
in the database i need to display this data in the pdf . i am using itext to create the pdf . i am using HTMLWorker to convert the html data . but styles...Displaying html code in pdf with itext   Hi , I am
How to set the border in header and footer from login page using swings/awt
How to set the border in header and footer from login page using swings/awt  Hi, How to set the border in header and footer from login page using swings/awt
How to put the image in footer in login form ?
How to put the image in footer in login form ?  Hi, how to put the image in footer in login form ?   Here is an example that shows image on footer along with Login Form in Java Swing. import javax.swing.*; import
How to insert a single page PDF file into another multipage PDF file using iText? - Java Beginners
How to insert a single page PDF file into another multipage PDF file using iText?  Hi ALL: I have two PDF files. PDF-A.pdf is a multipage PDF file. PDF-B.pdf is a single page PDF file. I want to insert PDF-B into PDF-A only
Image display in pdf
Image display in pdf  i am trying to display a image in pdf using xsl fo but " [ERROR] Error while creating area : Error with image URL: images... go through the following link: Display image in pdf
how to add the scrollbar to the pdf page when generating the pdf file from jsp - JSP-Servlet
how to add the scrollbar to the pdf page when generating the pdf file from jsp  I am not able to see all the columns when i generated the pdf file from jsp.i have 12 colums so how to add the scrollbar
Add Content of an iText PDF File using java code
Add Content of an iText PDF File using java code  ... to generate pdf file dynamically. The iText library can be used to generate PDF... facility to create pdf file from java application. iText is a freely available Java
Struts PDF Generating Example
Struts PDF Generating Example To generate a PDF in struts you need to use...;/param> </result> An example of PDF Generating is given below...;head> <title>PDF Generating Example</title> </head>
How I can filling pdf file that crated by livecycle using itext
How I can filling pdf file that crated by livecycle using itext  I have pdf file that already done by livecycle.. I read it then I used the pdfstamber class to manipolute and filling file.... the result it will be a new pdf file
pdf image extractor - Development process
pdf image extractor  hi deepak , can u pls tell me how to extract a text from a image in a pdf in java. for example bank stmt. i want to extract text from thet image which is in pdf. pls help me. Regards
adding background image - Java Beginners
adding background image  how do i add background image to this code: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class sampleProg extends JFrame { JButton button = new JButton ("Result
How to change the dropdown values based on other dropdown in itext pdf or in any other?
How to change the dropdown values based on other dropdown in itext pdf or in any other?  How to change the drop down values based on another drop... one state that state related postal code only come in editable pdf form
Adding image to database through jsp or HTML page ,while adding only image should show.
Adding image to database through jsp or HTML page ,while adding only image should show.   Adding image to database through jsp or HTML page,while adding only image should show. After that i need view that uploaded image from