InsertDocumentAtBookmark extra tab issue

Hi,

I’m currently using the following method to insert a document into a bookmark:

public static void InsertDocumentAtBookmark(String bookmarkName, Document dstDoc, Document srcDoc)
{
    NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles);
 
    //Create DocumentBuilder
    DocumentBuilder builder = new DocumentBuilder(dstDoc);
 
    //Move cursor to bookmark and insert paragraph break
    builder.MoveToBookmark(bookmarkName);
    builder.Writeln();
 
    // If current paragraph is a list item, we should clear its formating.
    if (builder.CurrentParagraph.IsListItem)           
        builder.ListFormat.RemoveNumbers();
            
 
 
    //Content of srcdoc will be inserted after this node
    Node insertAfterNode = builder.CurrentParagraph.PreviousSibling;
 
    //Content of first paragraph of srcDoc will be apended to this parafraph
    Paragraph insertAfterParagraph = null;
 
    if (insertAfterNode.NodeType == NodeType.Paragraph)
        insertAfterParagraph = (Paragraph)insertAfterNode;
 
 
    //Content of last paragraph of srcDoc will be apended to this parafraph
    Paragraph insertBeforeParagraph = builder.CurrentParagraph;
 
    //We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.ParentNode;
 
    //Remove empty paragraphs from the end of document
    while (!((CompositeNode)srcDoc.LastSection.Body.LastChild).HasChildNodes)
    {
        srcDoc.LastSection.Body.LastParagraph.Remove();
        if (srcDoc.LastSection.Body.LastChild == null)
            break;
    }
 
    //Loop through all sections in the source document.
    foreach (Section srcSection in srcDoc.Sections)
    {
        //Loop through all block level nodes (paragraphs and tables) in the body of the section.
        for (int nodeIdx = 0; nodeIdx < srcSection.Body.ChildNodes.Count; nodeIdx++)
        {
            Node srcNode = srcSection.Body.ChildNodes[nodeIdx];
 
            //Do not insert node if it is a last empty paragarph in the section.
            Paragraph para = null;
 
            if (srcNode.NodeType == NodeType.Paragraph)
                para = (Paragraph)srcNode;
 
 
 
            if ((para != null) && para.IsEndOfSection && (!para.HasChildNodes))
                break;
 
 
            //If current paragraph is first paragraph of srcDoc
            //then appent its content to insertAfterParagraph
 
            bool nodeInserted = false;
 
            if (para != null && para.Equals(srcDoc.FirstSection.Body.FirstChild))
            {
                nodeInserted = true; // set this flag to know that we already processed this node.
 
                for (int i = 0; i < para.ChildNodes.Count; i++)
                {
                    Node node = para.ChildNodes[i];
                    Node dstNode = importer.ImportNode(node, true);
                    insertAfterParagraph.AppendChild(dstNode);
                }
 
                //If subdocument contains only one paragraph
 
                //then copy content of insertBeforeParagraph to insertAfterParagraph
 
                //and remove insertBeforeParagraph
 
                if (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastChild))
                {
                    while (insertBeforeParagraph.HasChildNodes)
                    { 
                        insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild);
                    }
                    insertBeforeParagraph.Remove();
                }
 
            }
 
            //If current paragraph is last paragraph of srcDoc
            //then appent its content to insertBeforeParagraph
 
            if (para != null && para.Equals(srcDoc.LastSection.Body.LastChild))
            {
                nodeInserted = true; // set this flag to know that we already processed this node.
 
                Node previouseNode = null;
 
                for (int i = 0; i < para.ChildNodes.Count; i++)
                {
                    Node node = para.ChildNodes[i];
 
                    Node dstNode = importer.ImportNode(node, true);
 
                    if (previouseNode == null)
                        insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
                    else
                        insertBeforeParagraph.InsertAfter(dstNode, previouseNode);
 
                    previouseNode = dstNode;
                }
            }
 
            if (!nodeInserted)
            {
                //This creates a clone of the node, suitable for insertion into the destination document.
 
                Node newNode = dstDoc.ImportNode(srcNode, true);
 
                //Insert new node after the reference node.
 
                dstStory.InsertAfter(newNode, insertAfterNode);
 
                insertAfterNode = newNode;
            }
        }
    }
}

The issue I’m having is that for bullet lists, an extra tab is being inserted for each item thus causing each item to be indented to the right (See attachment). The source document was originally in Rich Text Format (RTF). Let me know if you need anymore information.

Hi Brian,

Thanks for your inquiry. Could you please attach your source and destination documents here for testing? I will investigate the issue on my side and provide you more information.

Best Regards,

Hi Awais,

What we’ve found is that inserting into bookmarks is irrelevant. The issue occurs when converting a word document to pdf. I’ve attached the original word document and the resulting pdf.

Hi,

Thanks for the additional information.

While using the latest version of Aspose.Words i.e. 10.6.0, I managed to reproduce this issue on my side. Your request has been linked to the appropriate issue and you will be notified as soon as it is resolved. However, as a work around, please see the following code:

Document doc = new Document(@"c:\test\Bullets\Doc1.docx");
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach(Paragraph para in paragraphs)
{
    if (para.IsListItem)
    {
        // Do some list formatting here e.g
        double leftIndent = para.ParagraphFormat.LeftIndent;
        para.ListFormat.ApplyBulletDefault();
        para.ParagraphFormat.LeftIndent = leftIndent;
    }
}
doc.Save(@"c:\test\Bullets\Doc1_out.pdf");

Please let us know if we can be of any further assistance.

Best Regards,

Hi Awais,

I’ve tried the work around and it does successfully get rid of the extra tab. However, on some lists, the bullet points stop displaying.

Hi,

Thanks for your inquiry. It is perfect that you managed to get rid of those extra tabs in lists. Moreover, to be able to reproduce this new problem on my side, could you please share the code snippet you are using?

Best Regards,

Hi Awais,
I’m not sure which section of code you will need as it’s quite long. The bullet points seem to disappear when the list is inside a bookmark. I added the list into the bookmark using the InsertDocumentAtBookmark method mentioned earlier. Hope this helps.

Hi
Thanks for your request. I suppose, the problem occurs because InsertDocumentAtBookmark method does not copy whole first paragraph from source document, it copies only content of this paragraph. That is why the first bullet is lost. Please try using the code suggested here:
https://forum.aspose.com/t/63485
Best regards,

Hi,

Thanks for your inquiry. I think, it may be that the issue only happens when you execute the following code:

// If current paragraph is a list item, we should clear its formatting.
if (builder.CurrentParagraph.IsListItem)
    builder.ListFormat.RemoveNumbers();

RemoveNumbers method will essentially remove bullets and numbering from all paragraphs in the main text of a section.
Best Regards,

Hi Awais,

I’ve been able to resolve the issue. In the end we used the below solution, which gets rid of any extra tabs from in front of a list item. Thanks for your help!

foreach (Paragraph para in paragraphs)
{
    if (para.IsListItem)
    {          
        foreach (Node node in para.ChildNodes)
        {
            Run run = node as Run;
 
            if (run != null)
            {
                bool runBeginsWithATab = run.Range.Text.Count() > 0 && run.Range.Text[0] == '\t';
 
                if (runBeginsWithATab)
                    run.Text = run.Range.Text.Remove(0, 1);
            }
        }        
    }
}

Hi Brian,

Thanks for the additional information. It is perfect that you managed to resolve the issue; however, we will keep you informed regarding status of this issue and let you know once it is resolved.

Best Regards,

The issues you have found earlier (filed as WORDSNET-2968) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

The issues you have found earlier (filed as WORDSNET-5251) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(4)