Issues converting word document to PDF

Hello, we are trying to convert an incoming msg file to pdf. We are using aspose-email 23.8 and aspose-words 23.8. We have two questions:

  1. If an incoming email has a wide image or table, the wide part gets cut off. Is there a way to ensure that the email size is scaled to the resulting PDF correctly?
  2. While we have mostly been successful in retaining the original formatting of the email, occasionally attributes can be lost. Is there a specific setting in aspose that has to be enabled to ensure the email (1) fits to the target pdf page size and (2) retains all data in it?

@drymers You can try using LayoutCollector and LayoutEnumerator to calculate actual table width and adjust page width accordingly:

Aspose.Email.MailMessage msg = Aspose.Email.MailMessage.Load(@"C:\Temp\in.msg");
msg.Save(@"C:\Temp\tmp.mhtml", Aspose.Email.SaveOptions.DefaultMhtml);

Document doc = new Document(@"C:\Temp\tmp.mhtml");
doc.FirstSection.PageSetup.Orientation = Orientation.Landscape;
            
// Determine the maximum table width using LayoutEnumerator.
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

double maxTableWidth = 0;
foreach (Section s in doc.Sections)
{
    foreach (Table t in s.Body.Tables)
    {
        enumerator.Current = collector.GetEntity(t.FirstRow.FirstCell.FirstParagraph);
        while (enumerator.Type != LayoutEntityType.Row)
            enumerator.MoveParent();

        maxTableWidth = System.Math.Max(maxTableWidth, enumerator.Rectangle.Width);
    }
}

PageSetup ps = doc.FirstSection.PageSetup;
double pageWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;

if (pageWidth < maxTableWidth)
    ps.PageWidth = maxTableWidth + ps.LeftMargin + ps.RightMargin;

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

With images the things is even simple, since you can get shape width without building document layout. But if you like, you can use the similar approach.

Hello, we are using aspose java. Would you mind converting the code sample to Java for us? Thank you so much!

Also, for context the wide image in the email is still being cut off. Is there a way I can resize images in the document to fit to the page with this method? I have attached the image here. wideImageInEmail.zip (1.3 MB)

@drymers Here is java version of the code. Also I have added processing of shapes in the code:

com.aspose.email.MailMessage msg = com.aspose.email.MailMessage.load("C:\\Temp\\in.msg");
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");

If the problem still persist, please attach the problematic MSG file here for testing.

Thank you so much @alexey.noskov. Follow up question - when we convert an email with attachments, those attachments tend to be shrunk down to be non recognizable. Is there a way that we can retain the original size of the attachment and/or fit it within the page?

@drymers Could you please attach the problematic input MSG file and output PDF produced on your side? We will check the issue and provide you more information.