This project has moved. For the latest updates, please go here.

problems with replacetext, inserttext and row.removeat

Sep 10, 2009 at 8:46 PM

I'm having a few problems and wondering if it is a doc formatting problem on my side or your DocX code - which is great, by the way.

With both Paragraph.InsertText and Paragraph.ReplaceText, the new text is always indented, like it's a paragraph contained within a paragraph, or as if the text is being put in as a child of the paragraph, rather than a sibling. I've tried all sorts of ways of doing this but alway have the same problem. I also get this when the paragraph is in a table cell.

Also, I can't remove rows in a table, the RemoveAt and RemoveRange do not work for me. I don't get an error, but the rows are still there when I open the saved docx.

Thanks

Coordinator
Sep 11, 2009 at 6:55 AM
Hello santacruzboy,

I am not sure why you are experiencing these issues. Can you please send me the document and a code snippet to reproduce this problem. I can then examine this problem more carefully on my side.

kind regards,
Cathal
Sep 11, 2009 at 3:54 PM
coffeycathal wrote:
Hello santacruzboy,

I am not sure why you are experiencing these issues. Can you please send me the document and a code snippet to reproduce this problem. I can then examine this problem more carefully on my side.

kind regards,
Cathal

 I don't see an attachment function on the reply for sending you the docx file, maybe you can tell me how to do that (sorry for being ignorant about that).

Here is the code snippet:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Novacode;

namespace DocxText
{
 class Program
 {
  static void Main(string[] args)
  {
   string templateName = @"C:\Templates\Template.docx";
   string replacementName = @"C:\Templates\ParagraphToInsert.docx";
   
   DocX tempDoc = DocX.Load(templateName);
   DocX insertDoc = DocX.Load(replacementName);
   
   //do the replacement work
   string docSection = tempDoc.Text;
   int index = docSection.LastIndexOf("{Key0001}");
   tempDoc.InsertParagraph(index + 9, insertDoc.Paragraphs[0]);
   tempDoc.ReplaceText(docSection.Substring(index, 9), string.Empty, false);
   
   //also insert this in the table
   tempDoc.Tables[0].InsertRow();
   tempDoc.Tables[0].Rows[1].Cells[0].Paragraph.InsertText(0, insertDoc.Paragraphs[0].Text, false);

   ////save to new word doc
   DateTime currDate = DateTime.Now;
   string temporaryName = @"C:\Templates\NewDoc_" + currDate.ToString("yyyyMMddhhmmss") + ".docx";
   tempDoc.SaveAs(temporaryName);
   tempDoc.Dispose();
   tempDoc = null;
  }
 }
}

The template.docx file looks like:

Reporting requirements

Due date

 

Paragraph 1.

 

{Key0001}

 

Paragraph 2.

and the ParagraphToInsert.docx file looks like:

For your convenience, please see Reporting Requirements, enclosed with this letter, for further instructions on preparing these reports.  Please use the grant reference number above in all reports and correspondence.

Two other things. I can now delete the row in the table by using the Table level RemoveRow(), the row level method didn't work for me, but that's OK now. The other problem I get is that the text index on the LastIndexOf, when I do the paragraph insert actually is inserting it in the middle of the Paragraph 2. line. The problem is that in the table cell and the inserted paragraph are both indented.

 

Thanks for your help.

Coordinator
Sep 12, 2009 at 11:14 AM
Hi santacruzboy,

unfortunately codeplex does not have an option for uploading attachments, you will have to email the document to me at coffey.cathal@gmail.com.

>> I can now delete the row in the table by using the Table level RemoveRow(), 
>> the row level method didn't work for me, but that's OK

The newest version of DocX v1.0.0.8 only contains a Table.RemoveRow(index), there is no Row.Remove().

>> The other problem I get is that the text index on the LastIndexOf, when I do 
>> the paragraph insert actually is inserting it in the middle of the Paragraph 2.
>> line.

I do not understand what you mean, can you please rephrase this question? Inserting text using an index is currently not as reliable as I would like it to be. If you are trying to add text to the end of a Paragraph please use Paragraph.Append() or Paragraph.AppendLine()

using (DocX document = DocX.Load("Test.docx"))
{
        Paragraph p1 = document.Paragraphs[0];
        p1.AppendLine("Hello World");
 
        document.Save();
}

If you are trying to add a new paragraph after another paragraph use Paragraph.InsertParagraphAfterSelf()

using (DocX document = DocX.Load("Test.docx"))
{
    Paragraph p1 = document.Paragraphs[0];
    p1.InsertParagraphAfterSelf("Hello World");

    document.Save();
}

Please let me know if this helps,
kind regards,
Cathal
Oct 1, 2009 at 3:13 PM

I was wondering if you were able to reproduce my problem with replaced text getting indented (like a sub-Paragraph)?