Adding Image to Header and footer at generating Itext PDF

Ads
 

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);

Ads









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
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
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
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
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
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
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
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
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>
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
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
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 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
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 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 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 <...; <body> <a href="addImage.action">Add Image in PDF
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
Version of itext>itext dependency
List of Version of itext>itext dependency
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
pdf to text
pdf to text  how to covert pdf file (which contain table and text) into word or excel file using itext api
Creating files of PDF's thumnails
server. You can then use following code to create PDF thumbnail image. <?php $im = new imagick('myfile.pdf'); $im->setImageFormat( "jpg" ); header...Creating files of PDF's thumnails  Hi, How to create PDF's thumbnail
page break using itext in java column wise
page break using itext in java column wise  I have a itext report which will displays n number of idcodes which will display from db.But in the pdf... in column wise in pdf. mainTab = new PdfPTable(prolength

Ads