java - itext : merge pdfs and add stationary on each page -


i merge multiples pdf new pdf , add stationery on each page.

for used pdfwriter :

public class fusionpdf extends pdfpageeventhelper {  private document document = null; private pdfwriter writer = null; private pdfreader markreader = null; private pdfimportedpage fonddepage = null; private string textfili = null; private string textmark = null; private static final font font1 = fontfactory.getfont(fontfactory.helvetica, 50, font.normal, new graycolor(0.7f)); private static final font font2 = fontfactory.getfont(fontfactory.helvetica, 6, font.normal, basecolor.black);  /**  * liste des fichiers � concatener  */ private list<file> pdfsoriginaux = new arraylist<file>();  /**  *   * @param pdfdestination fichier de pdf de destination  * @param pdfsoriginaux fichiers pdf � concatener  * @param pdftatoueur fond de page (peut �tre null)  * @param textfiligrane filigran (peut �tre null)  * @throws ioexception   * @throws documentexception   * @throws exception  */  public fusionpdf(file pdfdestination, list<file> pdfsoriginaux, file pdftatoueur, string textfiligrane, string textmarquage) throws documentexception, ioexception  {     if (pdfsoriginaux.isempty()){         throw new ioexception("aucun document pdf traiter !");     }     this.pdfsoriginaux = pdfsoriginaux;     this.init(pdfdestination, pdftatoueur, textfiligrane, textmarquage); }  /**  *   * @param pdfdestination fichier de pdf de destination  * @param pdforiginal fichier pdf traiter  * @param pdftatoueur fond de page (peut etre null)  * @param textfiligrane filigrane (peut etre null)  * @param textmarquage note technique en haut à droite en petit (peut etre null)  * @throws ioexception  * @throws documentexception  * @throws exception  */  public fusionpdf(file pdfdestination, file pdforiginal, file pdftatoueur, string textfiligrane, string textmarquage) throws documentexception, ioexception  {     this.pdfsoriginaux.add(pdforiginal);     this.init(pdfdestination, pdftatoueur, textfiligrane, textmarquage); }  /**  * initialisation des attributs  *   * @param pdfdestination  * @param pdftatoueur  * @param textfiligrane  * @throws documentexception  * @throws ioexception  */  private void init(file pdfdestination, file pdftatoueur, string textfiligrane, string textmarquage) throws documentexception, ioexception{      document = new document();     writer = pdfwriter.getinstance(document, new fileoutputstream(pdfdestination));     writer.setpageevent(this);     if (pdftatoueur != null) {         markreader = new pdfreader(pdftatoueur.getabsolutepath());         fonddepage = writer.getimportedpage(markreader, 1);     }      if (textfiligrane != null) {         textfili = textfiligrane;     }      if (textfiligrane != null) {         textmark = textmarquage;     }  }  /**  * applique la concatenation et la fusion  * @throws ioexception  */ public void fuse() throws ioexception{     //---->initialisation d'un flux vers le pdf     pdfreader originalreader = new pdfreader(pdfsoriginaux.get(0).getabsolutepath());     document.open();      (file pdforiginal : pdfsoriginaux) {         originalreader = new pdfreader(pdforiginal.getabsolutepath());         (int = 1; <= originalreader.getnumberofpages(); i++) {                 document.setpagesize(originalreader.getpagesizewithrotation(i));                 document.newpage();                 writer.getdirectcontent().addtemplate(writer.getimportedpage(originalreader, i), 0, 0);         }     }     document.close();     originalreader.close();     if (markreader != null) {         markreader.close();     } }   @override public void onendpage(pdfwriter writer, document document) {     pdfcontentbyte directcontent;     rectangle docpagesize = document.getpagesize();      //ajout du fond de page     if (markreader != null) {         directcontent = writer.getdirectcontentunder();         rectangle markpagesize = markreader.getpagesize(1);         float hscale = docpagesize.getwidth() / markpagesize.getwidth();         float vscale = docpagesize.getheight() / markpagesize.getheight();         float markscale = (hscale< vscale) ? hscale :  vscale;         float htrans = (float)((docpagesize.getwidth()-markpagesize.getwidth()* markscale) / 2.0);         float vtrans = (float)((docpagesize.getheight()-markpagesize.getheight()* markscale) / 2.0);         directcontent.addtemplate(fonddepage, markscale, 0, 0, markscale, htrans, vtrans );         }      //ajout du filigrane     if (textfili != null) {         directcontent = writer.getdirectcontent();         pdfgstate gstate = new pdfgstate();         gstate.setfillopacity(0.3f);         gstate.setstrokeopacity(0.3f);         directcontent.savestate();         directcontent.setgstate(gstate);         columntext.showtextaligned(directcontent,element.align_center, new phrase(textfili, font1), docpagesize.getwidth()/2, docpagesize.getheight()/2, 45);         directcontent.restorestate();     }      //ajout de la marque en haut à droite en petit     if (textmark != null) {         directcontent = writer.getdirectcontent();         pdfgstate gstate = new pdfgstate();         directcontent.savestate();         directcontent.setgstate(gstate);         columntext.showtextaligned(directcontent,element.align_right, new phrase(textmark, font2), docpagesize.getwidth()-3, docpagesize.getheight()-8, 0);         directcontent.restorestate();     } } 

}

but wrong : depending of original pdf result sometime buggy. orientation not keept. found answer of behavior here function can use itext concatenate / merge pdfs - causing issues

=> according m. lowagie have use pdfcopy instead of pdfwriter. made code:

public class fusionpdf2 extends pdfpageeventhelper {  private static final font font1 = fontfactory.getfont(fontfactory.helvetica, 50, font.normal, new graycolor(0.7f)); private static final font font2 = fontfactory.getfont(fontfactory.helvetica, 6, font.normal, basecolor.black);  private list<file> pdfsoriginaux = new arraylist<file>(); private file pdfdestination = null; private file pdftatoueur = null; private string textfiligrane = null; private string textmarquage = null;   /**  *   * @param pdfdestination fichier de pdf de destination  * @param pdfsoriginaux fichiers pdf à concatener  * @param pdftatoueur fond de page (peut être null)  * @param textfiligrane filigran (peut être null)  * @throws ioexception   * @throws documentexception   * @throws exception  */ public fusionpdf2(file pdfdestination, list<file> pdfsoriginaux, file pdftatoueur, string textfiligrane, string textmarquage) throws documentexception, ioexception  {     this.pdfdestination = pdfdestination;     if (pdfsoriginaux.isempty()){         throw new ioexception("aucun document pdf traiter !");     }     this.pdfsoriginaux = pdfsoriginaux;     this.pdftatoueur = pdftatoueur;     this.textfiligrane = textfiligrane;     this.textmarquage = textmarquage; }   /**  *   * @param pdfdestination fichier de pdf de destination  * @param pdforiginal fichier pdf traiter  * @param pdftatoueur fond de page (peut etre null)  * @param textfiligrane filigrane (peut etre null)  * @param textmarquage note technique en haut à droite en petit (peut etre null)  * @throws ioexception  * @throws documentexception  * @throws exception  */ public fusionpdf2(file pdfdestination, file pdforiginal, file pdftatoueur, string textfiligrane, string textmarquage) throws documentexception, ioexception  {     this.pdfdestination = pdfdestination;     this.pdfsoriginaux.add(pdforiginal);     this.pdftatoueur = pdftatoueur;     this.textfiligrane = textfiligrane;     this.textmarquage = textmarquage; }  /**  * applique la concatenation et la fusion  *   * @param pdfdestination  * @param pdftatoueur  * @param textfiligrane  * @throws documentexception  * @throws ioexception  * @throws ioexception  * @throws documentexception   */ public void fuse() throws ioexception, documentexception{      document document = new document();     pdfcopy copy = new pdfcopy(document, new fileoutputstream(pdfdestination));     document.open();     pdfreader originalreader;     pdfreader fdpreader = null;      if (pdftatoueur != null) {         fdpreader = new pdfreader(pdftatoueur.getabsolutepath());     }      (file pdforiginal : pdfsoriginaux) {         originalreader = new pdfreader(pdforiginal.getabsolutepath());         (int = 0 ; < originalreader.getnumberofpages(); ) {             pdfimportedpage page = copy.getimportedpage(originalreader, ++i);             pagestamp stamp = copy.createpagestamp(page);             rectangle docpagesize = originalreader.getpagesizewithrotation(i);              //ajout du fond de page             if (pdftatoueur != null) {                  pdfimportedpage fonddepage = copy.getimportedpage(fdpreader, 1);                  pdfcontentbyte directcontent = stamp.getundercontent();                 rectangle markpagesize = fdpreader.getpagesize(1);                 float hscale = docpagesize.getwidth() / markpagesize.getwidth();                 float vscale = docpagesize.getheight() / markpagesize.getheight();                 float markscale = (hscale< vscale) ? hscale :  vscale;                 float htrans = (float)((docpagesize.getwidth()-markpagesize.getwidth()* markscale) / 2.0);                 float vtrans = (float)((docpagesize.getheight()-markpagesize.getheight()* markscale) / 2.0);                 directcontent.addtemplate(fonddepage, markscale, 0, 0, markscale, htrans, vtrans );             }              //ajout du filigrane             if (stringutils.isnotblank(textfiligrane)) {                 pdfcontentbyte directcontent = stamp.getovercontent();                 pdfgstate gstate = new pdfgstate();                 gstate.setfillopacity(0.3f);                 gstate.setstrokeopacity(0.3f);                 directcontent.savestate();                 directcontent.setgstate(gstate);                 columntext.showtextaligned(directcontent,element.align_center, new phrase(textfiligrane, font1), docpagesize.getwidth()/2, docpagesize.getheight()/2, 45);                 directcontent.restorestate();             }              //ajout de la marque en haut à droite en petit             if (stringutils.isnotblank(textmarquage)) {                 pdfcontentbyte directcontent = stamp.getovercontent();                 columntext.showtextaligned(directcontent,element.align_right, new phrase(textmarquage, font2), docpagesize.getwidth()-3, docpagesize.getheight()-8, 0);             }              stamp.altercontents();             copy.addpage(page);         }          if (originalreader!=null) {             originalreader.close();         }     }      document.close();          } 

}

this time result in case : page in orientation.

but producted pdf 10 times fater previous code. after analysing found out cause : until added stationery.

it content of stationery duplicated each page instead of being reused.

obviously did wrong again. read chapter 6 of book wihtout finding solution.

if can me!

it content of stationery duplicated each page instead of being reused.

this true , reflects fundamental difference between fusionpdf , fusionpdf2 classes:

in former class import stationary page once @ beginning

private void init(file pdfdestination, file pdftatoueur, string textfiligrane, string textmarquage) throws documentexception, ioexception{     [...]     if (pdftatoueur != null) {         markreader = new pdfreader(pdftatoueur.getabsolutepath());         fonddepage = writer.getimportedpage(markreader, 1);     }     [...] } 

and reuse imported page again , again:

public void onendpage(pdfwriter writer, document document) {     [...]      //ajout du fond de page     if (markreader != null) {         [...]         directcontent.addtemplate(fonddepage, markscale, 0, 0, markscale, htrans, vtrans );         }      [...] } 

in latter class, on other hand, import stationary page again , again, once per page applied to:

for (int = 0 ; < originalreader.getnumberofpages(); ) {     [...]     if (pdftatoueur != null) {          pdfimportedpage fonddepage = copy.getimportedpage(fdpreader, 1);          [...]         directcontent.addtemplate(fonddepage, markscale, 0, 0, markscale, htrans, vtrans );     }      [...] } 

to have contents of stationary page in result pdf once, need import once.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -