When I save the document using SaveFormat.PDF the tables inside the document does not come

When I save using SaveFormat.DOCX it works properly.

aggParams.Reports is my parameter to method
and I have some Lists where I have some table details.
So, I am creating a table with StringBuilder and then inserting the html into the document

Aspose.Words.Document doc = new Aspose.Words.Document(aggParams.TemplatePath);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

StringBuilder tableBuilder = new StringBuilder();
IEnumerable<ESGTableProperties> table = tables.Where(s => s.DataPointId == agg.DataPointId);
int row = table.Select(s => s.RowIndex).Max();
int col = table.Select(s => s.ColIndex).Max();
var groupedTable = table.GroupBy(s => s.RowIndex);
tableBuilder.Append($"<table style='border: 3px solid #e8e8e8;border-collapse: collapse; width:100%';>");

foreach (var trow in groupedTable)
{
    List<ESGTableProperties> rows = trow.ToList();
    var tr = trow.FirstOrDefault().IsHeader == true ? "<tr style='background-color:#e8e8e8;text-align:center;'>" : "<tr style='text-align:center;'>";
    tableBuilder.Append(tr);
    foreach (ESGTableProperties tcol in trow)
    {
        DataPointFactDetail fact = agg.TableRows.Find(s => s.RowIndex == tcol.RowIndex && s.ColIndex == tcol.ColIndex);
        var cellValue = fact == null ? tcol.Name : fact.FactValue;
        if (tcol.IsHeader)
        {
            tableBuilder.Append($"<th style='border: 3px solid #e8e8e8;'  colspan='{tcol.ColSpan}' rowspan='{tcol.RowSpan}'>{cellValue}</td>");
        }
        else
        {
            tableBuilder.Append($"<td style='border: 3px solid #e8e8e8;' colspan='{tcol.ColSpan}' rowspan='{tcol.RowSpan}'>{cellValue}</td>");
        }
    }
    tableBuilder.Append("</tr>");
}

tableBuilder.Append("</table>");
htmlToInsert = tableBuilder.ToString();
builder.InsertHtml(htmlToInsert);

doc.UpdateFields();

if (aggParams.Reports.Contains(ESGReportFormats.WORD))
{
    using (MemoryStream ms = new MemoryStream())
    {
        doc.Save(ms, Aspose.Words.SaveFormat.Docx);
        bytes = ms.ToArray();
        File.WriteAllBytes(@"D:\\Aggregated-Output.docx", bytes);
    }
}
else if (aggParams.Reports.Contains(ESGReportFormats.PDF))
{
    using (MemoryStream ms = new MemoryStream())
    {
        doc.Save(ms, Aspose.Words.SaveFormat.Pdf);
        bytes = ms.ToArray();
        File.WriteAllBytes(@"D:\\Aggregated-Output.pdf", bytes);
    }
}

This is my Save as DOCX output
Aggregated-Report (4).docx (17.4 KB)

and this is my save as PDF output
Aggregated-Report (2).pdf (77.7 KB)

Note : When I open Word document by Exporting it as pdf, The tables are visible.

Expected Outcome: The tables should be visible.

@Kunal8460

It seems that you are experiencing issues with tables not appearing in the PDF output when saving your document using Aspose.Words. This can happen due to various reasons related to how the HTML is being rendered or how the PDF conversion settings are configured.

To ensure that tables are correctly rendered in the PDF output, you can try the following steps:

  1. Use PdfSaveOptions: When saving the document as a PDF, you can specify various options using the PdfSaveOptions class. This allows you to control how the document is rendered. For example, you can enable the export of document structure, which may help in preserving the layout of tables.

    Here’s an example of how to set up PdfSaveOptions:

    PdfSaveOptions saveOptions = new PdfSaveOptions
    {
        ExportDocumentStructure = true // This helps in preserving the structure of the document
    };
    doc.Save("output.pdf", saveOptions);
    
  2. Check HTML Validity: Ensure that the HTML you are generating for the table is valid. Sometimes, malformed HTML can lead to rendering issues. For instance, make sure that all tags are properly closed and that the structure of the table is correct.

  3. Test with Simple HTML: As a troubleshooting step, try inserting a simple HTML table directly into the document to see if it renders correctly in the PDF. This can help you determine if the issue lies with the generated HTML or with the PDF conversion process itself.

  4. Update Aspose.Words: Ensure that you are using the latest version of Aspose.Words, as updates often include bug fixes and improvements related to document rendering and conversion.

  5. Review Documentation: For more detailed information on rendering options and how to handle specific cases, you can refer to the Aspose documentation on converting documents to PDF. This documentation provides insights into various settings that can affect the output.

By following these steps, you should be able to resolve the issue with tables not appearing in the PDF output. If the problem persists, consider sharing a minimal reproducible example or checking the Aspose support forums for similar issues.

Sources:
[1]: Options When Converting to PDF|Aspose.Words for .NET - Aspose Documentation

@Kunal8460 Could you please save htmlToInsert = tableBuilder.ToString(); string into the file and provide HTML here for testing? We do not have your data and cannot generate the test HTML like yours. Also, please attach your input (template) document here. We will check the issue and provide you more information.

Aggregated-Report (5).docx (17.5 KB)

consider this file

@Kunal8460 Thank you for additional information, but still, could you please provide your input HTML and input template here for testing?

HtmlStrings.docx (19.4 KB)

I hope this works.

@Kunal8460 Thank you for additional information. Here are output documents produced on my side using the following simple code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml(File.ReadAllText(@"C:\Temp\in.html"));
doc.Save(@"C:\Temp\out.docx");
doc.Save(@"C:\Temp\out.pdf");

out.docx (13.6 KB)
out.pdf (75.7 KB)

As I can see the tables look the same in output DOCX and PDF documents. But the outputs do not match the document you have provided in your initial post. So, could you please create a simple console application that will allow us to reproduce the problematic output on our side?

This is working in a way but what I actually want to do is this

using (MemoryStream ms = new MemoryStream())
{
     doc.Save(ms, Aspose.Words.SaveFormat.Pdf);
     bytes = ms.ToArray();
}

I am returning this bytes to front-end to download the document
So, when I download the document or simply write these bytes to Local file system.
The pdf comes in a corrupted way.

@Kunal8460 Could you please zip and attach the corrupted PDF? Have you tried writing bytes directly the file (for testing purposes)? Perhaps the problem is in the code that sends the file to the front-end.