Not able to get the borders on the TIF

@asish312,

I request you to please observe this image representing the source MSG and generated MHTML files. Both doesn’t have any borders. Aspose.Email has rendered the output MHTML similar to that of source MSG. If there were any borders in source MSG but missing in exported MHTML, we could have considered this to be an issue in API. I hope the shared elaboration will be helpful.

1 Like

Yes I agree @mudassir.fayyaz
There is no border in the email.But here point is that,as per my requirement I need emailbody border in the output through code change.
That’s why before converting into MHTML/TIF/PDF I am adding this line,(I think we missed this point,Please check the code once again which i shared earlier)

//Trying to Put Border to Body Content with Style

string OldString = “<body lang=” + @""“EN-US”"" + " link=" + @"""#0563C1""" + " vlink=" + @"""#954F72""" + “>”;
string NewString = “<body lang=” + @""“EN-US”"" + " link=" + @"""#0563C1""" + " vlink=" + @"""#954F72""" + " style=" + @""“border: 1px solid black; white-space: nowrap;”"" + “>”;
msg.HtmlBody = msg.HtmlBody.Replace(OldString,NewString);

//or
  //Trying to add Border to Div Section in the Email
                msg.HtmlBody = msg.HtmlBody.Replace("<div class=" + @"""WordSection1""", "<div class=" + @"""WordSection1""" + "style=" + @"""border: 5px solid black; white-space: nowrap;""");

If you see this image.png (15.3 KB)

@asish312

Thanks for sharing the detail. Please note that Aspose.Words mimics the behavior of MS Word. If you open the HTML (msg.HtmlBody) in MS Word, you will get the same output. MS Word sets the border of page.

1 Like

Okay.I can see a page border to whole page while opening via word.(Override the border of table and email body)Well do you have any idea how can I put border to my mail body @tahir.manzoor

I am trying like this

//Trying to Put Border to Body Content with Style

string OldString = “&lt;body lang=” + @""“EN-US”"" + " link=" + @"""#0563C1""" + " vlink=" + @"""#954F72""" + “&gt;”;
string NewString = “&lt;body lang=” + @""“EN-US”"" + " link=" + @"""#0563C1""" + " vlink=" + @"""#954F72""" + " style=" + @""“border: 1px solid black; white-space: nowrap;”"" + “&gt;”;
msg.HtmlBody = msg.HtmlBody.Replace(OldString,NewString);

//or
//Trying to add Border to Div Section in the Email
msg.HtmlBody = msg.HtmlBody.Replace("&lt;div class=" + @""“WordSection1"”", “&lt;div class=” + @""“WordSection1"”" + “style=” + @""“border: 5px solid black; white-space: nowrap;”"");

Do you have any suggestions?? to get border from email body content?
My expectation would be following thisExpected_Output.zip (75.8 KB)

@asish312

Thanks for your inquiry. We are investigating this case and will get back to you soon.

1 Like

@asish312

In your case, we suggest you following solution.

  1. Create an empty document (Aspose.Words.Document).
  2. Insert a table with one row and one cell into document. Insert the message header (HTML) into this table. Set the table’s width as 90% and alignment as center.
  3. Insert another table with one row and one cell into document. Insert the message body (HTML) into this table. Set the table’s width as 90% and alignment as center.
  4. Set the width of tables in message body.
  5. Set the page border according to your requirement.
  6. Export the document to TIFF file format.

Below is the code example for above solution. We have attached the output TIFF file with this post for your kind reference. output TIFF.zip (72.5 KB)

Hope this helps you.

Document document = new Document();
DocumentBuilder documentBuilder = new DocumentBuilder(document);
Table table = documentBuilder.StartTable();
Cell cellheader = documentBuilder.InsertCell();
documentBuilder.InsertHtml(header);
documentBuilder.EndRow();
documentBuilder.EndTable();
table.PreferredWidth = PreferredWidth.FromPercent(90);
table.SetBorders(LineStyle.None, 0, System.Drawing.Color.Empty);
table.Alignment = TableAlignment.Center;
documentBuilder.Writeln();

