Formatting issue observered with new version of Aspose dll version V23

Hi,

We have recently updated aspose dll version from V14 to V23.We are seeing lot of formatting issues in the forms when we reference to newer version of dll into the solution.

Change :

From
AsposeDLLVersion value =“v14” in config file.

TO
AsposeDLLVersion value =“v23”

Find the attched froms for your references.

MC_CovLineBodilyInjury_notWorking.docx (17.0 KB)

MC_CovLineBodilyInjury-working.docx (17.8 KB)

Snapshot after actual rendering to UI.

Before change : working snapshot old dll reference output

After Change : Not working copy output new reference output

@omkars Could you please provide a simple code or a console application that will allow us to reproduce the problem on our side? We will check the issue and provide you more information.

@alexey.noskov please find the attached document which has snippet and method which is used to convert document to PDF.

AsposeIssue.docx (132.6 KB)

@omkars Thank you for additional information. Unfortunately, the provided code is still not enough to reproduce the problem on our side. If possible, could you please create a simple console application that will allow us to reproduce the problem? We will check the issue and provide you more information.

@alexey.noskov Please find the attached sample solution and few sample forms which are used to reproduce the issue.

sample input provided as word :

output PDF generated using aspose

Not able to upload the entire solution hence adding a code hence adding a code here

about.aspx.cs

BuildDocText :

public static void BuildDocText()
{
    string fileOutputTemporary = Path.GetTempFileName();
    try
    {
        XmlDocument xmlDoc = new XmlDocument();

        string fileXmlPath = @"C:\Users\omkar.sakpal\source\repos\WebAppAspose\XMLFile1.xml";
        xmlDoc.Load(fileXmlPath);

        XmlDocument mergeData = new XmlDocument();
        mergeData.InnerXml = xmlDoc.InnerXml;

        string docType = mergeData.DocumentElement.GetAttribute("docType");
        string rootPath = @"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_PremiumSection.docx";
        string outFileName = mergeData.DocumentElement.GetAttribute("PDFFile");

        GenerateDoc(mergeData, rootPath);
    }
    catch (Exception Err)
    {
        throw new Exception(string.Format(CultureInfo.CurrentCulture, fileOutputTemporary), Err);
    }
}

-----------GenerateDoc------------------

private static void GenerateDoc(XmlDocument mergeData, string rootPath)
{

    try
    {
        XmlDocument xmlDoc = new XmlDocument();


        XmlNodeList exportSections = mergeData.DocumentElement.SelectNodes("ExportSection");

        foreach (System.Xml.XmlElement exportSection in exportSections)
        {
            string exportName = GetExportName(exportSection);
            string duplex = GetDuplex(exportSection);
            string paperBin = GetPaperBin(exportSection);

            DateTime startTime = DateTime.Now;

            XmlNodeList subDocs = exportSection.SelectNodes("subForm");
            int subDocCount = subDocs.Count;
            Aspose.Words.Document finalDocument;
            foreach (System.Xml.XmlElement subDoc in subDocs)
            {
                string sourceFile = GetSubdocSourceFile(rootPath, subDoc);
                finalDocument = AddSubDoc(sourceFile, subDoc);
            }
        }
    }
    catch (Exception ex)
    {

    }
    finally
    {

    }
}

Add sub doc and merge are the two methods which uses aspose and club the forms

