Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7502117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:45:31+00:00 2026-05-29T20:45:31+00:00

I’m generating PDF files from my C# 4.0 windows application using iTextSharp API. I’ll

  • 0

I’m generating PDF files from my C# 4.0 windows application using iTextSharp API. I’ll be passing HTML string which will contain Rich Text and Images. My PDF file size is A4 with default margins. Noticed that when I have a large image in dimension (e.g. height=”701px” width=”935px”) , the image is not coming to PDF. Looks like, I have to scale down the image dimension which should be able to fit in the PDF A4 size. I checked this by pasting the image to a word document of A4 size, MS Word automatically scales down the image by 36% i.e. MS Word takes only 64% of the original image dimension and sets the absolute height & width.

Can someone please help to mimic similar behavior in C#?

Let me know how to automatically set an image height & width to fit in A4 PDF file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:45:35+00:00Added an answer on May 29, 2026 at 8:45 pm

    That’s correct, iTextSharp won’t automatically size images that are too big for the document. So it’s just a matter of:

    • Calculating available document width and height with left/right and top/bottom page margins.
    • Getting the image width and height.
    • Comparing the document’s width and height with the image’s width and height.
    • Scaling the image if needed.

    Here’s one way, see inline comments:

    // change this to any page size you want    
    Rectangle defaultPageSize = PageSize.A4;   
    using (Document document = new Document(defaultPageSize)) {
      PdfWriter.GetInstance(document, STREAM);
      document.Open();
    // if you don't account for the left/right margins, the image will
    // run off the current page
      float width = defaultPageSize.Width
        - document.RightMargin
        - document.LeftMargin
      ;
      float height = defaultPageSize.Height
        - document.TopMargin
        - document.BottomMargin
      ;
      foreach (string path in imagePaths) {
        Image image = Image.GetInstance(path);
        float h = image.ScaledHeight;
        float w = image.ScaledWidth;
        float scalePercent;
    // scale percentage is dependent on whether the image is 
    // 'portrait' or 'landscape'        
        if (h > w) {
    // only scale image if it's height is __greater__ than
    // the document's height, accounting for margins
          if (h > height) {
            scalePercent = height / h;
            image.ScaleAbsolute(w * scalePercent, h * scalePercent);
          }
        }
        else {
    // same for image width        
          if (w > width) {
            scalePercent = width / w;
            image.ScaleAbsolute(w * scalePercent, h * scalePercent);
          }
        }
        document.Add(image);
      }
    }
    

    The only point worth noting is that imagePaths above is a string[] so that you can test what happens when adding a collection of images that are to big to fit in a page.

    Another way is to put the image in a one column, single cell PdfPTable:

    PdfPTable table = new PdfPTable(1);
    table.WidthPercentage = 100;
    foreach (string path in imagePaths) {
      Image image = Image.GetInstance(path);
      PdfPCell cell = new PdfPCell(image, true);
      cell.Border = Rectangle.NO_BORDER;
      table.AddCell(cell);
    }
    document.Add(table);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am generating PDF/HTML report from a BIRT template. I am using visibility property
I am using alivepdf for generating pdf files from my flex project. I am
I am generating a PDF file dynamically from html/css using the cfdocument tag. There
I'm generating a complex PDF from a swing application by printing my JComponent. This
I'm working with generating .pdf's from PHP using this library: http://www.fpdf.org/ I am currently
I am generating one PDF from the Code Behind File using StringWriter and HtmlTextWriter.
I am generating a pdf file in asp.net c# using itextsharp. i am not
My Access 2000 application is generating a PDF from a snapshot of a report
I'm using PDFBox to extract the outline (bookmarks) information from PDF files, that's even
I'm generating a PDF using JasperReports from Java. I finally succeeded in making Jasper

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.