0

I have one image with width:1024px and height:100000px I want to export this image into pdf with full size, but the image is put only on first page,..

here is my code:

Document doc = new Document();
try
{
    iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(generatedPdfSaveFilePath, FileMode.Create));
    doc.Open();
    Image jpg = Image.GetInstance(imagePath);
    jpg.Border = Rectangle.BOX;
    jpg.BorderWidth = 5f;
    doc.Add(jpg);
    doc.Add(new Paragraph("Original Width: " + jpg.Width.ToString()));
    doc.Add(new Paragraph("Original Height " + jpg.Height.ToString()));
    doc.Add(new Paragraph("Scaled Width: " + jpg.ScaledWidth.ToString()));
    doc.Add(new Paragraph("Scaled Height " + jpg.ScaledHeight.ToString()));
    float Resolution = jpg.Width / jpg.ScaledWidth * 72f;
    doc.Add(new Paragraph("Resolution: " + Resolution));
}
catch (Exception ex)
{
    //Log error;
}
finally
{
    doc.Close();
}

How export big image with full size on multiple pages?

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Alex
  • 8,908
  • 28
  • 103
  • 157

2 Answers2

2

You need to scale the picture and then add it like so:

Document doc = new Document();
try
{
    iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(generatedPdfSaveFilePath, FileMode.Create));
    doc.Open();
    Image jpg = Image.GetInstance(imagePath);
    jpg.Border = Rectangle.BOX;
    jpg.BorderWidth = 5f;

    var jpeg = new Jpeg(jpg);
    jpeg.ScaleToFit(doc.PageSize.Width - (doc.LeftMargin + doc.RightMargin),
        doc.PageSize.Height - (doc.BottomMargin + doc.TopMargin));

    doc.Add(jpeg);

    doc.Add(new Paragraph("Original Width: " + jpg.Width.ToString()));
    doc.Add(new Paragraph("Original Height " + jpg.Height.ToString()));
    doc.Add(new Paragraph("Scaled Width: " + jpeg.ScaledWidth.ToString()));
    doc.Add(new Paragraph("Scaled Height " + jpeg.ScaledHeight.ToString()));
    float Resolution = jpg.Width / jpg.ScaledWidth * 72f;
    doc.Add(new Paragraph("Resolution: " + Resolution));
}
catch (Exception ex)
{
    //Log error;
}
finally
{
    doc.Close();
}
heathesh
  • 261
  • 1
  • 5
  • thanks for your reply, but this code export the image also only on the first page – Alex Sep 10 '14 at 07:52
  • If you want the image to span multiple pages, maybe take a look at this: http://stackoverflow.com/questions/21829386/add-an-image-and-it-span-in-multiple-pages-using-itextsharp – heathesh Sep 10 '14 at 07:56
0

If you want the jpg to fill the full size of the page, you need to scale it.

For instance:

Rectangle rect = document.getPageSize();
jpg.scaleAbsolute(rect.getWidth(), rect.getHeight());

I assume that you want the image to be in the background. In your current code sample, you add the image and then you add some text under the image (or the text on this page and the image on the next page, or the image on this page and the text on the next page, depending on the size of the image).

If you want to add the image under the text, you need:

jpg.setAbsolutePosition(0, 0);

Finally, you want to add the image to every page that is create. I have just answered a question that adds a border to every page: How to draw border for whole pdf pages using iText library 5.5.2

Please read my answer to that question and create a page event that looks like this:

public class BackgroundImage extends PdfPageEventHelper {

    Image jpg;

    public BackgroundImage(Image jpg) {
         this.jpg = jpg;
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte canvas = writer.getDirectContent();
        Rectangle rect = document.getPageSize();
        jpg.scaleAbsolute(rect.getWidth(), rect.getHeight());
        jpg.setAbsolutePosition(0, 0);
        canvas.addImage(jpg);
    }
}

I didn't test this code, I wrote it off the cuff. You may need to adapt it a little.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165