IText PDFImage seems to shrink or disappear during

2019-09-12 03:01发布

问题:

I seem to have an issue after upgrading iText from 2.1.7 where the PDF seems to be missing or the image has shrunk between pages.

I'm wondering if this is a known issue or if there is something that I need to set in order to fix it.

Some context:

  • no real calculations have been changed when switching libraries.

  • the general structure is that we have a Document which has a pdfTable which holds a bunch of pdfImages.

  • changing between landscape and portrait produces different results.

  • the images are scaled down so that it will fit the page.

Edit: Sorry, my application was pretty big and does a bunch of work. I had to make a simple mock version before posting the problem

Sample Code (So the numbers are just examples that I used. I basically added a 800*600 Image 5 times using landscape position. I end up seeing only 3 pages when I am expecting 5.) Note: Using portrait page size shows all 5 but it seems that the sizes vary for some reason.

 ByteArrayOutputStream baos = createTemporaryOutputStream();

 Document doc = newDocument();
 PdfWriter writer = newWriter(doc, baos);
 writer.setViewerPreferences(PdfWriter.ALLOW_PRINTING | PdfWriter.PageLayoutSinglePage);

 //create page rectangle landscape
 Rectangle page = new Rectangle(PageSize.A4.rotate());
    doc.setPageSize(page);
    doc.setMargins((float)36.0, (float)36.0, (float)36.0, (float)36.0);
    doc.open();

    //create element pdf table.
    PdfPTable table = new PdfPTable(new float[]{(float) 770.0});
    table.setWidthPercentage(100);
    table.setSplitRows(true);
    table.setSplitLate(false);
    table.setHeaderRows(0);

    // in my case I used 5 800*600 images (same picture) 
    //then I loop through them and create pdfcell 
    //and then add it to table which then gets added to the document
    List<Image> hi = (List<Image>) model.get("images");
    for (Image image : hi) {

        com.itextpdf.text.Image pdfImage = com.itextpdf.text.Image.getInstance(image.getBytes());
        pdfImage.scalePercent((float) (0.8642384 * 100));

        PdfPCell cell = new PdfPCell(pdfImage, false);

        table.addCell(cell);
    }

    doc.add(table);
    doc.close();
标签: itext