Table tablebody = documentBuilder.StartTable();
Cell cell = documentBuilder.InsertCell();
documentBuilder.InsertHtml(msg.HtmlBody);
documentBuilder.EndRow();
documentBuilder.EndTable();
tablebody.PreferredWidth = PreferredWidth.FromPercent(90);
tablebody.SetBorders(LineStyle.None, 0, System.Drawing.Color.Empty);
tablebody.Alignment = TableAlignment.Center;

foreach (Table tbl in tablebody.GetChildNodes(NodeType.Table, true))
{
    tbl.PreferredWidth = PreferredWidth.FromPercent(80);
}

document.FirstSection.PageSetup.LeftMargin = 10;
document.FirstSection.PageSetup.RightMargin = 10;
document.FirstSection.PageSetup.TopMargin = 10;
document.FirstSection.PageSetup.BottomMargin = 10;
document.FirstSection.PageSetup.Borders.LineStyle = LineStyle.Double;
document.FirstSection.PageSetup.Borders.LineWidth = 2;
                 
document.UpdatePageLayout();
document.Save(sourceFile + "19.2.tiff");
1 Like

Perfect @tahir.manzoor :slight_smile: Thank you @mudassir.fayyaz as well.
Ausm work people. Appreciates your time and effort.
Onemore last question Is that possible to display the embedded images coming from email body ?
Now except images/line/textbox,Tables and Text coming perfectly fine
I can see the embedded images in mhtm,However loosing in Tiff file conversion.
I have tried:
ref:
https://forum.aspose.com/t/converting-word-to-html-with-images-embedded/37205/2

Aspose.Words.Saving.HtmlSaveOptions saveOptions = new Aspose.Words.Saving.HtmlSaveOptions();
saveOptions.setExportImagesAsBase64(true);  //This shows syntactical error
document.Save(sourceFile + "19.2.tiff");

I am using 11.11.0.0 aspose word dll.If you could help me without upgrading the dll it would be a g8 favor
Please let me know any inputs

@asish312

Thanks for your inquiry.

The HtmlSaveOptions class can be used to specify additional options when saving a document into the HTML, MHTML, or EPUB. For addition options for image, you can use ImageSaveOptions class. In your case, you do not need to use these classes.

Could you please share your MSG file that contains the images here for testing? We will investigate the issue and provide you more information on it.

1 Like

Sure. Here you go I have attached the msg file containing images and table.
TestSubject.zip (29.9 KB)

@asish312

Thanks for sharing the detail. Please use the following code example to achieve your requirement.

string header = "Hola this a table with border<table border='2'" + "style=" + @"""border: 5px solid black; white-space: nowrap;""" + "><tr><td>From:</td> <td>Asish.Panda@jsjs.com</td></tr><tr><td>Sent:</td><td>SomeDate</td></tr><tr><td>To:</td><td>pdei@s,s.com</td></tr><tr><td>Subject:</td><td>Hello this is my subject</td></tr></table>";

Aspose.Email.MailMessage message = Aspose.Email.MailMessage.Load(MyDir + "TestSubject.msg");
Aspose.Email.HtmlSaveOptions options = Aspose.Email.SaveOptions.DefaultHtml;
options.EmbedResources = true;
message.Save(MyDir + "input.html", options);
                

Document document = new Document();
DocumentBuilder documentBuilder = new DocumentBuilder(document);
Table table = documentBuilder.StartTable();
Cell cellheader = documentBuilder.InsertCell();
documentBuilder.InsertHtml(header);
documentBuilder.EndRow();
documentBuilder.EndTable();
table.PreferredWidth = PreferredWidth.FromPercent(90);
table.SetBorders(LineStyle.None, 0, System.Drawing.Color.Empty);
table.Alignment = TableAlignment.Center;
documentBuilder.Writeln();

