pdf - How to resize image in itext in java -


this source code.

  document document = new document();     string txtvehno = "";     string fdate = "", tdate = "", remtype = "0";     string filename = "reminders_" + utility.sysdate("yyyy_mm_dd") + ".pdf";     string asdate = utility.sysdate("dd/mm/yyyy");     arraylist<reminderpdf> reminderpdfs = null;     userdetails userdetails = null;     string txtmarno = "";     string formatteddate = "";     string address = "";        blob imageblob = null;     byte[] imagebytes = new byte[0];      try {         txtvehno = request.getparameter("txtvehno2");         fdate = request.getparameter("txtdate3");         tdate = request.getparameter("txtdate4");         remtype = request.getparameter("remtype");         txtmarno = request.getparameter("txtmarno");          if (txtvehno != null && txtvehno != "") {              reminderpdfs = datamanagerbean.downloadremindersforrequirementletter(txtvehno, remtype);           } else if (txtmarno != null && txtmarno != "") {              reminderpdfs = datamanagerbean.downloadremindersforrequirementletterbymarno(txtmarno, remtype);           } else {              reminderpdfs = datamanagerbean.downloadremindersforrequirementletter(fdate, tdate, remtype);           }         if (reminderpdfs.size() > 0) {              response.setcontenttype("application/pdf");             response.setheader("content-disposition", "attachment; filename=\"" + filename + "\"");             pdfwriter.getinstance(document, response.getoutputstream());             //pdfwriter.getinstance(document, new fileoutputstream(filename));             document.open();             document.setpagesize(pagesize.a4);             document.setmargins(85, 85, 85, 85);             (int = 0; < reminderpdfs.size(); i++) {                 string adrstr[] = reminderpdfs.get(i).address.split("\r\n");                  address = reminderpdfs.get(i).address.replaceall(adrstr[adrstr.length - 1], "");                 document.newpage();                 paragraph[] paragraph = new paragraph[8];                 paragraph[0] = new paragraph(new phrase(reminderpdfs.get(i).mainheader, fontfactory.getfont(fontfactory.courier, 12, font.bold)));                 paragraph[0].setalignment(element.align_right);                 document.add(paragraph[0]);                 paragraph[1] = new paragraph(new phrase(reminderpdfs.get(i).subheader, fontfactory.getfont(fontfactory.courier, 11)));                 paragraph[1].setalignment(element.align_right);                 document.add(paragraph[1]);                 if (reminderpdfs.get(i).print.equals("y")) {                     paragraph[2] = new paragraph(new phrase("(reprint)", fontfactory.getfont(fontfactory.courier, 10)));                 } else {                     paragraph[2] = new paragraph(new phrase("", fontfactory.getfont(fontfactory.courier, 11)));                 }                 paragraph[2].setalignment(element.align_right);                 document.add(paragraph[2]);                 paragraph[3] = new paragraph(new phrase("date : " + reminderpdfs.get(i).asdate, fontfactory.getfont(fontfactory.courier, 11)));                 paragraph[3].setalignment(element.align_right);                 document.add(paragraph[3]);                 paragraph[4] = new paragraph(new phrase(" ", fontfactory.getfont(fontfactory.courier, 12, font.bold)));                 paragraph[4].setalignment(element.align_center);                 document.add(paragraph[4]);                 paragraph[5] = new paragraph(new phrase(address, fontfactory.getfont(fontfactory.courier, 11, font.bold)));                 paragraph[5].setalignment(element.align_left);                 document.add(paragraph[5]);                 paragraph[6] = new paragraph(new phrase("\n" + adrstr[adrstr.length - 1], fontfactory.getfont(fontfactory.courier, 11, font.plain)));                 paragraph[6].setalignment(element.align_left);                 document.add(paragraph[6]);                 paragraph[7] = new paragraph(new phrase(" ", fontfactory.getfont(fontfactory.courier, 12, font.bold)));                 paragraph[7].setalignment(element.align_center);                 document.add(paragraph[7]);                  pdfptable table = new pdfptable(2);                 table.setwidthpercentage(100);                   formatteddate = utility.dateformatter(reminderpdfs.get(i).dol, "yyyy-mm-dd", "dd-mm-yyyy");                  table.addcell(new phrase("vehicle no: " + reminderpdfs.get(i).vehno, fontfactory.getfont(fontfactory.courier, 11)));                 table.addcell(new phrase("mar no       : " + reminderpdfs.get(i).marno, fontfactory.getfont(fontfactory.courier, 11)));                 table.addcell(new phrase("policy no : " + reminderpdfs.get(i).policyno, fontfactory.getfont(fontfactory.courier, 11)));                 table.addcell(new phrase("date of loss : " + formatteddate, fontfactory.getfont(fontfactory.courier, 11)));                 document.add(table);                  paragraph[] paragraph2 = new paragraph[3];                 paragraph2[0] = new paragraph(new phrase(" ", fontfactory.getfont(fontfactory.courier, 12, font.bold)));                 paragraph2[0].setalignment(element.align_center);                 document.add(paragraph2[0]);                  string[] docs = reminderpdfs.get(i).content.split("\r\n");                  paragraph2[1] = new paragraph(new phrase(docs[0], fontfactory.getfont(fontfactory.courier, 11)));                 paragraph2[1].setalignment(element.align_justified);                 document.add(paragraph2[1]);                 paragraph2[2] = new paragraph(new phrase(" ", fontfactory.getfont(fontfactory.courier, 12, font.bold)));                 paragraph2[2].setalignment(element.align_center);                 document.add(paragraph2[2]);                   // if (reminderpdfs.get(i).docs.length() > 0) {                 int k = 1;                 (int j = 1; j < docs.length; j++) {                     if (!docs[j].trim().equalsignorecase("")) {                         document.add(new paragraph(new phrase("          " + k + ". " + docs[j].trim(), fontfactory.getfont(fontfactory.courier, 11))));                         k++;                     }                 }                 // }                  paragraph[] paragraph3 = new paragraph[7];                 paragraph3[0] = new paragraph(new phrase(" ", fontfactory.getfont(fontfactory.courier, 14, font.bold)));                 paragraph3[0].setalignment(element.align_center);                 document.add(paragraph3[0]);                  paragraph3[1] = new paragraph(new phrase("thank you,", fontfactory.getfont(fontfactory.courier, 11)));                 paragraph3[1].setalignment(element.align_left);                 document.add(paragraph3[1]);                  paragraph3[2] = new paragraph(new phrase("yours faithfully,", fontfactory.getfont(fontfactory.courier, 11)));                 paragraph3[2].setalignment(element.align_left);                 document.add(paragraph3[2]);                  paragraph3[3] = new paragraph(new phrase("union assurance general ltd", fontfactory.getfont(fontfactory.courier, 11, font.bold)));                 paragraph3[3].setalignment(element.align_left);                 document.add(paragraph3[3]);                  paragraph3[4] = new paragraph(new phrase(" ", fontfactory.getfont(fontfactory.courier, 12, font.bold)));                 paragraph3[4].setalignment(element.align_center);                 document.add(paragraph3[4]);                   imageblob = datamanagerbean.getesignature(reminderpdfs.get(i).createduser);                 if (imageblob != null) {                     imagebytes = imageblob.getbytes(1, (int) imageblob.length());                     image img = image.getinstance(imagebytes);                     float width = img.getwidth();                     float hight = img.getheight();                     if (width > 200 || hight > 100) { //                        img.scaleabsolutewidth(200); //                        img.scaleabsoluteheight(100);  //                       img.scaleabsolute(200f, 100f);                         img.scaletofit(200f, 100f);                     }                     width = img.getscaledwidth();                     hight = img.getscaledheight();                     document.add(img);                 }                  userdetails = userbean.getuserdetailsbyuserid(reminderpdfs.get(i).createduser);                  paragraph3[5] = new paragraph(new phrase((userdetails.getufname().concat(" ")).concat(userdetails.getulname()), fontfactory.getfont(fontfactory.courier, 11)));                 paragraph3[5].setalignment(element.align_left);                 document.add(paragraph3[5]);                   paragraph3[6] = new paragraph(new phrase(userdetails.getdesig(), fontfactory.getfont(fontfactory.courier, 11)));                 paragraph3[6].setalignment(element.align_left);                 document.add(paragraph3[6]);                   //datamanagerbean.printstatusupdate(reminderpdfs.get(i).rmid, "y");             }             document.close();              response.sendredirect("reminders.jsp");          } else {             if (txtvehno != null && txtvehno != "") {                 response.sendredirect("reminders.jsp?msgtype=3&msg=no records policy/vehicle no. - " + txtvehno);             } else if (txtmarno != null && txtmarno != "") {                 response.sendredirect("reminders.jsp?msgtype=4&msg=no records mar no. - " + txtmarno);             } else if (fdate != null && fdate != "") {                 response.sendredirect("reminders.jsp?msgtype=5&msg=no records between " + fdate + " " + tdate);             }         }     } catch (documentexception e) {         systemmessage.getinstance().writemessage("error : downloadrequirements.jsp - " + e.tostring());     } {         try {             //reminderpdfs = null;         } catch (exception ex) {         }     } 

i tried
img.scaleabsolutewidth(200);
img.scaleabsoluteheight(100);
img.scaleabsolute(200f, 100f);
img.scaletofit(200f, 100f);
non of resizing image.
when debug code width=200 , height= 100 value too. guess means it's height , width changed.
please let me know issue in here.
thank you.

note later
issues wasn't of these methods. issue image not compatible size. after set size 100,50 imaged resized perfectly.
thank again.

your allegation "none of resizing image" wrong, , using wrong methods width , height of image. take @ chapter 2 of book:

there different ways change dimensions of image:

  • the width , height parameters of scaletofit() define maximum dimensions of image. if width/height ratio differs aspect ratio of image, either width, or height, smaller corresponding parameter of method.
  • the width , height parameters respected when using scaleabsolute(). resulting image risks being stretched in x or y direction if don’t choose parameters wisely. can use scaleabsolutewidth() , scaleabsoluteheight().
  • scalepercent() comes in 2 versions: 1 2 parameters, percentage width , percentage height; , 1 parameter, percentage applied equally width , height.

it’s common misconception resizing images in itext changes quality of image. it’s important understand itext takes image is: itext doesn’t change number of pixels in image.

there many examples prove these methods change size of image in document.

if continue reading documentation, discover problem in code:

when creating image instance file, won’t know dimensions before or after scaling it. can width , height of image these methods:

  • getwidth() , getheight() inherited rectangle object. return original height , width of image.
  • getplainwidth() , getplainheight() return width , height after scaling. these dimensions of image used print on page.
  • getscaledwidth() , getscaledheight() return width , height needed print image. these dimensions equal plain width , height, except in cases image rotated.

in code, original dimensions of image. change dimensions of image, , ask original dimensions. shouldn't surprise you don't different dimensions when asking original dimensions instead of original dimensions.


Comments

Popular posts from this blog

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

asp.net - Problems sending emails from forum -