Copying a Paragraph

Feb 11, 2014 at 11:57 PM
Using compiled version v2.0.50727

I am trying to create a customer statement from a document template. For the detailed section, different customers have the columns in different orders. In the template is a paragraph that has the fields in the appropriate order.

To fill this, I need to copy the paragraph, do a ReplaceText on the items in the new paragraph, then repeat for the next month, up to 12 times for the various months on the statement.

DocX docx = DocX.Load(docStream);

//General replaces
foreach (var item in dict)
  docx.ReplaceText(item.Key, item.Value);

var pg = docx.Paragraphs.FirstOrDefault(p => p.Text.Contains("~+~"));

foreach (var rectrow in dtrct.AsEnumerable())
  var rdict = new Dictionary<string, string> {...}

  var newpg = pg.InsertParagraphBeforeSelf(pg.Text);
  foreach (var item in rdict)
    newpg.ReplaceText(item.Key, item.Value);
It works, sorta.


pg.Text = "\t\t\t\t\t\t\t\t\t~PST-DATE~\t~INT-DATE~\t~AMT-REF1~\t~AMT-REF2~\t~ET-INT~\t~ET-PRIN~\t~BYR-RESIN~\t~BYR-RESOUT~\t~SLRFEE~\t~ET-UNPAID~\t~ET-INTADDBK~\t~+~" but the original document doesn't contain any leading tabs.

newpg is not maintaining the tab positions or formatting, from what I can tell.

I tried setting newpg.XML = pg.XML but that didn't correctly set newpg.
I tried both pg.InsertParagraphBeforeSelf(pg) and pg.InsertParagraphBeforeSelf(pg.Text).
I have verified that .FirstOrDefault returns the exactly one matching Paragraph.

How can I correctly duplicate a Paragraph and maintain formatting and tab stops?
What am I doing wrong in trying to access the Paragraph Text?

Thank you for your time.
Feb 22, 2014 at 10:01 AM
I have the same issue!
How can we correctly duplicate a Paragraph?
Feb 22, 2014 at 10:36 AM
I think I found a way, the following code works for me :

// first create a paragraph from found paragraph
// foundParagraph == pg
Paragraph paragraph = foundParagraph.InsertParagraphAfterSelf(foundParagraph);

after that we can work on paragraph and it behaves like a new Paragraph