Cutting off right side after converting email to pdf

i am facing same issue applied this code but not working, could you please help me on this for local testing we are using evaluation one but for higher env we are using licensed one.
below is my code:


 public void generatePdfFromEmail(File inputFile, OutputStream outputStream, String filename, List<Long> pages) {
        try {
            MailMessage mailMessage;
            String extension = FilenameUtils.getExtension(filename);

            if (StringUtils.equalsIgnoreCase(extension, "eml") || StringUtils.equalsIgnoreCase(extension, "emlx")) {
                mailMessage = MailMessage.load(inputFile.getAbsolutePath());
            } else if (StringUtils.equalsIgnoreCase(extension, "msg")) {
                try (InputStream msgInput = new FileInputStream(inputFile)) {
                    MapiMessage mapi = MapiMessage.load(msgInput);
                    mailMessage = mapi.toMailMessage(new MailConversionOptions());
                }
            } else {
                throw new RuntimeException("Unsupported email file format: " + extension);
            }

            File tempMhtml = File.createTempFile("email-convert-", ".mhtml");
            try (OutputStream tempOut = new FileOutputStream(tempMhtml)) {
                mailMessage.save(tempOut, SaveOptions.getDefaultMhtml());
            }

            try (InputStream inputStream = new FileInputStream(tempMhtml)) {
                Document doc = new Document(inputStream);
                pages.add((long) doc.getPageCount());

                double maxContentWidth = 0;

                NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true);
                for (Shape shape : shapes) {
                    if (shape.hasImage()) {
                        maxContentWidth = Math.max(maxContentWidth, shape.getWidth());
                    }
                }

                NodeCollection<Table> tables = doc.getChildNodes(NodeType.TABLE, true);
                for (Table table : tables) {
                    if (table.getPreferredWidth() != null)
                        maxContentWidth = Math.max(maxContentWidth, table.getPreferredWidth().getValue());
                }

                boolean useLandscape = maxContentWidth > 555;

                for (Section section : doc.getSections()) {
                    PageSetup ps = section.getPageSetup();
                    ps.setPaperSize(PaperSize.A4);
                    ps.setOrientation(useLandscape ? Orientation.LANDSCAPE : Orientation.PORTRAIT);
                    ps.setTopMargin(20);
                    ps.setBottomMargin(20);
                    ps.setLeftMargin(20);
                    ps.setRightMargin(20);
                }

                for (Table table : tables) {
                    table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
                }

                for (Shape shape : shapes) {
                    if (!shape.hasImage()) continue;

                    PageSetup ps = shape.getParentParagraph().getParentSection().getPageSetup();
                    double freeWidth = shape.isInline()
                            ? ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin()
                            : ps.getPageWidth();
                    double freeHeight = shape.isInline()
                            ? ps.getPageHeight() - ps.getTopMargin() - ps.getBottomMargin()
                            : ps.getPageHeight();

                    ImageSize size = shape.getImageData().getImageSize();
                    boolean tooBig = size.getWidthPoints() > freeWidth
                            || size.getHeightPoints() > freeHeight
                            || shape.getWidth() > freeWidth
                            || shape.getHeight() > freeHeight;

                    if (tooBig) {
                        double ratio = freeWidth / size.getWidthPoints();
                        if (size.getHeightPoints() * ratio > freeHeight) {
                            ratio = freeHeight / size.getHeightPoints();
                        }
                        shape.setAspectRatioLocked(true);
                        shape.setWidth(size.getWidthPoints() * ratio);
                        shape.setHeight(size.getHeightPoints() * ratio);
                    }
                }
                
                PdfSaveOptions pdfOptions = new PdfSaveOptions();
                pdfOptions.setEmbedFullFonts(true);
                pdfOptions.setUseHighQualityRendering(true);
                pdfOptions.setImageColorSpaceExportMode(PdfImageColorSpaceExportMode.AUTO);
                pdfOptions.setTextCompression(PdfTextCompression.NONE);
                pdfOptions.setJpegQuality(100);

                doc.save(outputStream, pdfOptions);
            } finally {
                MediaUtils.silentlyDeleteTempFile(tempMhtml);
            }

        } catch (Exception ex) {
            throw new RuntimeException("Failed to convert email to PDF: " + ex.getMessage(), ex);
        }
    }

@Debasisha Could you please attach your input, output and expected output documents here for testing? We will check the issue and provide you more information.

sure!, let me share it.
Archive.zip (2.6 MB)

i have added the input and output file, please take a look!

any update @alexey.noskov

@Debasisha Please try using the following code:

com.aspose.email.MailMessage msg = com.aspose.email.MailMessage.load("C:\\Temp\\in.eml");
msg.save("C:\\Temp\\tmp.mhtml", com.aspose.email.SaveOptions.getDefaultMhtml());

Document doc = new Document("C:\\Temp\\tmp.mhtml");
doc.getFirstSection().getPageSetup().setOrientation(Orientation.LANDSCAPE);

// Determine the maximum table width using LayoutEnumerator.
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

double maxTableWidth = 0;
for (Section s : doc.getSections())
{
    for (Table t : s.getBody().getTables())
    {
        enumerator.setCurrent(collector.getEntity(t.getFirstRow().getFirstCell().getFirstParagraph()));
        while (enumerator.getType() != LayoutEntityType.ROW)
            enumerator.moveParent();

        maxTableWidth = Math.max(maxTableWidth, enumerator.getRectangle().getWidth());
    }
}
// Calculate maximum image width.
double maxShapeWidth = 0;
Iterable<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true);
for (Shape s : shapes)
{
    enumerator.setCurrent(collector.getEntity(s));
    maxShapeWidth = Math.max(maxShapeWidth, enumerator.getRectangle().getWidth());
}

PageSetup ps = doc.getFirstSection().getPageSetup();
double pageWidth = ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin();

double maxContentWidth = Math.max(maxShapeWidth, maxTableWidth);
if (pageWidth < maxContentWidth)
    ps.setPageWidth(maxContentWidth + ps.getLeftMargin() + ps.getRightMargin());

// Update page layout is required since LayoutCollector and LayoutEnumerator were used.
doc.updatePageLayout();
doc.save("C:\\Temp\\out.pdf");

out.pdf (443.3 KB)

Thank you so much!! @alexey.noskov
it is working now.

@Debasisha,

Thank you for sharing your feedback.

It is nice to hear that the suggested code snippet meets your requirements. Should you have any further questions or comments, please don’t hesitate to reach out to us at any time.