Home » Java » Remove space between images added into a single pdf file with iText using java.

Remove space between images added into a single pdf file with iText using java.

Posted by: admin July 12, 2018 Leave a comment

Questions:

I am trying to crate PDF files from a list of images. 4 image should cover a full page with no margin padding or anything. My problem is that the added images are separated by a white line, and I can’t figure out a way to remove this separation.

    public ByteArrayOutputStream createMultiTicketPdf(List<String> base64Images) {
    PDFCreator creator = new PDFCreator();
    Document document = creator.getDocument();
    creator.setForMulti(true);

    float nomargin = 0; 
    creator.addCustomCSS("common", "/pdf/common.css");
    document.setMargins(nomargin, nomargin, nomargin, nomargin);
    creator.setTemplateRelativePath("/pdf/multitickettemplate.html");

    for(String base64Image : base64Images) {
        try {
            String parsedString = StringUtils.substringAfter(base64Image, ",");
            byte[] decoded = Base64.getDecoder().decode(parsedString);

            Image image = Image.getInstance(decoded);
            float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
                   - document.rightMargin()) / image.getWidth()) * 100;
            image.scalePercent(scaler);    
            image.setPaddingTop(nomargin);


            creator.addImage(Image.getInstance(image));
        } catch (BadElementException | IOException e) {
            LOGGER.error("Error occured:", e);
        }
    }

    return creator.create();
}
Answers: