Complex Field Code Rendering Issue in DOCX to PNG Conversion

Issue:
Complex field codes render incorrectly when converting DOCX to PNG, causing complete layout disruption.

Attempted Solutions:

  • Called UpdateFields()
  • Tried different MetafileRenderingMode settings

Code:

using Aspose.Words;
using Aspose.Words.Saving;

new License().SetLicense(@"licensePath");
var document = new Document($@"inputDocxPath");
// document.UpdateFields();
var saveOptions = new ImageSaveOptions(SaveFormat.Png)
{
    // MetafileRenderingOptions =
    // {
    //     RenderingMode = MetafileRenderingMode.VectorWithFallback,
    //     EmulateRasterOperations = false
    // }
};
document.Save($@"outputPngPath", saveOptions);

Files Attached:
Input Docx:
input.docx (15.4 KB)

Aspose Result:

Expected Result:

Environment:

  • .NET Framework 4.8
  • Aspose.Words 25.9.0

@SzuChiChen

It seems you’re experiencing issues with complex field codes rendering incorrectly during the conversion of a DOCX document to PNG. Here are a few suggestions to help resolve this:

  1. Ensure Fields are Updated: Make sure to call UpdateFields() before saving the document to ensure that all fields are updated correctly.

  2. Check Metafile Rendering Options: Experiment with different MetafileRenderingMode settings. For example, try using MetafileRenderingMode.Vector or MetafileRenderingMode.Raster to see if it improves the output.

  3. Use a Different Save Format: As a workaround, consider saving the document as a PDF first, then convert the PDF to PNG. This can sometimes yield better results for complex layouts.

Here’s an updated version of your code with these suggestions:

using Aspose.Words;
using Aspose.Words.Saving;

new License().SetLicense(@"licensePath");
var document = new Document(@"inputDocxPath");
document.UpdateFields(); // Ensure fields are updated

// Save as PDF first
var pdfSaveOptions = new PdfSaveOptions();
document.Save(@"tempOutput.pdf", pdfSaveOptions);

// Now convert PDF to PNG (you'll need to use Aspose.PDF for this part)
using (var pdfDocument = new Aspose.Pdf.Document(@"tempOutput.pdf"))
{
    var pngSaveOptions = new Aspose.Pdf.Devices.PngDevice();
    pngSaveOptions.Process(pdfDocument.Pages[1], @"outputPngPath"); // Save the first page as PNG
}

Make sure to include the necessary references for Aspose.PDF if you choose to go this route. This approach can help maintain the layout integrity of your document.

If the issue persists, consider checking the Aspose documentation for any updates or specific handling of complex fields.

The issue persists when converting to PDF format as well. I’ve tested PDF conversion using the following code, and the field rendering problem remains identical.

Code:

using Aspose.Words;
using Aspose.Words.Saving;

new License().SetLicense(@"licensePath");
var document = new Document($@"inputDocxPath");
// document.UpdateFields();
var saveOptions = new PdfSaveOptions();
document.Save($@"outputPdfPath", saveOptions);

Aspose Output:
output.pdf (48.9 KB)

@SzuChiChen
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28688

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.