0

here i am trying to convert jpg formatted images files to single pdf by appending one by one.. here is the code

 //Create a new document
        iTextSharp.text.Document Doc = new iTextSharp.text.Document(PageSize.A4);
        //Store the document on the desktop
        string PDFOutput = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output2.pdf");
        PdfWriter writer = PdfWriter.GetInstance(Doc, new FileStream(PDFOutput, FileMode.Create, FileAccess.Write, FileShare.Read));

        //Open the PDF for writing
        Doc.Open();

        //give folder from your local system that contains images
        string Folder = System.Web.Configuration.WebConfigurationManager.AppSettings["path"].ToString();
        foreach (string F in System.IO.Directory.GetFiles(Folder, "*.jpg"))
        {
           Image image1 = Image.GetInstance(F);
            //image1.SetAbsolutePosition(10f, 10f);
            image1.ScaleAbsolute(100, 300);

            //Insert a page
            Doc.NewPage();
            //Add image
            Doc.Add(image1);
        }

        //Close the PDF
        Doc.Close();

but in output the image is not fitted into page but exceeds the page size..

help me out how to the set image size to fit without loss of quality... thank you

thiru
  • 173
  • 1
  • 4
  • 16
  • Take a look: [A4 is the default size when you don't pass a parameter to the Document constructor. Afterwards, you try changing the page size like this:](http://stackoverflow.com/a/30523899/3060520) – huse.ckr Oct 25 '16 at 08:04
  • Read the documentation: [How to add multiple images into a single PDF?](http://developers.itextpdf.com/question/how-add-multiple-images-single-pdf) The best way is **not** to create pages of size A4, but pages that have the same size of the images. The documentation shows a Java example, but it is a no-brainer to port Java examples to C#. I will close this question. – Bruno Lowagie Oct 25 '16 at 08:04
  • By the way: you care defining a `PdfPCell` and then you are adding that cell straight to the `Document`. That can't ever work. A `PdfPCell` should be added to a `PdfPTable`, not to the `Document`. – Bruno Lowagie Oct 25 '16 at 08:09

0 Answers0