4

I'm looking to convert an Image (PNG, JPEG, GIF), in the form of a byte[] to a PDF.

I'm currently using this function, which works, but cuts off the bottom of images that over a certain height or specific proportions; for example 500x2000.
Where am I going wrong here?

public byte[] ConvertImageToPDF(byte[] bytes)
{
    byte[] pdfArray;
    using (var memoryStream = new MemoryStream())
    {
        using (var pdfWriter = new PdfWriter(memoryStream))
        {
            var pdf = new PdfDocument(pdfWriter);
            var document = new Document(pdf);
            ImageData imageData = ImageDataFactory.Create(bytes);
            document.Add(new Image(imageData));
            document.Close();
        }
        pdfArray = memoryStream.ToArray();
    }
    return pdfArray;
}
1
  • Good call. It looks like the issue is caused by images with an aspect ratio too thin/tall when adjusted to the width of a normal pdf page. Actual pixel height is irrelevant. I'm currently having a lot of environment issues, but if you can confirm that this works with an unusually tall image, such as 500x2000, I'll mark .SetTextAlignment(TextAlignment.JUSTIFIED_ALL) as the solution. Commented May 11, 2022 at 21:40

1 Answer 1

1

I suppose what you want is the PdfWriter to auto-scale the Image inside the Document.
Optionally, position the Image in the center of the Page.

You can change your code setting [Image].SetAutoScale(true) and [Image].SetHorizontalAlignment(HorizontalAlignment.CENTER):

Note: I've defined aliases for iText.Layout.Properties (alias: PdfProperties) and iText.Layout.Element.Image (alias: PdfImage), to avoid conflict with other .Net assemblies that have classes and enumerators with the same exact names. Just remove them in case you don't need them at all.

using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using PdfProperties = iText.Layout.Properties;
using PdfImage = iText.Layout.Element.Image;

public byte[] ConvertImageToPDF(byte[] imageBytes)
{
    using (var ms = new MemoryStream()) {
        using (var pdfWriter = new PdfWriter(ms)) {
            var pdf = new PdfDocument(pdfWriter);
            var document = new Document(pdf);

            var img = new PdfImage(ImageDataFactory.Create(imageBytes))
                .SetAutoScale(true)
                .SetHorizontalAlignment(PdfProperties.HorizontalAlignment.CENTER);

            document.Add(img);
            document.Close();
            pdf.Close();
            return ms.ToArray();
        }
    }
}

You can also specify the size, in floating point units, of the Image and use the [Image].ScaleToFit() method, to scale the Image within those bounds.
Here, using a PageSize set to PageSize.A4. You can of course set different measures.

using iText.Kernel.Geom;

// [...]
var document = new Document(pdf);

var page = document.GetPageEffectiveArea(PageSize.A4);
var img = new PdfImage(ImageDataFactory.Create(imageBytes))
    .ScaleToFit(page.GetWidth(), page.GetHeight())
    .SetHorizontalAlignment(PdfProperties.HorizontalAlignment.CENTER);

// [...]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.