private static Aspose.Words.Document AddSubDoc(string sourceFile, XmlElement parent)
{
    Aspose.Words.Document mainDoc = new Aspose.Words.Document(sourceFile);

    try
    {
        // Load the main document where others will be appended

        // Load the additional documents to be appended
        Aspose.Words.Document doc1 = new Aspose.Words.Document(@"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_Header_Cov_Line.docx");
        Aspose.Words.Document doc2 = new Aspose.Words.Document(@"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_CovLineBodilyInjury_notWorking.docx");
        Aspose.Words.Document doc3 = new Aspose.Words.Document(@"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_CovLinePropertyDamage.docx");
        Aspose.Words.Document doc4 = new Aspose.Words.Document(@"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_CovSafetyRidingApparel.docx");
        Aspose.Words.Document doc5 = new Aspose.Words.Document(@"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_CSCredit.doc");

        // Append the documents to the main document
        mainDoc.AppendDocument(doc1, ImportFormatMode.KeepSourceFormatting);
        mainDoc.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);
        mainDoc.AppendDocument(doc3, ImportFormatMode.KeepSourceFormatting);
        mainDoc.AppendDocument(doc4, ImportFormatMode.KeepSourceFormatting);
        mainDoc.AppendDocument(doc5, ImportFormatMode.KeepSourceFormatting);

        int lastSectionIndex = doc2.Sections.Count - 1;


        if (lastSectionIndex < mainDoc.Sections.Count - 1)
        {
            MergeSections(mainDoc, lastSectionIndex);
        }
        //Save the Form into PDF
        string filename = @"C:\AsposeDLL\sample.pdf";
        mainDoc.Save(filename, Aspose.Words.SaveFormat.Pdf);
    }
    catch (Exception ex)
    {

    }

    return mainDoc;
}

private static void MergeSections(Aspose.Words.Document _document, int lastSectionIndex)
{
    //Initialize
    Section previousLastSection = _document.Sections[lastSectionIndex];
    Section newFirstSection = _document.Sections[lastSectionIndex + 1];

    newFirstSection.ClearHeadersFooters();
    //Clear contents of headers and footers. They are not null, just empty.
    //This links them to previous section's headers and footers (previousLastSection's)

    Body previousLastBody = previousLastSection.Body;
    Body newFirstBody = newFirstSection.Body;

    Paragraph previousLastParagraph = null;
    Table previousLastTable = null;
    bool lastParagraphIsEmpty = false;
    bool mergeTables = false;

    bool mergeTableStyle = true;// Configuration.Forms.MergeTableStyle;

    if (previousLastBody != null && newFirstBody != null)
    {
        Node lastChild = previousLastBody.LastChild;

        //Check if the last node of the document is a paragraph or table
        if (lastChild != null && lastChild.NodeType == NodeType.Paragraph)
        {
            previousLastParagraph = (Paragraph)lastChild;

            //If the last paragraph is empty, remember this so it can be removed
            lastParagraphIsEmpty = true;// ParagraphIsEmpty(previousLastParagraph);
            if (mergeTableStyle)
            {
                lastChild = lastChild.PreviousSibling;
            }
        }
        if (lastChild != null && lastChild.NodeType == NodeType.Table && mergeTableStyle)
        {
            //previousLastTable = (Table)lastChild;
            mergeTables = true;
        }

    }

    //Copy the content from the new section being added to the end of the current document
    previousLastSection.AppendContent(newFirstSection);

    //Remove the paragraph that used to be that last paragraph of the first section if it was empty.
    //This will merge adjacent tables if they were separated only by a paragraph marker
    //Tables seperated by nothing are merged automatically by word due to document validation.
    if (lastParagraphIsEmpty)
    {
        previousLastParagraph.Remove();
    }

    //Delete the second section, a copy of it has been attached to the main document.
    newFirstSection.Remove();

    if (mergeTables && previousLastTable != null && mergeTableStyle)
    {
        Table table = previousLastTable;

        //Iterate through every table directly following the table at the previous end of the document
        //until a non-table node is reached, or all available nodes have been visited. 
        //Then for each table, row by row, append marge said table to the previoys last table. 
        while ((table.NextSibling != null) && (table.NextSibling.NodeType == NodeType.Table))
        {
            Table nextSibling = (Table)table.NextSibling;
            while (table.HasChildNodes && nextSibling.FirstRow != null)
            {
                /* Workaround to Border Style issue with Aspose - remove when fixed
                 * see http://www.aspose.com/community/forums/thread/587638/docx-joining-two-tables-and-saving-to-pdf-changes-border-style.aspx
                 * Aspose issue WORDSNET-11049
                 */
                Row row = nextSibling.FirstRow;
                BorderCollection borders = row.RowFormat.Borders;
                table.Rows.Add(row);
                table.LastRow.RowFormat.Borders.Left.LineStyle = borders.Left.LineStyle;
                table.LastRow.RowFormat.Borders.Right.LineStyle = borders.Right.LineStyle;
                table.LastRow.RowFormat.Borders.Top.LineStyle = borders.Top.LineStyle;
                table.LastRow.RowFormat.Borders.Bottom.LineStyle = borders.Bottom.LineStyle;
            }
            //Delete the table that was just merged in
            //A copy of it was just merged to the previous last table.
            nextSibling.Remove();
        }
    }
}

