Hi, we are converting emails to pdf via aspose.email > aspose.words > aspose.pdf. This works well and we are able to get most of the content displaying very well. However, inline images which are wider than the page are being cut off. The accepted way of shrinking images to fit the page width in aspose.words does not work for us. We can verify that everything seems to work as required, but the resultant pdf does not show the image in the width we specify. Even when shrinking it to a “hardcoded” number like 100 points it still has no effect.
Is there a way to shrink the images to fit width wise?
See code (bold lines have no effect on the resultant pdf):
private static Byte[] PdfEnhancedByteArray(Email email, TimeZoneInfo workTimeZoneInfo, ExchangeListenerCollection.EmailAttachmentOptions attachmentOption)
{
// Uses Aspose.Email, Aspose.Words and Aspose.Pdf
// Convert MSG to MHTML - Use UTF8
Aspose.Email.Mail.MailMessage mailMsg = Aspose.Email.Mail.MailMessage.Load(email.OriginalEmail, new Aspose.Email.Mail.MhtmlLoadOptions { PrefferedTextEncoding = Encoding.UTF8, PreserveTnefAttachments = true });
// Use the Offset to display the correct time
mailMsg.TimeZoneOffset = workTimeZoneInfo.GetUtcOffset(DateTime.Now);
using (var ms = new MemoryStream())
{
try
{
mailMsg.Save(ms, Aspose.Email.Mail.SaveOptions.DefaultMhtml);
}
catch (SystemException ex)
{
Logging.Error("Could not convert email", ex);
throw ex;
}
// Convert MHTML to PDF
// create an instance of Document and load the MTHML from MemoryStream
Aspose.Words.Document document = new Aspose.Words.Document(ms, new Aspose.Words.LoadOptions { LoadFormat = Aspose.Words.LoadFormat.Mhtml });
// Format the document - smaller margins
foreach (Aspose.Words.Section section in document)
{
section.PageSetup.LeftMargin = 20;
section.PageSetup.RightMargin = 20;
section.PageSetup.TopMargin = 20;
section.PageSetup.BottomMargin = 20;
section.PageSetup.HeaderDistance = 10;
section.PageSetup.FooterDistance = 10;
}
// Prevent Tables from being cut off if the are too wide to fit to the page
foreach (Aspose.Words.Tables.Table tableNode in document.GetChildNodes(Aspose.Words.NodeType.Table, true))
{
tableNode.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPercent(100);
}
// TODO: Note, resizing does not work, so we are adding the inline images that are too wide to the pdf - revisit…
// Prevent embedded images from being cut off if the are too wide to fit to the page
foreach (Aspose.Words.Drawing.Shape shape in document.GetChildNodes(Aspose.Words.NodeType.Shape, true))
{
if (shape.ShapeType == Aspose.Words.Drawing.ShapeType.Image)
{
// If images needs to be shrunk then scale to fit (-40 for margins)
if (document.GetPageInfo(0).WidthInPoints < shape.Width)
{
double scale = (document.GetPageInfo(0).WidthInPoints - 40) /
shape.Width;
**shape.Width = document.GetPageInfo(0).WidthInPoints - 40; **
**shape.Height = shape.Height * scale; **
}
}
}
using (var myPdfStream = new MemoryStream())
{
byte[] returnArray;
// save the document to Pdf Stream
document.Save(myPdfStream, new Aspose.Words.Saving.PdfSaveOptions());
// Add Attachments to final PDF
if (attachmentOption == ExchangeListenerCollection.EmailAttachmentOptions.AddToEmailBody && email.Attachments.Count > 0)
{
// Create pdf
var objPdf = new Aspose.Pdf.Document(myPdfStream);
foreach (var attachment in email.Attachments)
{
if (!attachment.IsInlineContent)
{
var fileSpec = new Aspose.Pdf.FileSpecification(attachment.GetStream(), attachment.Name);
objPdf.EmbeddedFiles.Add(fileSpec);
}
else
{
System.Drawing.Image img = System.Drawing.Image.FromStream(attachment.GetStream());
if (img.Width > document.GetPageInfo(0).WidthInPoints - 40)
{
// Images aren’t getting scaled properly - so embed them to the pdf if they are too large
var fileSpec = new Aspose.Pdf.FileSpecification(attachment.GetStream(), attachment.Name);
objPdf.EmbeddedFiles.Add(fileSpec);
}
}
}
objPdf.Save(myPdfStream);
}
return myPdfStream.ToArray();
}
}
}