PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

How to Remove a paragraph / table from a migradoc document?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2915
Page 1 of 1

Author:  souravsaraf1230 [ Wed Aug 27, 2014 8:20 am ]
Post subject:  How to Remove a paragraph / table from a migradoc document?

I want to remove a paragraph / table from a migradoc document?

Consider the situation below :
I have para1 , para2 , para3. Whether para2 will be displayed or not is determined by the calculations done while determining para3. So I need to do something like the following :

DetermineContent_of_Para1();
Document.addpara(para1);

DetermineContent_of_Para2();
Document.addpara(para2);

DetermineContent_of_Para3();
Document.addpara(para3);

If(!displayPara2)
{
Document.removepara(para2);
}

Is there any such option like removePara() in migradoc or an alternative which achieves the above requirement?

Thank You

Author:  Thomas Hoevel [ Wed Aug 27, 2014 8:28 am ]
Post subject:  Re: How to Remove a paragraph / table from a migradoc docume

Paragraphs are document elements. They are stored in collections and you can remove them using Remove() or RemoveAt().

I guess it would be cleaner to create Para2 and Para3 and also determine whether Para2 is needed.
And then add Para2 if needed and always add Para3.

With MigraDoc the document is dynamic, you can remove paragraphs, insert paragraphs, alter paragraphs. But IMHO you don't need this for your scenario.

Author:  souravsaraf1230 [ Wed Aug 27, 2014 8:55 am ]
Post subject:  Re: How to Remove a paragraph / table from a migradoc docume

Hi Thomas,

Thanks for replying. The actual requirement is much more complex, I just framed the question this way to make things simpler.

Say we have the following code:

var pdfdocument = new Document();
var pdfsection = pdfdocument.AddSection();
var para1 = pdfsection.AddParagraph();
var para2 = pdfsection.AddParagraph();
var para3 = pdfsection.AddParagraph();

// para2.Remove() [No such function exists]
// pdfsection.Remove(para2) [No such function exists]
// pdfdocument.Remove(para2) [No such function exists]

Can you tell me exactly how to remove a para2 ? I cant seem to find Remove() or RemoveAt() functions. I am using migradoc version 1.32.

Author:  Thomas Hoevel [ Wed Aug 27, 2014 11:46 am ]
Post subject:  Re: How to Remove a paragraph / table from a migradoc docume

Oops, it's not Remove(), it's RemoveObjectAt().

Code:
myDocument.AddSection();
myDocument.LastSection.AddParagraph("Test");
var para = myDocument.LastSection.AddParagraph("Test #2");
myDocument.LastSection.AddParagraph("Test #3");

int idx = myDocument.LastSection.Elements.IndexOf(para);
if (idx >= 0)
    myDocument.LastSection.Elements.RemoveObjectAt(idx);

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/