OiO.lk Blog pdf Image in iText7 PDF Prints with Low Resolution
pdf

Image in iText7 PDF Prints with Low Resolution


I am creating a PDF file using iText7 and adding an image to it. Generally, this works fine, but for some reason, the image appears in very low resolution when printed from the PDF. However, when I print the image directly from Windows, the resolution is excellent.

I’m unsure what I’m doing wrong. I’ve created a small sample repository to demonstrate the issue: https://github.com/MarvinKlein1508/IText7Resolution

private static async Task Main(string[] args)
{
    using MemoryStream memoryStream = new MemoryStream();
    PdfWriter writer = new PdfWriter(memoryStream, new WriterProperties().SetCompressionLevel(0));
    PdfDocument pdfDoc = new PdfDocument(writer);
    Document document = new Document(pdfDoc);
    float pageMargin = CalcCentimeterToPoints(PAGE_MARGIN_CM);
    document.SetMargins(pageMargin, pageMargin, pageMargin, pageMargin);

    PageSize pageSize = PageSize.A4;
    pdfDoc.SetDefaultPageSize(pageSize);
    string imagePath = "SchulzLF_0.png";

    float imageMargin = CalcCentimeterToPoints(IMAGE_MARGIN_CM);
    float nutzenHeight = (pageSize.GetHeight() / 2) - (2 * imageMargin);
    float imageWidth = pageSize.GetWidth() - (2 * imageMargin);
    float imageHeight = nutzenHeight;

    ImageData imageData = ImageDataFactory.Create(imagePath);
    Image image = new Image(imageData);
    //image.SetAutoScaleHeight(false);
    //image.SetAutoScaleWidth(false);
    image.ScaleToFit(imageWidth, imageHeight);
    document.Add(image);
    image.SetFixedPosition(imageMargin, imageMargin);
    document.Add(image);
    document.Close();
    byte[] pdfBytes = memoryStream.ToArray();

    await File.WriteAllBytesAsync("SchulzLF_0.pdf", pdfBytes);

}

Do I need to modify the image? If so, what should I consider to ensure it displays correctly in iText7?

The image should cover the entire A4 page, minus 0.5 cm margins on all sides, and half the height of the A4 page, also minus 0.5 cm margins.



You need to sign in to view this answers

Exit mobile version