c# - Adding an image to a PDF using iTextSharp and scale it properly -


here's code. correctly adds pictures want , works except images using native resolution, if image big it's being cropped fit page.

is there way have picture use zoom feature stretch fit, maintain aspect ratio? there has i'm missing there. :p

here's picture illustrate problem: alt text

using system; using system.io; using itextsharp.text; using itextsharp.text.pdf; using system.drawing; using system.collections.generic;  namespace winformsplayground {     public class pdfwrapper     {         public void createpdf(list<system.drawing.image> images)         {             if (images.count >= 1)             {                 document document = new document(pagesize.letter);                 try                 {                      // step 2:                     // create writer listens document                     // , directs pdf-stream file                      pdfwriter.getinstance(document, new filestream("chap0101.pdf", filemode.create));                      // step 3: open document                     document.open();                      foreach (var image in images)                     {                         itextsharp.text.image pic = itextsharp.text.image.getinstance(image, system.drawing.imaging.imageformat.jpeg);                         document.add(pic);                         document.newpage();                     }                 }                 catch (documentexception de)                 {                     console.error.writeline(de.message);                 }                 catch (ioexception ioe)                 {                     console.error.writeline(ioe.message);                 }                  // step 5: close document                 document.close();             }         }     } } 

i solved using following:

foreach (var image in images) {     itextsharp.text.image pic = itextsharp.text.image.getinstance(image, system.drawing.imaging.imageformat.jpeg);      if (pic.height > pic.width)     {         //maximum height 800 pixels.         float percentage = 0.0f;         percentage = 700 / pic.height;         pic.scalepercent(percentage * 100);     }     else     {         //maximum width 600 pixels.         float percentage = 0.0f;         percentage = 540 / pic.width;         pic.scalepercent(percentage * 100);     }      pic.border = itextsharp.text.rectangle.box;     pic.bordercolor = itextsharp.text.basecolor.black;     pic.borderwidth = 3f;     document.add(pic);     document.newpage(); } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -