Extra line added when converting .dotm entries to rtf

Hello,

We having an issue when converting .dotm to rtf , an extra line is been added after the title section when converted to rtf format. Below is the code which we are using for conversion, also have attached the .dotm file as zip

public List GetBuildingBlocks(Stream template)
{
    List result = new List();
    Document srcDoc = new Document(template);
    Document dstDoc = new Document();
    DocumentBuilder builder = new DocumentBuilder(dstDoc);
    Node insertAfterNode = builder.CurrentParagraph;
    bool inEntry = false;
    foreach (Aspose.Words.BuildingBlocks.BuildingBlock b in srcDoc.GlossaryDocument.BuildingBlocks)
    {
        inEntry = false;
        if (b.Gallery.ToString() == "AutoText")
        {
            foreach (Section srcSection in b.Sections)
            {
                CompositeNode dstStory = insertAfterNode.ParentNode;
                NodeImporter importer = new NodeImporter(srcDoc.GlossaryDocument, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting);
                foreach (Node srcNode in srcSection.Body)
                {
                    if (srcNode.NodeType.Equals(NodeType.Paragraph))
                    {
                        Paragraph para = (Paragraph)srcNode;
                        if (para.IsEndOfSection && !para.HasChildNodes)
                            continue;
                    }
                    Node newNode = importer.ImportNode(srcNode, true);
                    builder.MoveTo(insertAfterNode);
                    if (!inEntry) builder.CurrentParagraph.ParentNode.InsertBefore(newNode, builder.CurrentParagraph);
                    else builder.CurrentParagraph.ParentNode.InsertAfter(newNode, builder.CurrentParagraph.ParentNode.LastChild);
                    inEntry = true;
                }
            }
            if (inEntry)
            {
                using (MemoryStream rtfStream = new MemoryStream())
                {
                    dstDoc.Save(rtfStream, Aspose.Words.SaveFormat.Rtf);
                    result.Add(new AutoText
                    {
                        Name = b.Name,
                        Description = string.IsNullOrEmpty(b.Description) ? b.Name : b.Description,
                        Content = System.Text.Encoding.UTF8.GetString(rtfStream.ToArray())
                    });
                }
                dstDoc = new Document();
                builder = new DocumentBuilder(dstDoc);
                insertAfterNode = builder.CurrentParagraph;
            }
        }
    }
    return result;
}

Do you have any idea, what can be the problem here.

Hi Ashok,

Thanks for your inquiry. Please upgrade to the latest version of Aspose.Words 16.1.0 from the following link. I hope, this helps:

Download Aspose.Words for .NET

In case the problem still remains, please attach the following resources here for testing:

  • Aspose.Words generated output document which shows the undesired behavior
  • Please create a standalone Console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

Hi ,

We tried upgrading the dll to Aspose.Words 16.1.0 but didn’t get any success.
Also we are facing one more issue which is that even the font is not been maintained converting .dotm file to rtf.

Have attached a console application which depict the following issue

  1. Inserting unwanted additional break line after the first line of the file when converted from .dotm to RTF
  2. Not maintaining the original font when converted from .dotm to RTF

The application is created in Visual studio 2013

The .dotm file are added in the project. You can open the file in ms word and go to insert from menu option and click on AutoText to see the List and than select any item from the list to verify the actual format.

Hi Ashok,

Thanks for your inquiry. We are working over your query and will get back to you soon.

Best regards,

Hi Ashok,

Thanks for being patient. Please see the following changes in your code to fix this issue:

public static List<AutoText> GetBuildingBlocks(Stream template)
{
    List<AutoText> result = new List<AutoText>();
    Document srcDoc = new Document(template);
    Document dstDoc = (Document)srcDoc.Clone();
    dstDoc.RemoveAllChildren();
    dstDoc.EnsureMinimum();
    DocumentBuilder builder = new DocumentBuilder(dstDoc);
    Node insertAfterNode = builder.CurrentParagraph;
    bool inEntry = false;
    foreach (Aspose.Words.BuildingBlocks.BuildingBlock b in srcDoc.GlossaryDocument.BuildingBlocks)
    {
        inEntry = false;
        if (b.Gallery.ToString() == "AutoText")
        {
            foreach (Section srcSection in b.Sections)
            {
                CompositeNode dstStory = insertAfterNode.ParentNode;
                NodeImporter importer = new NodeImporter(srcDoc.GlossaryDocument, insertAfterNode.Document, ImportFormatMode.UseDestinationStyles);
                foreach (Node srcNode in srcSection.Body)
                {
                    if (srcNode.NodeType.Equals(NodeType.Paragraph))
                    {
                        Paragraph para = (Paragraph)srcNode;
                        if (para.IsEndOfSection && !para.HasChildNodes)
                            continue;
                    }
                    Node newNode = importer.ImportNode(srcNode, true);
                    builder.MoveTo(insertAfterNode);
                    if (!inEntry) builder.CurrentParagraph.ParentNode.InsertAfter(newNode, builder.CurrentParagraph);
                    else builder.CurrentParagraph.ParentNode.InsertAfter(newNode, builder.CurrentParagraph.ParentNode.LastChild);
                    inEntry = true;
                }
            }
            if (inEntry)
            {
                using (MemoryStream rtfStream = new MemoryStream())
                {
                    dstDoc.FirstSection.Body.FirstParagraph.Remove();
                    dstDoc.Save(rtfStream, Aspose.Words.SaveFormat.Rtf);
                    result.Add(new AutoText
                    {
                        Name = b.Name,
                        Description = string.IsNullOrEmpty(b.Description) ? b.Name : b.Description,
                        Content = System.Text.Encoding.UTF8.GetString(rtfStream.ToArray())
                    });
                }
                dstDoc = new Document();
                builder = new DocumentBuilder(dstDoc);
                insertAfterNode = builder.CurrentParagraph;
            }
        }
    }
    return result;
}

Hope, this helps.

Best regards,

Hi,

We Updated the code and found that the issue for extra line added when converting .dotm entries to rtf was resolved, but we couldn’t resolve the issue for font change when converting .dotm entries to rtf.
So kindly give your feedback on the same.

Regards,
Ashok

Hi Ashok,

Thanks for your inquiry. You can fix this issue by replacing last three lines with the following code:

dstDoc = (Document)srcDoc.Clone();
dstDoc.RemoveAllChildren();
dstDoc.EnsureMinimum();
builder = new DocumentBuilder(dstDoc);
insertAfterNode = builder.CurrentParagraph;

Hope, this helps.

Best regards,