Duplicate image is creating when i try to convert into pdf

Duplicate image is creating when i try to convert into pdf is there any issue in this sample code?

class ImageProcessor implements FileProcessor {
@Override
public void processFile(Document pdfDocument, File file) {
System.out.println("Processing Image file: " + file.getName());

    try {
        BufferedImage bufferedImage = ImageIO.read(file);
        double imgWidth = bufferedImage.getWidth();
        double imgHeight = bufferedImage.getHeight();

        double pageWidth = 11.00 * 72;
        double pageHeight = 8.50 * 72;

        Page page = pdfDocument.getPages().add();
        page.setPageSize(pageWidth, pageHeight);

        double newWidth = imgWidth;
        double newHeight = imgHeight;
        if (imgWidth > pageWidth || imgHeight > pageHeight) {
            double scale = Math.min(pageWidth / imgWidth, pageHeight / imgHeight);
            newWidth = imgWidth * scale;
            newHeight = imgHeight * scale;
        }

        Image image = new Image();
        image.setFile(file.getAbsolutePath());
        image.setFixWidth(newWidth);
        image.setFixHeight(newHeight);

        image.setHorizontalAlignment(HorizontalAlignment.Center);
        image.setVerticalAlignment(VerticalAlignment.Center);

        page.getParagraphs().add(image);
    } catch (Exception e) {
        System.err.println("Error processing image: " + e.getMessage());
    }
}

}

@veeru.sit

It seems that you are experiencing an issue with duplicate images being created when converting an image to a PDF using your provided code. This could be due to how the image is being added to the PDF document.

Here are a few suggestions to help you troubleshoot and resolve the issue:

  1. Check for Multiple Additions: Ensure that the processFile method is not being called multiple times for the same image file. If this method is invoked in a loop or multiple times for the same file, it will add the image repeatedly.

  2. Image Handling: Instead of directly adding the image file path to the Image object, consider using a stream or a buffered image. This can help manage how the image is processed and added to the PDF. For example, you can read the image into a BufferedImage and then add it to the PDF.

  3. Use Unique Identifiers: If you are processing multiple images, ensure that each image is uniquely identified and added to the PDF. This can prevent duplicates if the same image is processed more than once.

Here’s a modified version of your code that demonstrates how to add an image from a BufferedImage to a PDF document, which may help avoid duplication:

class ImageProcessor implements FileProcessor {
    @Override
    public void processFile(Document pdfDocument, File file) {
        System.out.println("Processing Image file: " + file.getName());

        try {
            BufferedImage bufferedImage = ImageIO.read(file);
            Page page = pdfDocument.getPages().add();
            page.setPageSize(11.00 * 72, 8.50 * 72);

            // Add the image to the PDF
            XImage xImage = XImage.fromImage(bufferedImage);
            page.getParagraphs().add(new Image(xImage));

        } catch (Exception e) {
            System.err.println("Error processing image: " + e.getMessage());
        }
    }
}

In this example, the XImage.fromImage(bufferedImage) method is used to create an image object from the buffered image, which can help manage the image addition more effectively.

If the issue persists, consider checking the overall flow of your application to ensure that the processFile method is not being called unnecessarily for the same file.

If you need further assistance, feel free to ask!

getting these issues in the code
XImage xImage = XImage.fromImage(bufferedImage);
page.getParagraphs().add(new Image(xImage));

fromImage(bufferedImage); method is not available in XImage.

also, for new Image(xImage) method
expecting new argument but found one.

@veeru.sit
We are looking into it and will be sharing our feedback with you shortly.

@veeru.sit

Are you testing with the latest version of the API? Can you please share sample image and produced output PDF at your end for our reference? We will investigate in our environment and address the issue accordingly.