Unexpected location of horizontal line when converting from email to Word

Hello there,

I have the next situation. I’m using Aspose.Email ver 5.2.0.0 and Aspose.Words ver. 15.3.0.0 - for .NET, of course. I have the next pipeline: my program obtains an input email message as an instance of “Aspose.Email.Mail.MailMessage” class. Then I convert it to the “Aspose.Words.Document”. Then using a “Aspose.Words.DocumentBuilder” I add some content to the beginning of this document - header with “From”, “To” and “Subject” fields, in particular. And finally I save all of this to the PDF. Source code is below.

My problem is the next: I want to separate my own header from the body of the email message by using a horizontal rule. But I cannot do this with “ParagraphFormat.Borders.Bottom.LineStyle” - seescreeshot.

Horizontal line divides parts of the body instead of dividing the header and body. I’ve used advices from this thread: Inserting Horizontal Rule in document.

I don’t know - is this a bug or it is a feature. Nevertheless, it is an important to know how to make it work properly.

Source code is below, input MSG and output PDF are attached inside the ZIP file.

String full_path = path + DocumentName;
Aspose.Email.Mail.MailMessage document = MailMessage.Load(full_path);
Aspose.Words.Document wordsDocument;
using (MemoryStream memoryStream = new MemoryStream())
{
document.Save(memoryStream, MailMessageSaveType.MHtmlFormat, MailMessageSaveOptions.None);
wordsDocument = new Aspose.Words.Document(memoryStream, new Aspose.Words.LoadOptions(
LoadFormat.Mhtml, null, null));
DocumentBuilder builder = new DocumentBuilder(wordsDocument);

// Create custom email header
// Specify font formatting
Aspose.Words.Font font = builder.Font;
font.Size = 12;
font.Color = System.Drawing.Color.Black;

// Specify paragraph formatting
ParagraphFormat paragraphFormat = builder.ParagraphFormat;
paragraphFormat.FirstLineIndent = 0;

font.Bold = true;
builder.Write("From: ");
MailAddress fromAddress = document.From;
if (fromAddress != null)
{
font.Bold = false;
builder.Writeln(GetAddressWithName(fromAddress));
}
List adresses = new List();

if (document.To != null && document.To.Count > 0)
{
font.Bold = true;
builder.Write("To: “);
font.Bold = false;
foreach (MailAddress address in document.To)
{
adresses.Add(GetAddressWithName(address));
}
builder.Writeln(String.Join(”; ", adresses));
}

if (document.CC != null && document.CC.Count > 0)
{
font.Bold = true;
builder.Write("Cc: “);
font.Bold = false;
adresses.Clear();
foreach (MailAddress address in document.CC)
{
adresses.Add(GetAddressWithName(address));
}
builder.Writeln(String.Join(”; ", adresses));
}

if (document.Date != default(DateTime))
{
font.Bold = true;
builder.Write(String.Format("Sent: "));
font.Bold = false;
builder.Writeln(document.Date.ToString());
}

if (document.Subject != null)
{
font.Bold = true;
builder.Write(String.Format("Subject: "));
font.Bold = false;
builder.Writeln(document.Subject);
}

if (document.Attachments != null && document.Attachments.Any())
{
font.Bold = true;
builder.Write(String.Format("Attachment{0}: ", document.Attachments.Count == 1 ? “” : “s”));
StringBuilder allAttachmentsNames = new StringBuilder();
foreach (Aspose.Email.Mail.Attachment oneAttachment in document.Attachments)
{
allAttachmentsNames.Append(oneAttachment.Name + “; “);
}
allAttachmentsNames.Remove(allAttachmentsNames.Length - 2, 2);
font.Bold = false;
builder.Writeln(allAttachmentsNames.ToString());

// this way of adding a horizontal line is preferrable, but not working
builder.CurrentParagraph.ParagraphFormat.Borders.Bottom.LineStyle = LineStyle.Single;
builder.CurrentParagraph.ParagraphFormat.Borders.Bottom.LineWidth = 1.5;
builder.CurrentParagraph.ParagraphFormat.Borders.Bottom.Color = System.Drawing.Color.Gray;
builder.Writeln(””);
}
}
wordsDocument.Save(path + “email2pdf_output.pdf”, Aspose.Words.SaveFormat.Pdf);

Thanks.

Hi,
We are investigating your code and will update you soon.
Best Regards,

Hi there,
It is almost 1 month from the original post, can I obtain some info about this?
Thanks.
Denis Gvardionov

Hi,
The output is correct. There are three paragraphs in your document before adding any contents and your pointer remains on the first paragraph because you did not add any paragraph before adding the contents. Your code inserts a horizontal line at the bottom of first paragraph as it is shown in the output.
If you want to insert a horizontal line after the new contents you have added, you first need to insert a paragraph at the start of the document and then move to that paragraph before adding the contents.
Please check the following modified part of your code. Remaining code will remain the same.

using (MemoryStream memoryStream = new MemoryStream())
{
    document.Save(memoryStream, MailMessageSaveType.MHtmlFormat, MailMessageSaveOptions.None);
    wordsDocument = new Aspose.Words.Document(memoryStream, new Aspose.Words.LoadOptions(
    LoadFormat.Mhtml, null, null));
    DocumentBuilder builder = new DocumentBuilder(wordsDocument);

    Paragraph para = new Paragraph(wordsDocument);
    wordsDocument.FirstSection.Body.Paragraphs.Insert(0, para);
    builder.MoveToParagraph(0, 0);
}

Best Regards,