Words for .NET output questions

doc-gen format error.zip (2.1 MB)

Please see the questions below from the client and help, thank you !

We are currently experiencing issues with Aspose when converting Excel and Word files to PDF, specifically differences in styling and layout distortion.

Please refer to the two attached files within the Excel and Word data for comparison:

  • OutPut_Expected.png: This shows the expected output.
  • OutPut_Aspose.png: This shows the output generated by Aspose.

We expect the files converted by Aspose to match the format and styling of OutPut_Expected.png identically. We have also included the conversion code snippet for your reference.

@saphira_linksoft_com_tw

Cause

The differences in styling and layout distortion when converting Excel and Word files to PDF using Aspose can arise from various factors, including font availability, document structure, and specific formatting settings in the source files.

Solution

To address these issues, consider the following steps:

  1. Check Font Availability: Ensure that all fonts used in the original documents are installed on the server where the conversion is taking place. Missing fonts can lead to unexpected styling changes.

  2. Use the Latest Version: Make sure you are using the latest version of Aspose.Words and Aspose.Cells. Updates often include bug fixes and improvements related to document conversion.

  3. Adjust Layout Options: Review and adjust the layout options in your conversion code. For example, you can set the PdfSaveOptions to preserve the layout more accurately.

  4. Test with Simplified Documents: If possible, create simplified versions of your documents to isolate the issue. This can help identify specific elements causing the distortion.

Code

Here’s a sample code snippet to convert a Word document to PDF while preserving layout:

Document doc = new Document("input.docx");
PdfSaveOptions options = new PdfSaveOptions();
options.SaveFormat = SaveFormat.Pdf;
options.PrettyFormat = true; // Optional: for better readability
doc.Save("output.pdf", options);

For Excel files, you can use:

Workbook workbook = new Workbook("input.xlsx");
workbook.Save("output.pdf", SaveFormat.Pdf);

Next Steps

  1. Implement the suggested changes and test the conversion again.
  2. If issues persist, please share the specific code snippet you are using for conversion, along with any additional details about the documents.
  3. Review the output files closely to identify any specific elements that are not rendering as expected.

Please proceed with these steps, and let me know if you need further assistance!

@saphira_linksoft_com_tw I cannot reproduce the problem on my side using the latest 25.7 version of Aspose.Words. Here is PDF document produced by MS Word on my side:
ms.pdf (155.0 KB)

Here is the output produced by Aspose.Words and the following simple code:

Document doc = new Document(@"C:\Temp\in.docx");
doc.Save(@"C:\Temp\out.pdf");

out.pdf (62.1 KB)

Though, the output differs from MS Word output a bit. To get the output closer to MS Word it is required to enable open type features. Aspose.Words.Shaping.Harfbuzz package provides support for OpenType features in Aspose.Words using the HarfBuzz text shaping engine. You should enabling open type features to get the expected result. To achieve this you should add reference to Aspose.Words Shaping Harfbuzz plugin and use the following code to convert your document:

Document doc = new Document(@"C:\Temp\in.docx");
doc.LayoutOptions.TextShaperFactory = Aspose.Words.Shaping.HarfBuzz.HarfBuzzTextShaperFactory.Instance;
doc.Save(@"C:\Temp\out_HarfBuzz.pdf");

out_HarfBuzz.pdf (68.2 KB)

In addition, the problem can occur on your side because fronts used in your original document are not available in the environment where the document is converted to PDF. The fonts are required to build document layout. If Aspose.Words cannot find the font used in the document, the font is substituted . This might lead into fonts mismatch and document layout differences due to the different fonts metrics. You can implement IWarningCallback to get notifications when font substitution is performed.
Please see our documentation to learn where Aspose.Words looks for fonts:
https://docs.aspose.com/words/net/specifying-truetype-fonts-location/

I will move the topic into Aspose.Total category, My colleagues from Aspose.Cells will answer the question about Excel to PDF conversion.

@saphira_linksoft_com_tw,

I reviewed the issue using the sample Excel file you provided regarding the Aspose.Cells functionality for Excel to PDF conversion. It appears that the “Rows to repeat at top” option (found under the “Page Setup | Sheet” tab) is configured for a specific range of rows. As a result, some title rows are being repeated on the second page. Additionally, certain content using the “Leelawadee UI” font in some cells is not being displayed completely or properly. This occurs because the relevant rows and columns are set to “automatic.” When the file is opened in MS Excel, the auto-fit rows/columns option is applied to ensure the content is fully displayed. To address these issues, kindly consider revising the relevant code segment:
i.e.,

....
var ws = workbook.Worksheets[0];
ws.PageSetup.PrintGridlines = true;
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

foreach (var pair in data)
{
        ws.Cells[pair.Key].PutValue(pair.Value ?? "");
}

with:

....
var ws = workbook.Worksheets[0];
ws.PageSetup.PrintGridlines = true;
ws.PageSetup.PrintTitleRows = null;
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

foreach (var pair in data)
{
      ws.Cells[pair.Key].PutValue(pair.Value ?? "");
}
ws.AutoFitColumns();    
ws.AutoFitRows();

Kindly add the suggested lines of code to your snippet, and it should work as expected. I tested it, and the output PDF file (attached) appears to be fine.
out1.pdf (59.2 KB)

Let us know if you still find any issue with Aspose.Cells APIs.