Filipe Costa
Filipe Costa

Reputation: 555

passing a list to a list<T>

I am working with openxml, and have something that is pulling my hairs up, basicly i am editing a pré existing document, it is a template, the template should mantain the first page and the second, so every section i add(paragraph, table etc..) it should be added between the 2 pages, i already accomplish that, i can insert a simple table this way:

      DocTable docTable = new DocTable();
        Paragraph paragraph = doc.MainDocumentPart.Document.Body.Descendants<Paragraph>()
        .Where<Paragraph>(p => p.InnerText.Equals("some Text")).First();
        Table table = docTable.createTable(Convert.ToInt16(2), Convert.ToInt16(2));


        mainPart.Document.Body.InsertAfter(table, paragraph);

i basicly search the pargraph at the end of the page 1 and insert the table after. My problem is: i don't receive a single section from a frontEnd webpage, i receive a list of sections, i defined this list as a list of object without a defined type since it can have Tables, paragraphs and other things.

so basicly i have this:

List<Object> listOfSections = new List<Object>();

In receive the sections from the front end, and identify what it is with the key like this:

foreach (DocumentAtributes section in sections.atributes)
{
    if(section.key != "Document")
    {
         checkSection(mainPart, section, listOfSections);
    }
}

public void checkSection(MainDocumentPart mainPart,DocumentAtributes section,List<Object> listOfSections)
{
    switch (section.key)
    {
        case "Table":
            DocTable docTable = new DocTable();
            Table table = docTable.createTable(Convert.ToInt16(section.rows), Convert.ToInt16(section.cols));
            listOfSections.Add(new Run(table));
            break;
        case "Paragraph":
            DocRun accessTypeTitle = new DocRun();
            Run permissionTitle = accessTypeTitle.createParagraph(section.text, PARAGRAPHCOLOR, Convert.ToInt16(section.fontSize), DEFAULTFONT,section.align);
            listOfSections.Add(permissionTitle);
            break;
        case "Image":
            DocImage docImage = new DocImage();
            Run image = docImage.imageCreatorFromDisk(mainPart, "abcd", Convert.ToInt16(section.width), Convert.ToInt16(section.height), section.align, null, null, section.wrapChoice, section.base64);
            listOfSections.Add(image);
            break;
    }
}

I need a way to add this list to the insertAfter, it must be the list i can't add the individual object since after i insert the first the next sections will be added after the paragraph either it brings me a issue since i want the order to be the same as it comes in the sections.atributes.

So the insertAfter accepts a list and i have a list of objects the method is like this: insertAfter(List, refChild)

Can i cast my list of objects or do something else? need some help here.

Upvotes: 1

Views: 95

Answers (1)

petelids
petelids

Reputation: 12815

You can iterate the list in reverse to have the first element in the list immediately after the paragraph, followed by the second, then the third etc.

for (int i = listOfSections.Count - 1; i >= 0; i--)
{
    mainPart.Document.Body.InsertAfter(listOfSections[i], paragraph);
}

If you start with a list with elements:

Element1
Element2
Element3
Element4

And the document starts with just:

Paragraph

Then after each iteration you would end up with:

Iteration 1

Paragraph
Element4

Iteration 2

Paragraph
Element3
Element4

Iteration 3

Paragraph
Element2
Element3
Element4

and finally, Iteration 4

Paragraph
Element1
Element2
Element3
Element4

which is the desired result.

Upvotes: 2

Related Questions