Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# high resolution images in PDF

I'm trying to put high quality images into PDF (one per page). But if I set page size to a4, I have to resize my pictures, becouse they're too large. Then they loose their quality. Is there any way to put big image to a4 page without loosing quality?

I'm using iTextSharp library, firstly I'm creating the document

document = new Document(PageSize.A4, 0, 0, 0, 0);
FileStream output = new FileStream(pdfPath + "document.pdf", FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(document, output);
document.Open();

then I'm adding each picture

document.Add(iTextSharp.text.Image.GetInstance(toSaveImage, System.Drawing.Imaging.ImageFormat.Tiff));

and closing the document

document.Close();
like image 888
Com Piler Avatar asked Jun 06 '13 19:06

Com Piler


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

First let me clear a couple of misunderstandings:

  • a PDF document doesn't have a resolution. The comment by spender was wrong. There is no such thing as DPI in PDF. The resolution only comes into play when a PDF is rendered (to the screen, to paper,...) and that's why there may be a DPI in a PDF viewer (but that's something completely different).
  • when you scale an Image object in iTextSharp, you don't lose any information: the number of pixels remains the same. Whereas PDF doesn't have a resolution, the images inside a PDF do. When you the image scale down (that is: you put the same number of pixels on a smaller canvas), the resolution increases; when you scale up, the resolution decreases.

Now for your question: you're not obliged to create A4 pages:

Image img =
    iTextSharp.text.Image.GetInstance(toSaveImage,
        System.Drawing.Imaging.ImageFormat.Tiff);
Rectangle pagesize = new Rectangle(img.ScaledWidth, img.ScaledHeight);
Document document = new Document(pagesize);
img.SetAbsolutePosition(0, 0);
document.Add(img);

I created the Document based on the scaled dimensions of the Image. Don't let the method names mislead you: ScaledWidth and ScaledHeight are the safest methods to use when getting the dimensions of an Image. Not only do they include any scaling operations, you may have done on the image, the also take into account the space needed for the image after rotating it.

I've set the absolute position to the lower-left corner. That's safer than setting the page margins to 0.

EDIT: If you don't want to change the page size, then you have to use the ScaleToFit() method:

Image img =
    iTextSharp.text.Image.GetInstance(toSaveImage,
        System.Drawing.Imaging.ImageFormat.Tiff);
img.ScaleToFit(PageSize.A4);

Note that the method to scale to fit to a Rectangle object was introduced in one of the latest iTextSharp versions. An alternative would be to use the ScaleToFit() method that requires the width and the height of the rectangle.

like image 111
Bruno Lowagie Avatar answered Oct 09 '22 22:10

Bruno Lowagie