While generating Output in PDF Allginment is not same as DOCX generator

Hello Aspose Support Team,

I’m generating documents using Aspose.Words from a template (.docx) and dynamic JSON data. The output is generated in both DOCX and PDF formats, but I’m observing a noticeable alignment difference between the two outputs — for example, table or text alignment shifts when comparing the DOCX and the converted PDF.

Here’s the relevant part of my code:

// Save the result
using (MemoryStream outputStream = new MemoryStream())
{
    if (outputFormate == ".pdf")
    {
        PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
        pdfSaveOptions.OutlineOptions.DefaultBookmarksOutlineLevel = 1;
        docContent.Save(outputStream, pdfSaveOptions);
    }
    else if (outputFormate == ".docx")
    {
        docContent.Save(outputStream, SaveFormat.Docx);
    }

    modifiedByte = outputStream.ToArray();
}

I’m using the same input JSON and Word template for both outputs.
However, when I open the generated files, the alignment and layout differ (especially in tables and text blocks).

Could you please help me understand:

  • What rendering or layout algorithm Aspose.Words uses internally for DOCX vs. PDF output?
  • Why might alignment differ between the two formats?
  • Are there any recommended settings or compatibility options to ensure layout consistency between DOCX and PDF outputs?

I can share the template, input JSON, and both generated outputs (DOCX & PDF) if needed for investigation.

Mortgage - Non MERS Barcode.docx (34.4 KB)
OutputTestAssemble.docx (26.3 KB)

test1.pdf (85.8 KB)
output json is like below

{
           "LenderNameOverride": "First Horizon Bank",
      "LoanNumber": "2395678910",
      "PreparedByIndividualName": "BRIAN T. MORGAN",
      "PreparedByCompanyName": "Horizon Mortgage Services",
      "PreparedByAddress": "1825 Magnolia Avenue",
      "PreparedByCity": "Birmingham",
      "PreparedByState": "AL",
      "PreparedByZip": "35205",
      "PreparedByPhone": "205-555-7821",
      "RecordReturnToCompanyName": "Outamation, Inc.",
      "RecordReturnToAddress": "1371 SAWGRASS CORPORATE PARKWAY",
      "RecordReturnToCity": "SUNRISE",
      "RecordReturnToState": "FL",
      "RecordReturnToZip": "33323",
      "casenumber": "AL99887766",
      "PropertyAddress": "3187 Oak Meadow Drive"
   }

Thank you,
Vaibhavi

@Vaibhavi_Lad

Hello Vaibhavi,

The differences in alignment and layout between the DOCX and PDF outputs can occur due to several factors related to how Aspose.Words processes these formats. Here are some insights and recommendations to help you achieve better consistency:

  1. Rendering Algorithms: Aspose.Words uses different rendering algorithms for DOCX and PDF formats. The DOCX format is designed for editing and may allow for more flexible layout adjustments, while PDF is a fixed-layout format that aims to preserve the appearance of the document as it would be printed.

  2. Potential Causes for Alignment Differences:

    • Fonts: Ensure that the fonts used in your DOCX template are available and embedded in the PDF. If the fonts are not available, it may lead to unexpected layout changes.
    • Page Margins and Sizes: Check the page setup in your DOCX template. Differences in margins or page sizes can affect how content is rendered in PDF.
    • Table Properties: Tables can behave differently in DOCX and PDF. Ensure that table properties (like cell padding, spacing, and alignment) are explicitly set.
  3. Recommended Settings:

    • You can try adjusting the PdfSaveOptions to include settings that might help with layout consistency. For example:
      PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
      pdfSaveOptions.UseHighQualityRendering = true; // Improves rendering quality
      pdfSaveOptions.EmbedFullFonts = true; // Ensures fonts are embedded
      // Add other options as needed
      
  4. Testing with Different Options: Experiment with different PdfSaveOptions settings to see if they improve the alignment. You can also try saving the document as HTML first and then converting that HTML to PDF, which sometimes yields better results.

If you continue to experience issues, sharing the template and generated outputs would be helpful for further investigation.

Best regards,
[Your Name]

I face same issue after giving this options after providing that pdfoptions can you please give me more solution and also output pdf as well as docs and input docx and json is already attach

@Vaibhavi_Lad 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 (54.9 KB)