Issue with text placeholders in table cells when converting DOCX to PDF using Aspose.Words (overflow and hidden text)

I am facing an issue when converting DOCX files to PDF using Aspose.Words for .NET and later filling custom tags in the PDF using Aspose.PDF.Drawing.

We use a custom tagging format in the DOCX files:

#REG_DATUMS#  
#REG_NUMURS#

(i.e. strings in the format #([^#]*)#).

Problem:

  • When the DOCX is converted with Aspose.Words, the table cell text containing these tags (e.g. #REG_DATUMS#) becomes shifted to the left and partially hidden/overflowing inside the PDF.
  • When the same DOCX is converted to PDF directly in Microsoft Word and then processed with Aspose.PDF.Drawing, the issue does not occur — everything fills correctly.

How it should be:

How it is in a result:

Therefore, the root cause seems to be in how Aspose.Words exports tables with custom text placeholders into PDF.

Steps to Reproduce:

  1. Take the provided DOCX template containing tables and placeholders.
  2. Convert it to PDF using Aspose.Words.
  3. Open the resulting PDF and fill the placeholders with Aspose.PDF.Drawing.
  4. Compare the result with a PDF produced by converting the same DOCX to PDF from Microsoft Word, then filling with Aspose.PDF.

We use the following code to replace text in PDF:

private bool FillDocumentWithJsonData(Document document, string json)
{
    Dictionary<string, object> values = null;

    try
    {
		values = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
    }
    catch (Exception ex)
    {
		throw new Exception("Notikusi kļūda nolasot JSON datus", ex);
    }

    if (values == null || !values.Any())
		return false;

    values.Add("Sysdate", DateTime.Now.ToShortDateString());
			
    TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("#([^#]*)#");
    textFragmentAbsorber.TextSearchOptions = new TextSearchOptions(true);
    document.Pages.Accept(textFragmentAbsorber);

    object valueObject;
    bool changed = false;
    foreach (TextFragment textFragment in textFragmentAbsorber.TextFragments)
    {
		string key = textFragment.Text.Trim('#');
		string cleanKey = key.Replace("\r\n", "").Replace("\r", "").Replace("\n", "").Replace(" ", "");

		if (values.ContainsKey(cleanKey))
		{
			values.TryGetValue(cleanKey, out valueObject);

			string escapedValue = "";
			if (valueObject != null)
			{
				escapedValue = AsposePdfHelper.ReplaceLineBreaks(valueObject.ToString());
			}
			values[key] = escapedValue;
			var valueString = AsposeWordsHelper.TryConvertToString(values[cleanKey]);

			if (textFragment.Text != valueString)
			{
				textFragment.Text = valueString;
				changed = true;
			}
		}
    }
    return changed;
}

Expected Result:

  • Text placeholders inside table cells should remain properly positioned (as in the MS Word PDF conversion).

Actual Result:

  • Placeholders in PDFs generated with Aspose.Words are shifted left inside the table cells, partially hidden, and do not correctly display filled values.

Attachments:

  1. aspose_words_converted.pdf – converted with Aspose.Words (problematic).
  2. ms_office_converted.pdf – converted with Microsoft Word (works correctly).
  3. aspose_words_converted_aspose_pdf_filled.pdf – filled with Aspose.PDF after Aspose.Words conversion (shows the overflow/hidden issue).
  4. ms_office_converted_aspose_pdf_filled.pdf – filled with Aspose.PDF after Word conversion (works correctly).
  5. template.docx – the DOCX template.

aspose_words_converted.pdf (19.1 KB)

aspose_words_converted_aspose_pdf_filled.pdf (95.2 KB)

ms_office_converted.pdf (10.6 KB)

ms_office_converted_aspose_pdf_filled.pdf (82.7 KB)

template.docx (12.9 KB)


Environment:

  • Aspose.Words for .NET 25.9.0
  • Aspose.PDF.Drawing for .NET 25.9.0
  • Input documents created in Microsoft Word 2019
  • .NET 8.0

@janimos

It seems you’re experiencing issues with text placeholders in table cells when converting DOCX files to PDF using Aspose.Words. The placeholders are shifting and becoming partially hidden in the resulting PDF, which does not occur when converting the same DOCX file directly in Microsoft Word.

To address this issue, you can try the following steps:

  1. Check Table Cell Properties: Ensure that the table cell properties in your DOCX file are set correctly. Sometimes, padding or margins can cause text to overflow or shift.

  2. Use Layout Options: When converting the DOCX to PDF, you can specify layout options to better handle the content. For example, you can set the PdfSaveOptions to control how the document is rendered. Here’s a sample code snippet:

    Document doc = new Document("template.docx");
    PdfSaveOptions options = new PdfSaveOptions();
    options.LayoutOptions = PdfLayoutOptions.KeepTogether; // Adjust as needed
    doc.Save("output.pdf", options);
    
  3. Adjust Placeholder Formatting: If the placeholders are formatted differently in the DOCX, consider standardizing their formatting to ensure consistent rendering.

  4. Post-Processing in Aspose.PDF: After converting to PDF, you might need to adjust the positioning of the placeholders programmatically using Aspose.PDF. This can help align them correctly.

If these suggestions do not resolve the issue, consider sharing a minimal reproducible example with the Aspose support team for further assistance.

Feel free to reach out if you have any further questions!

The suggested solution isn’t working because PdfSaveOptions doesn’t contain LayoutOptions.

@janimos I think your workflow must be changed to get the desired output. You should fill the document with data before converting it to PDF. As I can see the value that must be replaced is represented with DOCPROPERTY field. So you can use the following code:

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

if (doc.CustomDocumentProperties["REG_NUMURS"] == null)
    doc.CustomDocumentProperties.Add("REG_NUMURS", "");
doc.CustomDocumentProperties["REG_NUMURS"].Value = "IKSD-25-3-LI";

if (doc.CustomDocumentProperties["REG_DATUMS"] == null)
    doc.CustomDocumentProperties.Add("REG_DATUMS", "");
doc.CustomDocumentProperties["REG_DATUMS"].Value = "25.09.2025";

doc.UpdateFields();
doc.Save(@"C:\Temp\out.pdf");

out.pdf (19.5 KB)

@alexey.noskov Yes, we have this as a default workflow, but mentioned workflow in issue is an optional and before we haven’t encountered the problem because we weren’t using Aspose to convert file as option previously, but now with client specific templates we encountered this problem when users convert files to PDF before these custom tags and fields are filled.

Some fields have display value with mentioned custom tag and some are the plain text in format of #([^#]*)#. System allows to fill these custom tags even in PDF.

@janimos Since you are replacing placeholders in PDF using Aspose.PDF. I will move your topic to Aspose.PDF support forum. My colleagues will help you shortly.

@janimos

If possible, can you please provide the sample JSON in text or .zip format for our reference? We will test the scenario in our environment and address it accordingly.

@asad.ali Yes, here is the JSON that you can use:

{
  "REG_DATUMS": "25.09.2025.",
  "REG_DATUMS_V": "2025. gada 25. septembris",
  "REG_DATUMS_V_L": "2025. gada 25. septembr\u012B",
  "REG_NUMURS": "IKSD-25-3-LI",
  "DOC_DAT": "25.09.2025.",
  "#DOC_DAT#": "25.09.2025.",
  "DOC_NR": "IKSD-25-3-LI",
  "#DOC_NR#": "IKSD-25-3-LI"
}

@janimos

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): PDFNET-60819

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.

1 Like