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