Table tablebody = documentBuilder.StartTable();
Cell cell = documentBuilder.InsertCell();
documentBuilder.InsertHtml(File.ReadAllText(MyDir + "input.html"));
documentBuilder.EndRow();
documentBuilder.EndTable();
tablebody.PreferredWidth = PreferredWidth.FromPercent(90);
tablebody.SetBorders(LineStyle.None, 0, System.Drawing.Color.Empty);
tablebody.Alignment = TableAlignment.Center;

foreach (Table tbl in tablebody.GetChildNodes(NodeType.Table, true))
{
    tbl.PreferredWidth = PreferredWidth.FromPercent(80);
}

foreach (Shape shape in document.GetChildNodes(NodeType.Shape, true))
    ResizeImageToPageSize(shape);

document.FirstSection.PageSetup.LeftMargin = 10;
document.FirstSection.PageSetup.RightMargin = 10;
document.FirstSection.PageSetup.TopMargin = 10;
document.FirstSection.PageSetup.BottomMargin = 10;
document.FirstSection.PageSetup.Borders.LineStyle = LineStyle.Double;
document.FirstSection.PageSetup.Borders.LineWidth = 2;

document.UpdatePageLayout();
document.Save(MyDir + "19.2.tiff"); 

public static void ResizeImageToPageSize(Shape image)
{
    // Return if this shape is not an image.
    if (!image.HasImage)
        return;

    // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account.
    PageSetup ps = image.ParentParagraph.ParentSection.PageSetup;
    double freePageWidth = image.IsInline ? ps.PageWidth - ps.LeftMargin - ps.RightMargin : ps.PageWidth;
    double freePageHeight = image.IsInline ? ps.PageHeight - ps.TopMargin - ps.BottomMargin : ps.PageHeight;


    ImageSize size = image.ImageData.ImageSize;
    Boolean exceedsMaxPageSize = size.WidthPoints > freePageWidth || size.HeightPoints > freePageHeight
        || image.Width > freePageWidth || image.Height > freePageHeight;

    if (exceedsMaxPageSize)
    {
        // Set the new size.
        image.Width = size.WidthPoints * .80;
        image.Height = size.HeightPoints * .80;
    }
}
1 Like

You nailed it @tahir.manzoor Perfect solution.I can able to see the image in my test console (Aspose Email 18.12 version) application but when I run the same thing in my real solution, I got syntactical error over this line
Aspose.Email.HtmlSaveOptions options = Aspose.Email.SaveOptions.DefaultHtml;

Well I compared the changes between two application and I afraid due to old dll version using for word as well as email. So I suggested our team to upgrade.
But as of now I would go with table format which was suggested by you previously. But I am stuck with one issue when table columns i.e width is more,table border gets overlapped.
So thing I am trying here when table border content is more I would like to use FitContent method else how it displays that’s perfect.

I want to use below method when content is more

  public static void FitTableToPageWidth(WORDS.Tables.Table table1)

        {
            //Get the page settings from the section where the table occurs, as each section can have different page settings.
            WORDS.Section section = (WORDS.Section)table1.GetAncestor(WORDS.NodeType.Section);
            //First run simply gets the table size (the widest row). This is used to calculate the ratio below instead of just each row length
            //as the last cell in one row could be shorter than the last cell in the other row. This will preserve these different sizes when fitting.
            double tableWidth = 0;
            foreach (WORDS.Tables.Row row in table1.Rows)
            {
                double rowWidth = 0;
                foreach (WORDS.Tables.Cell cell in row.Cells)
                {
                    rowWidth += cell.CellFormat.Width;
                    cell.CellFormat.FitText = true;
                }
                //If this row is larger than previous set this width as the longest row.
                if (rowWidth > tableWidth)
                    tableWidth = rowWidth;
            }
            //Calculate the width of the page
            double pageWidth = section.PageSetup.PageWidth -(section.PageSetup.LeftMargin + section.PageSetup.RightMargin);
            //In the second run set each cell in the row proportionally to the width of the page
            foreach (WORDS.Tables.Row row in table1.Rows)
            {
                foreach (WORDS.Tables.Cell cell in row.Cells)
                {
                    //Calculate the ratio of each cell to the row width and then translate this ratio to the page width.
                    double cellRatio = cell.CellFormat.Width / tableWidth;

                    cell.CellFormat.Width = (cellRatio * pageWidth);
                }
            }
        }

