1

Goal

I need to increase the quality of the bitmap that is being added to a pdf. I have a 300dpi image, but looks really bad when itextsharp adds it to pdf.

Description

I am working on a project which takes a bitmap and writes it to a pdf using itextSharp. The problem is that the project makes a graph, saves it as a bitmap, writes it to the pdf, and the graph appears to be low quality. I am unable to attach the picture here of what is being graphed, but will do my best to describe what is going on. Here is the code below:

//Declare chart object 
private static System.Windows.Forms.DataVisualization.Charting.Chart chart_runs;

//Start the PDF Document
Document pdfDocument = new Document(PageSize.LETTER);

private static System.Drawing.Bitmap GetChartBitmap()
{

        chart_runs.Width = 2269;
        chart_runs.Height = 1406;

        System.Drawing.Bitmap bitmap1 = new System.Drawing.Bitmap(2269, 1406);
        bitmap1.SetResolution(300, 300);
        chart_runs.DrawToBitmap(bitmap1, new System.Drawing.Rectangle(0, 0, 2269, 1406));

        string path = System.IO.Path.GetTempPath();

        bitmap1.Save(path + @"\Image.png");
        return bitmap1;

            }

Here is how the bitmap and image is added to the pdf.

        System.Drawing.Bitmap img = GetChartBitmap();
        img.Dispose();
        string path = System.IO.Path.GetTempPath();
        iTextSharp.text.Image imag
            = iTextSharp.text.Image.GetInstance(path + @"\Image.bmp");

        //imag.ScaleAbsolute(chart_runs.Width, chart_runs.Height);
        //imag.ScaleToFit(550, 330);
          imag.ScaleAbsolute(550, 350);
        //imag.ScaleAbsolute(650, 450);
        //imag.ScaleAbsolute(100f, 100f);

        pdfDocument.Add(imag);

        pdfDocument.Close();
        pdfDocument.Dispose();

Attempts

I have spent considerable time trying to fix this, here are some of the highlights.

  • At first, it was believed this was a DPI issue. However, when I made the bitmap (and itext image) 300DPI, no quality difference was noticed.
  • I then added more pixels by enlarging the targetBounds object above. This had the weird effect of shrinking my graph on my PDF? Why?
  • I have attempted many manipulations with itext.scale functions as I found here.
  • Another justification why I am trying to make my bitmap larger is on here.

As I said above, I seem to be making my image even smaller with the current approach. I am just trying to increase the quality of the bitmap so when people print it off or zoom in on the pdf it doesn't look terrible. I appreciate any help.

Community
  • 1
  • 1
hlyates
  • 1,279
  • 3
  • 22
  • 44
  • Is there no way for you to produce a sample PDF with a sample chart? It really would help for us to see what you're talking about. However, generally speaking, when dealing with .Net, iTextSharp and PDFs I recommend ignoring any setting that claims to be DPI. See [this post for a discussion on it](http://stackoverflow.com/a/8245450/231316) but for a higher quality image you just want more pixels crammed into a smaller space. 200 horizontal pixels placed in a 100 "unit" space at the default 72 units/inch gives you an _effective_ DPI of 144. (72 * 200 / 100). – Chris Haas Sep 12 '14 at 20:50
  • @ChrisHaas I will attempt to code a self enclosed example tonight and update my question. I don't have enough rep to post images, but I will attempt to provide sample code that will show my exact problem. What I mean is that the code will create the chart image and everything. – hlyates Sep 12 '14 at 20:53

1 Answers1

2

Not an answer yet

Here's a very simple image generating function just for testing purposes, it should be fairly self-explanatory:

private static byte[] CreateImage(string text, int fontSize, int width, int height) {
    using (var b = new Bitmap(width, height)) {
        using (var g = Graphics.FromImage(b)) {
            using(var br = new SolidBrush(System.Drawing.Color.Red)){
                using (var f = new System.Drawing.Font("Arial Unicode MS", fontSize)) {
                    g.DrawString(text, f, br, 10, 10);
                    using (var ms = new System.IO.MemoryStream()) {
                        b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                        return ms.ToArray();
                    }
                }
            }
        }
    }
}

If you examine the PDF generated from the below code that uses the function above you'll see each image in the PDF gets progressively "better" and you can zoom in further before seeing jagged lines. Ultimately you're working with raster/bitmap files, however, so some pixelation will almost always eventually occur. The DPI listed is the "effective DPI" which is based on the assumption that the PDF will map 72 units into 1 inch of space (which is the PDF standard's default) and that it is printed at 100% resolution.

var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");

using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (var doc = new Document()) {
        using (var writer = PdfWriter.GetInstance(doc, fs)) {
            doc.SetMargins(0, 0, 0, 0);
            doc.Open();


            var img1 = iTextSharp.text.Image.GetInstance(CreateImage("Hello", 24, 600, 50));
            doc.Add(img1); //72 DPI

            var img2 = iTextSharp.text.Image.GetInstance(CreateImage("Hello", 48, 1200, 100));
            img2.ScaleAbsolute(600, 50);
            doc.Add(img2); //144 DPI

            var img3 = iTextSharp.text.Image.GetInstance(CreateImage("Hello", 96, 2400, 200));
            img3.ScaleAbsolute(600, 50);
            doc.Add(img3); //288 DPI

            var img4 = iTextSharp.text.Image.GetInstance(CreateImage("Hello", 192, 4800, 400));
            img4.ScaleAbsolute(600, 50);
            doc.Add(img4); //576 DPI


            doc.Close();
        }
    }
}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • What a fantastic post. This proves to me I must figure out how to get Systems.Windows.Forms.DataVisualization.Charting.Chart to draw pictures of higher resolution. – hlyates Sep 12 '14 at 21:30
  • @ChrisHass I have a 2268 x 2343, 300dpi image. I did this by calculating what a 7.56" x 4.68" image on a page should be. However, when I am trying to add this image to my pdf. The size is not right and it looks very blurry. It seems itext is killing the quality of my image. Do you know why this might be happening? – hlyates Sep 15 '14 at 15:49
  • I simply took what the 72 dpi calculation was for 7.56' x 4.68'. I then took the 3X by that and had it graph. – hlyates Sep 15 '14 at 17:02