DocX - Insert Text at Bookmark Position

5

Click here to load reader

description

DocX - Insert Text at Bookmark Position

Transcript of DocX - Insert Text at Bookmark Position

  • DocX - Insert Text at Bookmark position

    https://docx.codeplex.com/discussions/534685[30/03/2015 21:59:04]

    Insert Text at Bookmark position Wiki Link: [discussion:534685]

    248907Feb 24, 2014 at 7:49 AMEdited Feb 24, 2014 at 7:51 AM

    Hello,

    is it possible to insert new text at the position of an existing bookmark?

    If yes, could you please give me a full example?

    at the moment i am working with Replace, but its not a very good solution if there is a chance that someone manually likes to change the document.

    Here is my actual code:

    using (DocX document = DocX.Load(@"C:\temp\readme.docx")){ document.ReplaceText("strDatum", "24-02-2014"); document.Save();}

    Hope anyone can help me.

    CodePlex Project Hosting for Open Source Software Register Sign In

    HOME SOURCE CODE DOWNLOADS DOCUMENTATION DISCUSSIONS ISSUES PEOPLE LICENSE

    New Thread

    Subscribe

  • DocX - Insert Text at Bookmark position

    https://docx.codeplex.com/discussions/534685[30/03/2015 21:59:04]

    Thank you

    Reto

    MadBoyDeveloper

    Feb 24, 2014 at 7:54 AMEdited Feb 24, 2014 at 7:56 AM

    Some time ago bookmarks were added to main source code (not in the binary): "This patch adds basic support for inserting and manipulating bookmarks inside a word file. It also allows for inserting text at bookmark locations. The change was predicated on a need to insert text at bookmark locations in a sort of token replacement."

    The code is below but this example is taken straight from Source Code where there are multiple examples for different things.

    private static void Bookmarks() { Console.WriteLine("\nBookmarks()");

    using (var document = DocX.Create(@"docs\Bookmarks.docx")) { var paragraph = document.InsertBookmark("firstBookmark");

    var paragraph2 = document.InsertParagraph("This is a paragraph which contains a "); paragraph2.AppendBookmark("secondBookmark"); paragraph2.Append("bookmark");

    paragraph2.InsertAtBookmark("handy ", "secondBookmark");

    document.Save(); Console.WriteLine("\tCreated: docs\\Bookmarks.docx\n"); } }

    Unless you mean you need functionality that works for template that already has them then i guess you would need to find the paragraphs first and then try InsertAtBookmark but this wasn't tested and may not be there.

    248907Feb 24, 2014 at 8:53

    Hello,

    thanks for your answer.

  • DocX - Insert Text at Bookmark position

    https://docx.codeplex.com/discussions/534685[30/03/2015 21:59:04]

    AM Yes i am looking for a solution to use a template i got and find the bookmark and insert text. I was not able to find a paragraph or a bookmark after trying a few hours now.

    Is there a solution to do it?

    Hope you can help me.

    Reto

    bjr001Feb 25, 2014 at 2:28 PM

    What about iterate all paragraphs and look up for your specific bookmark?!

    foreach (var paragraph in document.Paragraphs) { var bookmark = paragraph.GetBookmarks().FirstOrDefault(x => x.Name == "strDatum"); if (bookmark != null) bookmark.Paragraph.InsertAtBookmark("24-02-2014", bookmark.Name); }

    248907Feb 26, 2014 at 11:06 AM

    would be nice, but:

    Error 1 'Novacode.Paragraph' does not contain a definition for 'GetBookmarks' and no extension method 'GetBookmarks' accepting a first argument of type 'Novacode.Paragraph' could be found (are you missing a using directive or an assembly reference?)

    bjr001Feb 27, 2014 at 11:54 AM

    Seems like you're using a wrong version of DocX.dll .. see also: https://docx.codeplex.com/discussions/534409

    248907Feb 27, 2014 at 12:10 PM

    WOW!, yes.

    i DID download the newest compiled Version here 1.0.1.3 and the Thread you mentioned has the same Version but smaller and newer.

    Now it works. Exactly what i needed. Thank you very very much!

    Hi guys,

  • DocX - Insert Text at Bookmark position

    https://docx.codeplex.com/discussions/534685[30/03/2015 21:59:04]

    misidraSep 11, 2014 at 10:45 AM

    I'm trying to fill bookmarks of a Word document by the solution provided bjr001 but I have the following issue: GetBookmarks() is not a member of Novacode.Paragraph.

    Any solution possible?

    Thanks in advance.

    248907Sep 11, 2014 at 1:37 PM

    Hi misidra,

    download the Version from the Thread mentioned by bjr001 and compile it yourself, then use the .dll you compiled. It worked for me, i hope it works for you too.

    Let us know.

    Greetings Reto

    misidraSep 11, 2014 at 2:02 PM

    I recently try to use that version, but now VS send me this error: Excepcin no controlada del tipo 'System.IO.FileNotFoundException' en System.Windows.Forms.dll

    Informacin adicional: Could not load file or assembly 'DocX, Version=1.0.1.13, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

    It doesn't work any function.

    248907Sep 11, 2014 at 3:29 PM

    Hi misidra,

    it says that the system is not able to find your dll. have you copied there from where you linked it in? Or is the dll where it should be at runtime? where your .exe ist located?

    saludos, Reto

    misidraSep 11, 2014 at 3:58 PM

    The .dll file is located at debug folder, like other resources that I'm using.

    from where is the error thrown?

  • DocX - Insert Text at Bookmark position

    https://docx.codeplex.com/discussions/534685[30/03/2015 21:59:04]

    248907Sep 12, 2014 at 8:45 AM

    misidraSep 12, 2014 at 10:14 AM

    I've just solved the problem using Interop thing.

    Thanks anyway ;)

    MadBoyDeveloper

    Sep 12, 2014 at 1:44 PM

    @misindra if you're fine with Interop limitations and you know consequences of using it then it's fine. You've been warned :-)

    breath2kOct 13, 2014 at 1:20 PM

    I used NuGet to install DocX so shouldn't I have the latest version anyway? The link on the other thread didn't work and I don't have the GetBookmarks() or any of the InsertBookmark, AppendBookmark etc code at all??

    MadBoyDeveloper

    Oct 17, 2014 at 5:27 PMEdited Oct 17, 2014 at 5:28 PM

    breath2k wrote:

    I used NuGet to install DocX so shouldn't I have the latest version anyway? The link on the other thread didn't work and I don't have the GetBookmarks() or any of the InsertBookmark, AppendBookmark etc code at all??

    We don't support NuGet deployment as we don't have access there (not sure who is publishing there). So the version as NuGet package may be a lot older then what you get by either downloading newest binary from here or compiling the source code. I would suggest updating the version from binary that is downloadable from here. It was released recently.

    Sign in to post message or set email notifications

    2006-2015 Microsoft Get Help Privacy Statement Terms of Use Code of Conduct Advertise With Us Version 12.4.2014.20959

    codeplex.comDocX - Insert Text at Bookmark position

    NvbS9kaXNjdXNzaW9ucy81MzQ2ODUA: form4: searchsite: Search all projects