Any help would be appreciated.

@tahir.manzoor
For this solution I am having issue in the attachmentError.zip (185.4 KB)

https://forum.aspose.com/t/not-able-to-get-the-borders-on-the-tif/191292/22?u=asish312

So

  1. thing I am trying here when table border content is more then maximum page width, I would like to use FitContent method else how original table width that should display
  2. In Page3 in Tiff Image getting a blank page.Please let me know how can remove if empty. Sample Images attached

Thanks again for the good work.

@asish312

Thanks for your inquiry. Please set the table’s width using following code example.

Document document = new Document(MyDir + "input.html"); 

foreach (Table tbl in document.GetChildNodes(NodeType.Table, true))
{
    tbl.PreferredWidth = PreferredWidth.FromPercent(80);
}

document.Save(MyDir + "19.2.tiff");
1 Like

Tried but no luck. :frowning:
Content coming out of the border.
https://forum.aspose.com/uploads/default/23871

Please process this check.msg and rightside border not coming as shown in the attached tiff file.

Note:

  1. documentBuilder.InsertHtml(header +"<br>"+ File.ReadAllText(sourceFile + "input.html"));-----making performance slow
  2. Aspose.Email.HtmlSaveOptions options = Aspose.Email.SaveOptions.DefaultHtml;----------------->Does not support
    So saving into HTML using
 MemoryStream msgStream123 = new MemoryStream();
 msg.Save(msgStream123);
 WORDS.Document msgDocumentNew = new WORDS.Document(msgStream123);
 msgDocumentNew.Save(sourceFile + "input.html");

3.Even if I use borders are not working :frowning:
Kindly help me replicating with previous solution

  1. Create an empty document (Aspose.Words.Document).
  2. Insert a table with one row and one cell into document. Insert the message header (HTML) into this table. Set the table’s width as 90% and alignment as center.
  3. Insert another table with one row and one cell into document. Insert the message body (HTML) into this table. Set the table’s width as 90% and alignment as center.
  4. Set the width of tables in message body.
  5. Set the page border according to your requirement.
  6. Export the document to TIFF file format.

This fits nice but when content is more does not work

@asish312

Thanks for your inquiry. In case you are using old version of Aspose products, please use the latest version of Aspose.Words for .NET 19.2 and Aspose.Email for .NET 19.1.1.

We have not found any issue with output TIFF file while using latest version of Aspose.Words and Aspose.Email. We have attached the output TIFF file with this post for your kind reference. output document.zip (155.1 KB)

If the width of tables and images in MSG file is greater than page size, we suggest you please increase the page’s width using PageSetup.PageWidth property.

1 Like

Okay. Thank you for the response and support. Would you mind sending me the code again for generating above .tiff image please?
https://forum.aspose.com/uploads/default/23906

Thanks again :slight_smile:

@asish312

Thanks for your inquiry. The TIFF file was generated by using the same code shared here:
https://forum.aspose.com/t/not-able-to-get-the-borders-on-the-tif/191292/26?u=tahir.manzoor

Please use the latest version of Aspose.Words for .NET 19.2 and Aspose.Email for .NET 19.1.1 to get the desired output.

1 Like

OKay…
documentBuilder.InsertHtml(File.ReadAllText(MyDir + “input.html”));----->takes much time to process

Well, Thank you @tahir.manzoor for all the support and your time.

Keep up the good work. :slight_smile:

@asish312

Please note that performance and memory usage all depend on complexity and size of the documents you are generating.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

1 Like