The probable cause is
Once sub forms have been appended then it tries to re-align the sub form exactly below to main form where it fails and which is causing formatting and alignment issues , when the subform has large number of section than the main form

As shown in method snippet.
This method tries to readjust the section and alignment of sub form, and due to which there are formatting issues

Forms used
MC_CovComprehensive_NotWorking.docx (16.9 KB)

MC_CovLineBodilyInjury_notWorking.docx (18.0 KB)

MC_CovSafetyRidingApparel.docx (16.9 KB)

MC_Header_Cov_Line.docx (17.8 KB)

MC_PremiumSection.docx (15.9 KB)

1 Like

@omkars Thank you for additional information. Still the code is not fully runnable since some of method are missed and some of documents used in your code are also not attached. But as I can see the purpose of your code is simply concatenate several of documents and save the result as PDF. After merging the attached documents the result on my side looks like this:
out.pdf (57.8 KB)

It is not quite clear for me what is the purpose of merging sections in the output document. Different sections might have different page setup and moving content to another section might affect the document layout. If avoid merging the sections, the result is the following:
out_no_section_merging.pdf (58.1 KB)

As you can see in this case the tables looks the same as in the source documents. If the goal is to avoid page breaks between merged documents, you can simply set section start of each section to continous:

foreach (Section s in mainDoc.Sections)
    s.PageSetup.SectionStart = SectionStart.Continuous;

out_no_section_merging_continuous.pdf (56.9 KB)

@alexey.noskov tried the code that suggested by you, the alignment between the forms is getting effected due to the code change

s.PageSetup.SectionStart = SectionStart.Continuous;

There is a slight improvement between merging the subdocument with main document. However, there is an alignment and space issue can be seen between the form. The same thing is working fine on V14 however breaking on V23.
V14 Vs V 23

Due to the code change the sentence shifted to next line as highlighted and due to which further pages alignment is getting impacted,
For eg:

also wanted to know what is causing FONT to change the style in V23
V23 Vs V14

@osakpal As I have mentioned the attached document has different page setup nd table settings, that causes the issues after merging them into one document.
Also, the provided code is not runnable on my side, so I cannot fully test your scenario. Analyzing the problem by screenshots is impossible.
So, could you please create a simple console application that we can run on our side and see the problem? This will help us to analyze it and provide you an assistance.

In additional you should note that between 14 and 23 version of Aspose.Words there were 9 year of development and 14 version might have some missed functionality, which led to expected result on your side.

@alexey.noskov please find the sample working solution which has exact scenario which we are trying to explain. please note that the issue occurring on V23 and not on V14 the snapshot which shared in prior discussion

AsposeFormIssuePOC.zip (112.4 KB)

@osakpal Thank you for additional information, but the MC_CovComprehensive_NotWorking.docx file specified in the XML file is not attached. Could you please attach it here too.

@alexey.noskov can you refer previous threads where file is already shared.

image.png (5.7 KB)

@osakpal The code you have provided can be simplified to the following:

string rootPath = @"C:\Temp";

Document mainDoc = new Document($"{rootPath}\\MC_CovComprehensive_NotWorking.docx");
// Load the additional documents to be appended
Document Subdoc1 = new Document($"{rootPath}\\MC_Header_Cov_Line.docx");
Document Subdoc2 = new Document($"{rootPath}\\MC_CovLineBodilyInjury_notWorking.docx");
Document Subdoc3 = new Document($"{rootPath}\\MC_CovLinePropertyDamage.docx");
Document Subdoc4 = new Document($"{rootPath}\\MC_CovSafetyRidingApparel.docx");
Document Subdoc5 = new Document($"{rootPath}\\MC_CSCredit.doc");
// Append the documents to the main document
mainDoc.AppendDocument(Subdoc1, ImportFormatMode.KeepSourceFormatting);
mainDoc.AppendDocument(Subdoc2, ImportFormatMode.KeepSourceFormatting);
mainDoc.AppendDocument(Subdoc3, ImportFormatMode.KeepSourceFormatting);
mainDoc.AppendDocument(Subdoc4, ImportFormatMode.KeepSourceFormatting);
mainDoc.AppendDocument(Subdoc5, ImportFormatMode.KeepSourceFormatting);

