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 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.

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
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
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
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
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
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
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
ModuleNotFoundError: No module named 'header-detail-footer'
ModuleNotFoundError: No module named 'header-detail-footer'  Hi...: No module named 'header-detail-footer' How to remove the ModuleNotFoundError: No module named 'header-detail-footer' error? Thanks   Hi
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
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
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
PDF to Image
PDF to Image  Java code to convert PDF to Image
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
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
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
regarding the pdf table using itext
regarding the pdf table using itext  if table exceeds the maximum width of the page how to manage
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
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 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
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
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
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
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 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
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
ModuleNotFoundError: No module named 'pdf-image-to-text'
ModuleNotFoundError: No module named 'pdf-image-to-text'  Hi, My... named 'pdf-image-to-text' How to remove the ModuleNotFoundError: No module named 'pdf-image-to-text' error? Thanks   Hi, In your
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
Rotating image in the pdf file
Rotating image in the pdf file   ... insert a image in a pdf file and rotate it irrespective of the fact whether... will help us to make and use pdf file in our program.ADS_TO_REPLACE_1 Now
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
how to add the two tables in same row when generating pdf file from jsp - JSP-Servlet
how to add the two tables in same row when generating pdf file from jsp  How to add the two tables in same row one is left side and other is right side please help me.   Hi Friend, Try the following code
The type Font is ambiguous where generating pdf file from jsp page - JSP-Servlet
The type Font is ambiguous where generating pdf file from jsp page  I am geeting ambiguous error .Do u want to add any jar files   Hi Friend, Import the class Font into your JSP page like the following
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
how to get the image path when inserting the image into pdf file in jsp - JSP-Servlet
how to get the image path when inserting the image into pdf file in jsp ...")); document.open(); Image image = Image.getInstance("C:/image2.png"); Image image1 = Image.getInstance("C:/node.jpg"); document.add(image); document.add
The type Font is ambiguous where generating pdf file from jsp page - JSP-Servlet
The type Font is ambiguous where generating pdf file from jsp page  i am getting the type font is ambiguous and type imgae ambiguous ? document.open(); 32: Font headFont = FontFactory.getFont(FontFactory.HELVETICA
How to wrap image in the pdf file
How to wrap image in the pdf file   ... insert a image  and wrap it according to your own style in a pdf file...*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our
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
How to change image size before adding to mysql using jsp
How to change image size before adding to mysql using jsp  I want to upload an image to MySQL DB, before inserting the image I want to change the image size as per defined by me and then I want to store that image into my DB
Inserting image in the pdf file
Inserting image in the pdf file   ... insert a image in a pdf file irrespective of the fact whether it exists... to make and use pdf file in our program.ADS_TO_REPLACE_1 Now create a file
How to insert image in PDF file in struts2
How to insert image in PDF file in struts2 In this example, we will see how to insert image in PDF file using struts2.2.1 framework.. index.jsp <... Image in PDF....</a>ADS_TO_REPLACE_2 </body> </html>
itext chunk
itext chunk       In this free tutorial we are going to tell you about chunk in iTextiText is a framework for creating pdf files in java. A Chunk
Examples of iText
.   Inserting image in the pdf file...; How to wrap image in the pdf file... portrait in iTextiText is a framework for creating pdf files
how to get the image path when inserting the image into pdf file in jsp - JSP-Servlet
how to get the image path when inserting the image into pdf file in jsp  Hi Friend, my image path;C:/images/photo.jpg. i am getting the below error error: The type Image is ambiguous document.open(); Image
Open Source PDF
Open Source PDF Open Source PDF Libraries in Java iText is a library that allows you to generate PDF files on the fly...: The look and feel of HTML is browser dependent; with iText and PDF you can
What is iText in Java?
What is iText in Java?  Hi, What is iText in Java? How to create PDF in Java? Thanks   Hi, Check the tutorial: Examples of iText. Thanks
how to set the image and address in single row when genearting pdf fil from jsp - JSP-Servlet
how to set the image and address in single row when genearting pdf fil from jsp  i need to set the image is left side and right side is address when genrating the pdf file from jsp  Hi Friend, Try the following code
Version of itext>itext dependency
List of Version of itext>itext dependency
pdf to text
pdf to text  how to covert pdf file (which contain table and text) into word or excel file using itext api

Ads