How do I reference a single embedded file from multiple rows in a table

We have been trying to achieve this since a week now, still no luck. I wonder if this is possible. So need a confirmation from other experts.

We are trying to create PDF table with attachments embedded inside PDF per row like this in screenshot.

We are able to write a code which can print above PDF table correctly and it works fine.

The problem here is – inside PDF the embedded-file is same. But this file is actually copied 9 times instead of only once. Here is the screenshot:

Now because — our system has few files which are just evidence for each row. We just need to embed one attachment only once and then refer it as icon into multiple rows. This will help us save the size of PDF drastically.

One probable solution we have is — we will need to change the design, where for each evidence show all related rows. But our client would never approve this. Hence looking for a solution.
Please help us with this.

Here is the code that we are using:

@ExtendWith(MockitoExtension.class)
public class DeleteMeTest {

    public static final int ICON_SIZE = 11;
    public static final String PATH = "src/test/resources";
    public static final File FILE = new File(PATH);
    public static final String LOCATION = FILE.getAbsolutePath() + "/pdf-embed/";
    public static final String FILE_ICON_NAME = LOCATION + "file.png"; // Icon file
    public static final String ATTACHMENT_FILE_NAME = "source.pdf"; // source file
    public static final String TARGET_PDF = "target.pdf"; // target pdf

    @Test
    void testPdfWithEmbeddedDocuments() throws IOException {
        try (OutputStream target = new FileOutputStream(LOCATION + TARGET_PDF);
             InputStream source1 = new FileInputStream(LOCATION + ATTACHMENT_FILE_NAME);
             InputStream source2 = new FileInputStream(LOCATION + ATTACHMENT_FILE_NAME);
             InputStream source3 = new FileInputStream(LOCATION + ATTACHMENT_FILE_NAME);
             InputStream source4 = new FileInputStream(LOCATION + ATTACHMENT_FILE_NAME);
             InputStream source5 = new FileInputStream(LOCATION + ATTACHMENT_FILE_NAME);
             InputStream source6 = new FileInputStream(LOCATION + ATTACHMENT_FILE_NAME);
             InputStream source7 = new FileInputStream(LOCATION + ATTACHMENT_FILE_NAME);
             InputStream source8 = new FileInputStream(LOCATION + ATTACHMENT_FILE_NAME);
             InputStream source9 = new FileInputStream(LOCATION + ATTACHMENT_FILE_NAME)) {

            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new BufferedOutputStream(target)));
            Document document = new com.itextpdf.layout.Document(pdfDocument, PageSize.A4);
            Table table = new Table(UnitValue.createPercentArray(new float[]{90, 10})).useAllAvailableWidth();
            table.setFontSize(7);
            // Add Table body
            addTableRow(table, pdfDocument, source1);
            addTableRow(table, pdfDocument, source2);
            addTableRow(table, pdfDocument, source3);
            addTableRow(table, pdfDocument, source4);
            addTableRow(table, pdfDocument, source5);
            addTableRow(table, pdfDocument, source6);
            addTableRow(table, pdfDocument, source7);
            addTableRow(table, pdfDocument, source8);
            addTableRow(table, pdfDocument, source9);
            // finalize PDF
            document.add(table);
            pdfDocument.close();
        }

    }

    private void addTableRow(Table table, PdfDocument pdfDocument, InputStream source) {
        table.startNewRow();
        table.addCell(getTextCell("Some File Name: "));
        table.addCell(getAttachmentCell(pdfDocument, new Attachment(source, TARGET_PDF)));
    }

    private Cell getTextCell(String text) {
        Paragraph paragraph = new Paragraph();
        paragraph.add(text);
        Cell cell = new Cell();
        cell.add(paragraph);
        return cell;
    }

    private Cell getAttachmentCell(PdfDocument pdfDoc, Attachment attachment) {
        try (InputStream iconFileStream = new FileInputStream(FILE_ICON_NAME)) {
            ImageData img = ImageDataFactory.create(iconFileStream.readAllBytes());
            Image i = new Image(img);
            i.setNextRenderer(new LinkImageRenderer(i, pdfDoc, attachment));
            Cell fileCell = new Cell();
            fileCell.add(i);
            return fileCell;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static class LinkImageRenderer extends ImageRenderer {
        private final Attachment attachment;
        private final PdfDocument pdfDoc;
        private final Image image;

        public LinkImageRenderer(Image image, PdfDocument pdfDoc, Attachment attachment) {
            super((image.setHeight(ICON_SIZE).setWidth(ICON_SIZE)));
            this.pdfDoc = pdfDoc;
            this.image = image;
            this.attachment = attachment;
        }

        @Override
        public IRenderer getNextRenderer() {
            return new LinkImageRenderer(image, pdfDoc, attachment);
        }

        @Override
        public void draw(DrawContext drawContext) {
            super.draw(drawContext);
            PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec(pdfDoc,
                    attachment.inputStream,
                    attachment.fileName,
                    attachment.fileName,
                    null,
                    null);
            Rectangle rect = getOccupiedAreaBBox();
            PdfAnnotation pdfAnnotation = new PdfFileAttachmentAnnotation(rect, fileSpec);
            pdfAnnotation.setContents(attachment.fileName);
            PdfFormXObject xObject = new PdfFormXObject(rect);
            pdfAnnotation.setNormalAppearance(xObject.getPdfObject());
            drawContext.getDocument().getLastPage().addAnnotation(pdfAnnotation);
        }
    }

    public static class Attachment {
        public String fileName;
        public InputStream inputStream;

        public Attachment(InputStream stream, String file) {
            inputStream = stream;
            fileName = file;
        }
    }
}

Here is the itext dependency

<!-- https://kb.itextpdf.com/itext/release-itext-core-8-0-0 -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext7-core</artifactId>
    <version>8.0.2</version>
</dependency>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật