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 7025189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:56:28+00:00 2026-05-27T23:56:28+00:00

I’m using iTextSharp to convert large images to PDF documents. This works, but the

  • 0

I’m using iTextSharp to convert large images to PDF documents.

This works, but the images appear cropped, because they exceed boundaries of the generated document.

So the question is – how to make the document same size as the image being inserted into it?

I’m using the following code:

  Document doc = new Document(PageSize.LETTER.Rotate());
  try
  {
     PdfWriter.GetInstance(doc, new FileStream(saveFileDialog1.FileName,FileMode.Create));
     doc.Open();
     doc.Add(new Paragraph());
     iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);
     doc.Add(img);
   }
   catch
   {
      // add some code here incase you have an exception
   }
   finally
   {
      //Free the instance of the created doc as well
      doc.Close();
   }
  • 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-27T23:56:29+00:00Added an answer on May 27, 2026 at 11:56 pm

    The Document object in iText and iTextSharp is an abstraction that takes care of various spacings, paddings and margins for you automatically. Unfortunately for you, this also means that when you call doc.Add() it takes into account existing margins of the document. (Also, if you happen to add anything else the image will be added relative to that, too.)

    One solution would be to just remove the margins:

    doc.SetMargins(0, 0, 0, 0);
    

    Instead, it’s easier to add the image directly to the PdfWriter object which you get from calling PdfWriter.GetInstance(). You’re currently throwing away and not storing that object but you can easily change your line to:

    PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(saveFileDialog1.FileName,FileMode.Create));
    

    Then you can access the DirectContent property of the PdfWriter and call its AddImage() method:

    writer.DirectContent.AddImage(img);
    

    Before doing this you must also absolutely position the image by calling:

    img.SetAbsolutePosition(0, 0);
    

    Below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.1.0 that shows the DirectContent method above. It dynamically creates two images of different sizes with two red arrows stretching across both vertically and horizontally. Your code would obviously just use standard image loading and could thus omit a lot of this but I wanted to deliver a full working example. See the notes in the code for more details.

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    namespace WindowsFormsApplication1 {
        public partial class Form1 : Form {
            public Form1() {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e) {
                //File to write out
                string outputFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Images.pdf");
    
                //Standard PDF creation
                using (FileStream fs = new FileStream(outputFilename, FileMode.Create, FileAccess.Write, FileShare.None)) {
                    //NOTE, we are not setting a document size here at all, we'll do that later
                    using (Document doc = new Document()) {
                        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                            doc.Open();
    
                            //Create a simple bitmap with two red arrows stretching across it
                            using (Bitmap b1 = new Bitmap(100, 400)) {
                                using (Graphics g1 = Graphics.FromImage(b1)) {
                                    using(Pen p1 = new Pen(Color.Red,10)){
                                        p1.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                        p1.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                        g1.DrawLine(p1, 0, b1.Height / 2, b1.Width, b1.Height / 2);
                                        g1.DrawLine(p1, b1.Width / 2, 0, b1.Width / 2, b1.Height);
    
                                        //Create an iTextSharp image from the bitmap (we need to specify a background color, I think it has to do with transparency)
                                        iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(b1, BaseColor.WHITE);
                                        //Absolutely position the image
                                        img1.SetAbsolutePosition(0, 0);
                                        //Change the page size for the next page added to match the source image
                                        doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, b1.Width, b1.Height, 0));
                                        //Add a new page
                                        doc.NewPage();
                                        //Add the image directly to the writer
                                        writer.DirectContent.AddImage(img1);
                                    }
                                }
                            }
    
                            //Repeat the above but with a larger and wider image
                            using (Bitmap b2 = new Bitmap(4000, 1000)) {
                                using (Graphics g2 = Graphics.FromImage(b2)) {
                                    using (Pen p2 = new Pen(Color.Red, 10)) {
                                        p2.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                        p2.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                        g2.DrawLine(p2, 0, b2.Height / 2, b2.Width, b2.Height / 2);
                                        g2.DrawLine(p2, b2.Width / 2, 0, b2.Width / 2, b2.Height);
                                        iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(b2, BaseColor.WHITE);
                                        img2.SetAbsolutePosition(0, 0);
                                        doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, b2.Width, b2.Height, 0));
                                        doc.NewPage();
                                        writer.DirectContent.AddImage(img2);
                                    }
                                }
                            }
    
    
                            doc.Close();
                        }
                    }
                }
                this.Close();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.