We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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);
        <span style="color:green;">//Create DocumentBuilder</span>
        <span style="color:#2b91af;">DocumentBuilder</span> builder = <span style="color:blue;">new</span> <span style="color:#2b91af;">DocumentBuilder</span>(dstDoc);

        <span style="color:green;">//Move cursor to bookmark and insert paragraph break</span>
        builder.MoveToBookmark(bookmarkName);
        builder.Writeln();

        <span style="color:green;">// If current paragraph is a list item, we should clear its formating.</span>
        <span style="color:blue;">if</span> (builder.CurrentParagraph.IsListItem)           
            builder.ListFormat.RemoveNumbers();
        


        <span style="color:green;">//Content of srcdoc will be inserted after this node</span>
        <span style="color:#2b91af;">Node</span> insertAfterNode = builder.CurrentParagraph.PreviousSibling;

        <span style="color:green;">//Content of first paragraph of srcDoc will be apended to this parafraph</span>
        <span style="color:#2b91af;">Paragraph</span> insertAfterParagraph = <span style="color:blue;">null</span>;

        <span style="color:blue;">if</span> (insertAfterNode.NodeType == <span style="color:#2b91af;">NodeType</span>.Paragraph)
            insertAfterParagraph = (<span style="color:#2b91af;">Paragraph</span>)insertAfterNode;


        <span style="color:green;">//Content of last paragraph of srcDoc will be apended to this parafraph</span>
        <span style="color:#2b91af;">Paragraph</span> insertBeforeParagraph = builder.CurrentParagraph;

        <span style="color:green;">//We will be inserting into the parent of the destination paragraph.</span>
        <span style="color:#2b91af;">CompositeNode</span> dstStory = insertAfterNode.ParentNode;

        <span style="color:green;">//Remove empty paragraphs from the end of document</span>
        <span style="color:blue;">while</span> (!((<span style="color:#2b91af;">CompositeNode</span>)srcDoc.LastSection.Body.LastChild).HasChildNodes)
        {
            srcDoc.LastSection.Body.LastParagraph.Remove();
            <span style="color:blue;">if</span> (srcDoc.LastSection.Body.LastChild == <span style="color:blue;">null</span>)
                <span style="color:blue;">break</span>;
        }

        <span style="color:green;">//Loop through all sections in the source document.</span>
        <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">Section</span> srcSection <span style="color:blue;">in</span> srcDoc.Sections)
        {
            <span style="color:green;">//Loop through all block level nodes (paragraphs and tables) in the body of the section.</span>
            <span style="color:blue;">for</span> (<span style="color:blue;">int</span> nodeIdx = 0; nodeIdx < srcSection.Body.ChildNodes.Count; nodeIdx++)
            {
                <span style="color:#2b91af;">Node</span> srcNode = srcSection.Body.ChildNodes[nodeIdx];

                <span style="color:green;">//Do not insert node if it is a last empty paragarph in the section.</span>
                <span style="color:#2b91af;">Paragraph</span> para = <span style="color:blue;">null</span>;

                <span style="color:blue;">if</span> (srcNode.NodeType == <span style="color:#2b91af;">NodeType</span>.Paragraph)
                    para = (<span style="color:#2b91af;">Paragraph</span>)srcNode;



                <span style="color:blue;">if</span> ((para != <span style="color:blue;">null</span>) && para.IsEndOfSection && (!para.HasChildNodes))
                    <span style="color:blue;">break</span>;


                <span style="color:green;">//If current paragraph is first paragraph of srcDoc</span>
                <span style="color:green;">//then appent its content to insertAfterParagraph</span>

                <span style="color:blue;">bool</span> nodeInserted = <span style="color:blue;">false</span>;

                <span style="color:blue;">if</span> (para != <span style="color:blue;">null</span> && para.Equals(srcDoc.FirstSection.Body.FirstChild))
                {
                    nodeInserted = <span style="color:blue;">true</span>; <span style="color:green;">// set this flag to know that we already processed this node.</span>

                    <span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 0; i < para.ChildNodes.Count; i++)
                    {
                        <span style="color:#2b91af;">Node</span> node = para.ChildNodes[i];
                        <span style="color:#2b91af;">Node</span> dstNode = importer.ImportNode(node, <span style="color:blue;">true</span>);
                        insertAfterParagraph.AppendChild(dstNode);
                    }

                    <span style="color:green;">//If subdocument contains only one paragraph</span>

                    <span style="color:green;">//then copy content of insertBeforeParagraph to insertAfterParagraph</span>

                    <span style="color:green;">//and remove insertBeforeParagraph</span>

                    <span style="color:blue;">if</span> (srcDoc.FirstSection.Body.FirstParagraph.Equals(srcDoc.LastSection.Body.LastChild))
                    {
                        <span style="color:blue;">while</span> (insertBeforeParagraph.HasChildNodes)
                        { 
                            insertAfterParagraph.AppendChild(insertBeforeParagraph.FirstChild);
                        }
                        insertBeforeParagraph.Remove();
                    }

                }

                <span style="color:green;">//If current paragraph is last paragraph of srcDoc</span>
                <span style="color:green;">//then appent its content to insertBeforeParagraph</span>

                <span style="color:blue;">if</span> (para != <span style="color:blue;">null</span> && para.Equals(srcDoc.LastSection.Body.LastChild))
                {
                    nodeInserted = <span style="color:blue;">true</span>; <span style="color:green;">// set this flag to know that we already processed this node.</span>

                    <span style="color:#2b91af;">Node</span> previouseNode = <span style="color:blue;">null</span>;

                    <span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 0; i < para.ChildNodes.Count; i++)
                    {
                        <span style="color:#2b91af;">Node</span> node = para.ChildNodes[i];

                        <span style="color:#2b91af;">Node</span> dstNode = importer.ImportNode(node, <span style="color:blue;">true</span>);

                        <span style="color:blue;">if</span> (previouseNode == <span style="color:blue;">null</span>)
                            insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
                        <span style="color:blue;">else</span>
                            insertBeforeParagraph.InsertAfter(dstNode, previouseNode);

                        previouseNode = dstNode;
                    }
                }

                <span style="color:blue;">if</span> (!nodeInserted)
                {
                    <span style="color:green;">//This creates a clone of the node, suitable for insertion into the destination document.</span>

                    <span style="color:#2b91af;">Node</span> newNode = dstDoc.ImportNode(srcNode, <span style="color:blue;">true</span>);

                    <span style="color:green;">//Insert new node after the reference node.</span>

                    dstStory.InsertAfter(newNode, insertAfterNode);

                    insertAfterNode = newNode;
                }
            }
        }
    }</pre>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.<br>

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”);<o:p></o:p>

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 formating.
<o:p></o:p>

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;
                    <span style="color: blue;">if</span> (run != <span style="color: blue;">null</span>)
                    {
                        <span style="color: blue;">bool</span> runBeginsWithATab = run.Range.Text.Count() > 0 && run.Range.Text[0] == <span style="color: rgb(163, 21, 21);">'\t'</span>;

                        <span style="color: blue;">if</span> (runBeginsWithATab)
                            run.Text = run.Range.Text.Remove(0, 1);
                    }
                }        
            }
        }</pre><pre style="background: white; color: black; font-family: Consolas;"> </pre>

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)