java - IText : Add an image on a header with an absolute position -


i want place header on each page of pdf. text part of header done can't find way place image.

public static class header extends pdfpageeventhelper {                  public void onendpage(pdfwriter writer, document document) {         try{             pdfcontentbyte cb = writer.getdirectcontent();                    /*               code place text in header             */              image imgsoc = image.getinstance("c:\\...\\logo.jpg");             imgsoc.scaletofit(110,110);             imgsoc.setabsoluteposition(390, 720);              columntext ct = new columntext(cb);             ct.addtext(new chunk(imgsoc,0,0));             ct.go();          }catch(exception e){             e.printstacktrace();         }     } } 

i'm not sure i'm doing right way.

there 2 answers using tables.

tables can helpful create dynamic layout of different header parts (document title, document version, page number, logo, ...).

but if don't need that, have in place op has, can add image @ fixed position fixed size:

public static class header extends pdfpageeventhelper {                  public void onendpage(pdfwriter writer, document document) {         try         {             pdfcontentbyte cb = writer.getdirectcontent();                    /*               code place text in header             */              image imgsoc = image.getinstance("c:\\...\\logo.jpg");             imgsoc.scaletofit(110,110);             imgsoc.setabsoluteposition(390, 720);              cb.addimage(imgsoc);         }         catch(exception e)         {             e.printstacktrace();         }     } } 

ps: if use same logo on pages, had better read image file image instance once (e.g. in constructor or onopendocument), hold instance in variable , re-use again , again. way include image data once in pdf.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -