itextsharp - How to edit PdfTemplate width (in Java) -


is possible edit width of pdftemplate object before close of document ? in pdf process create document, , set template write total page number. set width doesnot works :

// onopendocument pdftemplate pagenumtemplate = writer.getdirectcontent().createtemplate(10, 20); globaldatamap.put("mytemplate", pagenumtemplate)  // onclosedocument font font = (font) globaldatamap.get("myfont"); string pagenum = string.valueof(writer.getpagenumber());  float widthpoint = font.getbasefont().getwidthpoint(pagenum, font.getsize()); pdftemplate pdftemplate = (pdftemplate) globaldatamap.get("mytemplate"); pdftemplate.setwidth(widthpoint); columntext.showtextaligned(pdftemplate, element.align_left, new phrase(pagenum), 0, 1, 0); 

an error in op's initial code

you call columntext.showtextaligned string third parameter:

string pagenum = string.valueof(writer.getpagenumber()); [...] columntext.showtextaligned(pdftemplate, element.align_left, pagenum, 0, 1, 0); 

but columntext.showtextaligned overloads expect phrase there:

public static void showtextaligned(final pdfcontentbyte canvas, final int alignment, final phrase phrase, final float x, final float y, final float rotation)  public static void showtextaligned(final pdfcontentbyte canvas, int alignment, final phrase phrase, final float x, final float y, final float rotation, final int rundirection, final int arabicoptions) 

(at least current 5.5.9-snapshot development version).

thus, might want use

columntext.showtextaligned(pdftemplate, element.align_left, new phrase(pagenum), 0, 1, 0); 

instead.

but works

based on op's code built proof-of-concept:

try (   fileoutputstream stream = new fileoutputstream(new file(result_folder, "dynamictemplatewidths.pdf"))    ) {     document document = new document(pagesize.a6);     pdfwriter writer = pdfwriter.getinstance(document, stream);     writer.setpageevent(new pdfpageeventhelper()     {         pdftemplate dynamictemplate = null;         font font = new font(basefont.createfont(), 12);         string postfix = "0123456789";          @override         public void onopendocument(pdfwriter writer, document document)         {             super.onopendocument(writer, document);             dynamictemplate = writer.getdirectcontent().createtemplate(10, 20);         }          @override         public void onendpage(pdfwriter writer, document document)         {             writer.getdirectcontent().addtemplate(dynamictemplate, 100, 300);         }          @override         public void onclosedocument(pdfwriter writer, document document)         {             float widthpoint = font.getbasefont().getwidthpoint(postfix, font.getsize());             dynamictemplate.setwidth(widthpoint / 2.0f);             columntext.showtextaligned(dynamictemplate, element.align_left, new phrase(string.valueof(writer.getpagenumber()) + "-" + postfix), 0, 1, 0);         }      });     document.open();      document.add(new paragraph("page 1"));     document.newpage();     document.add(new paragraph("page 2"));      document.close(); } 

the output:

dynamictemplatewidths.pdf

considering set template width half width of postfix 0123456789 in onclosedocument, expected.


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 -