Edit Document Range

Last post 02-07-2011, 8:36 PM by aske012. 21 replies.
Page 1 of 2 (22 items)   1 2 Next >
Sort Posts: Previous Next
  •  01-25-2011, 9:43 AM 280900

    Edit Document Range

    I am looking for a way to edit the document's range. I know it is read-only but I need to trim off the last character of the document, whatever that character may be.

    It could be a paragraph character or an actual letter/number.

    So if my document.range.text was "hello world!", I would need to trim off the "!", keeping in mind that I won't always know ahead of time what the last character will be.

    Thanks a lot and if you need further clarification please let me know.

    Thanks,

    RJ

     
  •  01-25-2011, 8:08 PM 281008 in reply to 280900

    Re: Edit Document Range

    Hi there,

    Thanks your inquiry.

    Range.Replace may not work properly here as special characters (such as paragraph breaks etc) will cause the replacement to not work. Instead you can try using a technique like below. I assume you want to remove the last character from the body of the document and not the headers or footers. If that happens to be the case I will provide some further code for you.

    // Get all the runs in the last paragraph in the body of the last section in the document

    NodeCollection runs = doc.LastSection.Body.LastParagraph.GetChildNodes(NodeType.Run, true);

    // The last run

    Run lastRun = (Run)runs[runs.Count - 1];

    // Remove the last character.

    lastRun.Text = lastRun.Text.Substring(0, lastRun.Text.Length - 1);

    Thanks,


    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
  •  01-26-2011, 9:15 AM 281143 in reply to 281008

    Re: Edit Document Range

    Thanks for the reply Adam. Still having some trouble though.

    For example, if my

    doc.range.text = Name: 12345 *

    The star is a place holder for the the actual value that is there. It's a little square box in visual studio's debugger. Usually represents a paragraph break.

    If I try your doc.lastSection.body.lastParagraph.GetChildNodes(nodetype.run, True) on it it returns 0 as the nodeCollection count. Therefore it doesn't remove the last character.

    I can't know what the last character may be, it could be a paragraph break, a bookmark, a letter/number. 

    This is only for the body of the document, yes.

    As an experiment I tried removing all whitespace from the line and trying what you provided however it didn't remove the last "square box". All it did was the last character before the "square box". So using the example above it would look like this....

    Name:12345* and after using your code it would be this Name:1234*

    Keep in mind with the *'s representing the "square box" still.

    I'd appreciate any help you could provide.

    Thanks a lot.

    RJ

     
  •  01-27-2011, 4:57 AM 281356 in reply to 281143

    Re: Edit Document Range

    Hi there,

    Thanks for this additional information. Could you please attach your template or templates here for testing and I will provide some further information.

    Thanks,


    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
  •  01-27-2011, 7:40 AM 281389 in reply to 281356

    Re: Edit Document Range

    Attachment: Present (inaccessible)

    I've attached 2 documents. Both are word templates.

    The first(doc1) is the main template which has the other template(doc2) inserted into it at the corresponding bookmark(IIDSPOT) using aspose's insertBefore method and passing in the bookmark as the insertBeforeNode.

    Doc2 has a number inserted it then it's supposed to be inserted into the main document in between IDS and IO.

    I can get the insertion to occur but when it inserts it also inserts the extra line at the bottom of doc2's template.

    I need this extra line removed so that looking at the final inserted document there shouldn't be a space between IID and IO.

    Keep in mind that there are many other templates that will be inserted into the doc1, but i've limited it to just one to send to you as to learn the process and I should be able to apply to all others.

     

    Thanks a lot for any help you can provide.

     
  •  01-27-2011, 9:08 AM 281414 in reply to 281389

    Re: Edit Document Range

    Also I forgot to mention that I cannot edit the templates as an older system uses them and they work as is and cannot be modified.

    Thanks,

    RJ

     
  •  01-27-2011, 9:44 PM 281559 in reply to 281414

    Re: Edit Document Range

    Hi there,

    Thanks for attaching your documents here.

    Please try using the code from this thread here. This will remove any empty paragraphs at the end of a document. I suggest you run this for both of your documents, as on my side when inserting your document there was no space between IID and IO but there was one above.

    Thanks,


    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
  •  01-28-2011, 7:55 AM 281680 in reply to 281559

    Re: Edit Document Range

    Hi Adam,

    Thanks a lot for your help so far but it's still not working. If i use the emptyLastParagraph procedure it doesn't really do what I expect it to.

    I can see 2 paragraphs in the doc2 template. The first has the "IID: 12345" as the range.text and the 2nd paragraph shows "the square box" as its range.text. So after i remove the last paragraph it does show that there is only 1 paragraph but the "square box" has moved into the range.text of paragraph 1 like this "IID: 12345'squarebox'".

    Any idea what could be going on?

     
  •  01-28-2011, 7:58 PM 281809 in reply to 281680

    Re: Edit Document Range

    Hi there,

    Thanks for this additional information.

    I see what you mean now. I can see this character is a page break in your document, but does not actually act as page break in the document. Most likely your documents are slightly corrupted. You're right when removing the last paragraph the page break moves to the previous line. This is very odd behaviour and I was unable to find any way around this at the moment.

    I have logged this issue to be fixed. We will keep informed of when it's resolved. In the mean time I can't suggest any work around apart to modify the original documents (I am aware you said you can't modify them).

    May I also ask why you need this character removed if it seems to be causing no visible problems?

    Thanks,


    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
  •  01-31-2011, 6:40 AM 282001 in reply to 281809

    Re: Edit Document Range

    It is causing there to be an empty line right below where I insert the Doc2 template into the Doc1 template.

    I cannot just remove all blank lines from the finished Doc1 document because there are many template inserts into Doc1 with appropriate empty lines that we cannot remove.

    Spacing is very important because these are legal documents that have to remain exaclty how they are in our older system.

    I understand that there isn't really anything left you can suggest. Thanks for your attempts and please keep me informed if you discover how to remedy this in the future.

    Thanks,

    RJ

     
  •  02-01-2011, 4:18 AM 282189 in reply to 282001

    Re: Edit Document Range

    Sorry I did not follow the whole thread (it is a bit long for me), but I can give you this food for thought:

    1. Your doc2.dot has two paragraphs.

    The first paragraph contains text "IID:  *"     (* - stands for the paragraph break symbol)

    The second paragraph is empty and therefore contains only paragraph break "*".

    2. The normal paragraph break character in Aspose.Words is "\x000d". This character is returned at the end of the first paragraph if you request Paragraph.GetText for example.

    But the second paragraph is also the last paragraph in the section (and end of the document) and Aspose.Words returns the ControlChar.SectionBreak character at the end of such paragraph (well, MS Word Automation does the same I think). Now, ControlChar.SectionBreak character is "\x000c" which is actually the same as ControlChar.PageBreak. That's the same in Microsoft Word too - Aspose.Words just borrowed these concepts from MS Word.

    So there is no page break in the model in your document. Now you or Adam can probably figure out what to do next.

    BTW If a paragraph is the last paragraph in a table cell, then Aspose.Words (and MS Word) will return "\x0007" character at the end of the paragraph instead of the normal "\x000d".

    I am closing the issue for now.


    Roman Korchagin
    Aspose.Words Team Leader
    Aspose Auckland Team
     
  •  02-01-2011, 8:50 AM 282245 in reply to 282189

    Re: Edit Document Range

    Hi Roman,

     Thanks for giving me some additional information on my forum post "Edit Document Range".

    I still am a little confused though. I understand what you are saying that the last page break in a document becomes a section break but I'm still not sure how I can resolve the problem I'm having where I want to trim off the last one in each template.

    Perhaps you can suggest a way to get around the problem I'm having using documentBuilder?

    Thanks a lot for all your help.

    RJ

     
  •  02-01-2011, 8:17 PM 282392 in reply to 282245

    Re: Edit Document Range

    Hi there,

    Thanks for that clarification Roman.

    As Roman explained there is no issue with that character as it's expected in the Range facade. That should of clicked at the time but I was too busy focusing on the range and not actual cause of the issue. My apologises for leading you in a circle.

    The actual issue occurs because the bookmark has been inserted into a blank paragraph. This is the cause of the empty paragraph and not the character seen at the end of the range in the second document.

    To remove this empty paragraph you can check for the block level node where the document is inserted and remove it if empty. Please see the code below, I'm assuming you're using the InsertDocument method provided in the documentation here.

    Document doc1 = new Document("doc1.dot");

    Document doc2 = new Document("doc2.dot");

     

    BookmarkStart bookmarkstart = doc.Range.Bookmarks["IID"].BookmarkStart;

    InsertDocument(bookmarkstart.ParentNode, doc2);

    doc.Save("Document out.docx");

     

     

    public static void InsertDocument(Node insertAfterNode, Document srcDoc)

    {

        // Make sure that the node is either a pargraph or table.

        if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) &

          (!insertAfterNode.NodeType.Equals(NodeType.Table)))

            throw new ArgumentException("The destination node should be either a paragraph or table.");

     

        // We will be inserting into the parent of the destination paragraph.

        CompositeNode dstStory = insertAfterNode.ParentNode;

        CompositeNode originalInsertNode = (CompositeNode)insertAfterNode;

     

        // This object will be translating styles and lists during the import.

        NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting);

     

        // 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.

            foreach (Node srcNode in srcSection.Body)

            {

                // Let's skip the node if it is a last empty paragarph in a section.

                if (srcNode.NodeType.Equals(NodeType.Paragraph))

                {

                    Paragraph para = (Paragraph)srcNode;

                    if (para.IsEndOfSection && !para.HasChildNodes)

                        continue;

                }

     

                // This creates a clone of the node, suitable for insertion into the destination document.

                Node newNode = importer.ImportNode(srcNode, true);

     

                // Insert new node after the reference node.

                dstStory.InsertAfter(newNode, insertAfterNode);

                insertAfterNode = newNode;

            }

        }

     

        if(string.IsNullOrEmpty(originalInsertNode.ToTxt().Trim()))

            originalInsertNode.Remove();

    }

    Thanks,


    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
  •  02-04-2011, 1:13 PM 283082 in reply to 282392

    Re: Edit Document Range

    What you have suggested works for the most part however I'm running into a different problem now.

    Lets say I have doc1 that looks like this...

    Dear :

    And doc2 looks like this...

    John Smith

    And I have a bookmark right before the ":"(colon). Now if i need to insert from doc2 at that bookmark how can I get it to go at that exact location? If i use the insertAfter or insertBefore method, you have to pass the parentNode of the bookmark which is that whole section. If i use the insertBefore method it comes out like this...

    John Smith

    Dear :

    And if i use insertAfter it comes out like this...

    Dear : John Smith

    I need it to be inserted right at the bookmark itself and not before/after its parentNode. Can this be done?

    Thanks a lot and let me know if you require anything else from me.

    RJ 

     
  •  02-04-2011, 1:15 PM 283083 in reply to 283082

    Re: Edit Document Range

    Sorry I forgot to include what I wanted it to look like...

    Dear John Smith:

    I'm assuming this can be done using the documentBuilder however I won't always have just a small piece of text to insert. It could be a paragraph node with children, a table, etc.

    Thanks again.

     
Page 1 of 2 (22 items)   1 2 Next >
View as RSS news feed in XML