foreach (Section s in mainDoc.Sections)
{
    s.PageSetup.SectionStart = SectionStart.Continuous;
}
//Save the Form into PDF
string filename = $"{rootPath}\\MergerSampleDocument_14.8.pdf";
mainDoc.Save(filename, SaveFormat.Pdf);

I have tested the code with 24.8 and 14.8 versions and here are the results:
MergerSampleDocument_14.8.pdf (59.4 KB)
MergerSampleDocument_24.8.pdf (59.3 KB)

The result produced by 24.8 version is closer to DOCX document:
out_14.8.docx (17.8 KB)
out_24.8.docx (19.7 KB)

So the output produced by the latest version is correct and corresponds the document layout displayed by MS Word.

@alexey.noskov we have implemented the same into our solution however still some formatting issues

can you please let us know why these forms are working correctly in V14 and not in V23

@osakpal Could you please attach the the problematic DOCX and PDF documents produced on your side? It looks like some font substitution issue. Have you tried implementing IWaringCallback?

@alexey.noskov Please find the V14 and v23 word document which has formatting and font issues. also we have attached the output PDF files for your reference.

Input form 1 for doc
Worksheet_Header_1.docx (21.3 KB)

Worksheet_Line_Fee_2.docx (14.8 KB)

Worksheet_Header_1.docx (21.3 KB)

Output of this using V14 and V23 are as follows

V14_form1.pdf (61.2 KB)

V23_form1.pdf (62.0 KB)

Form 2 which has issues as well as follows
input document
MC_Header_Cov_Line.docx (17.3 KB)

MC_CovLineBodilyInjury_notWorking.docx (17.0 KB)

MC_CovLinePropertyDamage.docx (16.9 KB)

MC_Header_Cov_Risk.docx (16.7 KB)

App_Street_PaymentOption.docx (33.2 KB)

output form 2
V14_Form2_output.pdf (368.3 KB)

V23_form2_output.pdf (381.6 KB)

In addition, there is an observation from our side is that while merging the main document with subdocument V14 is applying the format setting of main document. While in V23 we observed that the settings are getting applied which is of subdocument and not of main document. Is this something that has been changed inside the aspose DLL ? it can be observed in form 1 for the font issue.

@osakpal Thank you for additional information. But unfortunately, I cannot reproduce the problem on my side. In your template you have merge fields, which are filled before merging documents. I tried with dummy data and the above provided code to merge the documents, but the problem is not reproducible. Is the problem reproducible on your side if save the output as DOCX? If not, is the problem reproducible if convert the output DOCX to PDF?

@alexey.noskov the issue is reproducible if convert the output DOCX to PDF. Also,

can you also confirm below observation
In addition to this there is an observation from our side is that while merging the main document with subdocument V14 is applying the format setting of main document. While in V23 we observed that the settings are getting applied which is of subdocument and not of main document. Is this something that has been changed inside the aspose DLL ? it can be observed in form 1 for the font issue.

@osakpal

Could you please attach your output DOCX documents?

As far as I know there were no changes in this behavior. As you can see you are using ImportFormatMode.KeepSourceFormatting in this mode Aspose.Words should preserve the original documents formatting.

@alexey.noskov Please find the sample docx file and output generated using Aspose V23.
Please note that the same document rendering correctly in Aspose V14.

SampleDocx.docx (16.7 KB)

AlignmentIssue.pdf (53.0 KB)

@osakpal Unfortunately, I still cannot reproduce the problem. I used the following code for testing:

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

out_23.2.pdf (50.4 KB)
out_24.9.pdf (50.5 KB)