Есть ли способ динамически создавать таблицы в Migradoc - PullRequest
0 голосов
/ 21 июня 2020

Я закончил создание счета в Migrado c. Я пытаюсь провести рефакторинг и понимаю, что у меня несколько случаев использования вызовов таблиц с одним и тем же кодом. Заголовки таблиц всегда одинаковы, как и количество столбцов, размеры и все остальное. Единственное, что изменится, - это строки внутри таблицы, а также текст AddParagraph (). Вот пример:

Таблица счетов

Я новичок в c#, так что, возможно, дело в том, что я использую классы .. Мне было предложено использовать свой собственный класс, например:

public class MyTable 
  {
    public Table table;
  }

Вот некоторые моего кода для первых двух таблиц, которые я создал, он очень подробный и повторяющийся, а не DRY:

Table TimeDetailTop = section.AddTable();
        TimeDetailTop.Rows.LeftIndent = "-1cm";
        TimeDetailTop.Borders.Width = 0.75;
        

        Column column4 = TimeDetailTop.AddColumn(Unit.FromCentimeter(4.5));
        column4.Format.Alignment = ParagraphAlignment.Left;
        column4 = TimeDetailTop.AddColumn(Unit.FromCentimeter(13.5));
        column4.Format.Alignment = ParagraphAlignment.Left;

        Row row4 = TimeDetailTop.AddRow();
        Cell cell4 = row4.Cells[0];
        cell4.Format.Font.Size = 9;
        cell4.Shading.Color = Colors.LightGray;
        cell4.MergeRight = 1;
        cell4.AddParagraph("Invoice Time Detail");
        cell4.Format.Font.Bold = true;

        row4 = TimeDetailTop.AddRow();
        cell4 = row4.Cells[0];
        cell4.Row.Borders.Visible = false;

        row4 = TimeDetailTop.AddRow();
        cell4 = row4.Cells[0];
        cell4.Format.Font.Bold = true;
        cell4.Format.Font.Size = 9;
        cell4.AddParagraph("Invoice Number:");
        cell4.Row.Borders.Visible = false;

        cell4 = row4.Cells[1];
        cell4.Format.Font.Size = 9;
        cell4.AddParagraph("2000498");
        cell4.Row.Borders.Visible = false;

        row4 = TimeDetailTop.AddRow();
        cell4 = row4.Cells[0];
        cell4.Format.Font.Size = 9;
        cell4.Format.Font.Bold = true;
        cell4.AddParagraph("Company:");
        cell4.Row.Borders.Visible = false;

        cell4 = row4.Cells[1];
        cell4.Format.Font.Size = 9;
        cell4.AddParagraph("ADV Integrity");
        cell4.Row.Borders.Visible = false;

        row4 = TimeDetailTop.AddRow();
        cell4 = row4.Cells[0];
        cell4.AddParagraph();
        cell4.Row.Borders.Visible = false;

        row4 = TimeDetailTop.AddRow();
        cell4 = row4.Cells[0];
        cell4.AddParagraph();
        cell4.Row.Borders.Visible = false;

        //created table of details in the invoice
        Table timeDetailTable = section.AddTable();
        timeDetailTable.Rows.LeftIndent = "-1cm";
        timeDetailTable.Borders.Width = 0.75;

        Column column5 = timeDetailTable.AddColumn(Unit.FromCentimeter(2));
        column5.Format.Alignment = ParagraphAlignment.Center;
        column5 = timeDetailTable.AddColumn(Unit.FromCentimeter(3));
        column5.Format.Alignment = ParagraphAlignment.Left;
        column5 = timeDetailTable.AddColumn(Unit.FromCentimeter(4.75));
        column5.Format.Alignment = ParagraphAlignment.Left;
        column5 = timeDetailTable.AddColumn(Unit.FromCentimeter(1));
        column5.Format.Alignment = ParagraphAlignment.Center;
        column5 = timeDetailTable.AddColumn(Unit.FromCentimeter(1.5));
        column5.Format.Alignment = ParagraphAlignment.Center;
        column5 = timeDetailTable.AddColumn(Unit.FromCentimeter(3.75));
        column5.Format.Alignment = ParagraphAlignment.Left;
        column5 = timeDetailTable.AddColumn(Unit.FromCentimeter(2));
        column5.Format.Alignment = ParagraphAlignment.Right;

        Row row5 = timeDetailTable.AddRow();
        row5.KeepWith = 3;
        row5.Format.Font.Size = 9;
        row5.Format.Alignment = ParagraphAlignment.Left;
        row5.Shading.Color = Colors.LightGray;
        Cell cell5 = row5.Cells[0];
        cell5.MergeRight = 6;
        cell5.AddParagraph("Charge To: ADV Integrity / Error Message on Outlook   Location: NO NOT USE");
        cell5.Format.Font.Bold = true;

        row5 = timeDetailTable.AddRow();
        cell5 = row5.Cells[0];
        cell5.AddParagraph("Date");
        
        

        cell5 = row5.Cells[1];
        cell5.AddParagraph("Staff");

        cell5 = row5.Cells[2];
        cell5.AddParagraph("Notes");

        cell5 = row5.Cells[3];
        cell5.AddParagraph("Bill");

        cell5 = row5.Cells[4];
        cell5.AddParagraph("Hours");

        cell5 = row5.Cells[5];
        cell5.AddParagraph("Rate");

        cell5 = row5.Cells[6];
       
        cell5.AddParagraph("Ext Amt");

        for(int i = 0; i <= 6; i++)
        {
            cell5 = row5.Cells[i];
            cell5.Borders.Left.Visible = false;
            cell5.Borders.Right.Visible = false;
            cell5.Borders.Top.Visible = false;
            cell5.Format.Font.Bold = true;
            cell5.Format.Font.Size = 9;
        }

        //add blank row
        row5 = timeDetailTable.AddRow();
        cell5 = row5.Cells[0];
        cell5.AddParagraph();
        cell5.Row.Borders.Visible = false;

        row5 = timeDetailTable.AddRow();
        cell5 = row5.Cells[0];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("04/06/2020");
        
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[1];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("Beed, Jacob");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[2];
        cell5.Format.Font.Size = 9;
        cell5.Format.Font.Bold = true;
        var paragraph = cell5.AddParagraph("Service Ticket:");
        paragraph.AddFormattedText(" 41410", TextFormat.NotBold);
        cell5.AddParagraph();
        var paragraph2 = cell5.AddParagraph("Summary:");
        paragraph2.AddFormattedText(" Error Message on Outlook", TextFormat.NotBold);
        cell5.AddParagraph();
        var paragraph3 = cell5.AddParagraph("");
        paragraph3.AddFormattedText("Looking at her calendars in outlook, noticed about 6 or 8 \"other calendars\" and" +
            "when clicking on them received an error message that said the object had been deleted, clicked ok " +
            "and it was removed from the list. Continued to do that for all of them and clicked send and receive all" +
            "and it didn't generate the error message again. If the message returns, Wendy will reopen the ticket" +
            "including another screenshot.", TextFormat.NotBold);
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[3];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("Y");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[4];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("0.50");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[5];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("125.00");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[6];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("$62.50");

        row5 = timeDetailTable.AddRow();
        row5.Shading.Color = Colors.LightGray;
        cell5 = row5.Cells[0];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("04/07/2020");
        cell5.Borders.Right.Visible = false;
      

        cell5 = row5.Cells[1];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("Beed, Jacob");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[2];
        cell5.Format.Font.Size = 9;
        cell5.Format.Font.Bold = true;
        paragraph = cell5.AddParagraph("Service Ticket:");
        paragraph.AddFormattedText(" 41410", TextFormat.NotBold );
        cell5.AddParagraph();
        paragraph2 = cell5.AddParagraph("Summary:");
        paragraph2.AddFormattedText(" Error Message on Outlook", TextFormat.NotBold);
        cell5.AddParagraph();
        paragraph3 = cell5.AddParagraph("");
        paragraph3.AddFormattedText("Remote into Wendy's computer and checked the internet calendars. There weren't any" +
            "listed. Wendy said she hasn't got the error message again but she isn't receiving emails until she logs out" +
            "and logs back in. Then, I created a new outlook profile and set it as default. The email issue should be resolved.", TextFormat.NotBold);
            
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[3];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("Y");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[4];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("0.50");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[5];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("125.00");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[6];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("$62.50");


        row5 = timeDetailTable.AddRow();
        cell5 = row5.Cells[0];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("04/08/2020");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[1];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("Beed, Jacob");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[2];
        cell5.Format.Font.Size = 9;
        cell5.Format.Font.Bold = true;
        paragraph = cell5.AddParagraph("Service Ticket:");
        paragraph.AddFormattedText(" 41410", TextFormat.NotBold);
        cell5.AddParagraph();
        paragraph2 = cell5.AddParagraph("Summary:");
        paragraph2.AddFormattedText(" Error Message on Outlook", TextFormat.NotBold);
        cell5.AddParagraph();
        paragraph3 = cell5.AddParagraph("");
        paragraph3.AddFormattedText("After creating a new outlook profile, there were some of her sent emails that were missing. While trying" +
            "to resolve the issue I ended up switching between profiles a few times and the missing sent emails have returned on the old profile. I sent" +
            "a test email and it was received. She will continue to monitor for issues but seems to be resolved for now.", TextFormat.NotBold);
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[3];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("Y");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[4];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("0.50");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[5];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("125.00");
        cell5.Borders.Right.Visible = false;

        cell5 = row5.Cells[6];
        cell5.Format.Font.Size = 9;
        cell5.AddParagraph("$62.50");

        //adding horizontal line 
        row5 = timeDetailTable.AddRow();
        
        for(int i = 0; i <= 6; i++)
        {
            cell5 = row5.Cells[i];
            cell5.Borders.Visible = false;
        }



        row5 = timeDetailTable.AddRow();
        cell5 = row5.Cells[5];
        cell5.Format.Font.Size = 9;
        cell5.MergeRight = 1;
        cell5.Format.Alignment = ParagraphAlignment.Right;
        cell5.Format.Font.Bold = true;
        cell5.AddParagraph("Subtotal: $187.50");

        
        for(int i = 0; i <= 6; i++)
        {
            cell5 = row5.Cells[i];
            cell5.Borders.Bottom.Visible = false;
            cell5.Borders.Left.Visible = false;
            cell5.Borders.Right.Visible = false;
        }

        //adding horizontal line 
        row5 = timeDetailTable.AddRow();
        row5.Borders.Visible = false;
        


        Table timeDetailTable2 = section.AddTable();
        timeDetailTable2.Rows.LeftIndent = "-1cm";
        timeDetailTable2.Borders.Width = 0.75;

        Column column6 = timeDetailTable2.AddColumn(Unit.FromCentimeter(2));
        column6.Format.Alignment = ParagraphAlignment.Center;
        column6 = timeDetailTable2.AddColumn(Unit.FromCentimeter(3));
        column6.Format.Alignment = ParagraphAlignment.Left;
        column6 = timeDetailTable2.AddColumn(Unit.FromCentimeter(4.75));
        column6.Format.Alignment = ParagraphAlignment.Left;
        column6 = timeDetailTable2.AddColumn(Unit.FromCentimeter(1));
        column6.Format.Alignment = ParagraphAlignment.Center;
        column6 = timeDetailTable2.AddColumn(Unit.FromCentimeter(1.5));
        column6.Format.Alignment = ParagraphAlignment.Center;
        column6 = timeDetailTable2.AddColumn(Unit.FromCentimeter(3.75));
        column6.Format.Alignment = ParagraphAlignment.Left;
        column6 = timeDetailTable2.AddColumn(Unit.FromCentimeter(2));
        column6.Format.Alignment = ParagraphAlignment.Right;

        Row row6 = timeDetailTable2.AddRow();
        row6.KeepWith = 3;
        Cell cell6 = row6.Cells[0];
        cell6.Format.Font.Size = 9;
        cell6.Format.Alignment = ParagraphAlignment.Left;
        cell6.Shading.Color = Colors.LightGray;
        cell6.MergeRight = 6;
        cell6.AddParagraph("Charge To: ADV Integrity / New e-mail needed, please, ASAP   Location: NO NOT USE");
        cell6.Format.Font.Bold = true;

        row6 = timeDetailTable2.AddRow();
        cell6 = row6.Cells[0];
        cell6.AddParagraph("Date");



        cell6 = row6.Cells[1];
        cell6.AddParagraph("Staff");

        cell6 = row6.Cells[2];
        cell6.AddParagraph("Notes");

        cell6 = row6.Cells[3];
        cell6.AddParagraph("Bill");

       cell6 = row6.Cells[4];
        cell6.AddParagraph("Hours");

        cell6 = row6.Cells[5];
        cell6.AddParagraph("Rate");

        cell6 = row6.Cells[6];

        cell6.AddParagraph("Ext Amt");

        for (int i = 0; i <= 6; i++)
        {
            cell6 = row6.Cells[i];
            cell6.Borders.Left.Visible = false;
            cell6.Borders.Right.Visible = false;
            cell6.Borders.Top.Visible = false;
            cell6.Format.Font.Bold = true;
            cell6.Format.Font.Size = 9;
        }

        row6 = timeDetailTable2.AddRow();
        cell6 = row6.Cells[0];
        cell6.AddParagraph();
        cell6.MergeRight = 6;
        cell6.Borders.Bottom.Visible = false;
        cell6.Row.Borders.Visible = false;

        row6 = timeDetailTable2.AddRow();
        cell6 = row6.Cells[0];
        cell6.Format.Font.Size = 9;
        cell6.AddParagraph("04/09/2020");
        cell6.Borders.Right.Visible = false;

        cell6 = row6.Cells[1];
        cell6.Format.Font.Size = 9;
        cell6.AddParagraph("Beed, Jacob");
        cell6.Borders.Right.Visible = false;

        cell6 = row6.Cells[2];
        cell6.Format.Font.Size = 9;
        cell6.Format.Font.Bold = true;
        paragraph = cell6.AddParagraph("Service Ticket:");
        paragraph.AddFormattedText(" 41466", TextFormat.NotBold);
        cell6.AddParagraph();
        paragraph2 = cell6.AddParagraph("Summary:");
        paragraph2.AddFormattedText(" New e-mail needed please, ASAP", TextFormat.NotBold);
        cell6.AddParagraph();
        paragraph3 = cell6.AddParagraph("");
        paragraph3.AddFormattedText("Created new email address for webinars and assigned exchange online" +
            "license. Mailbox will be available shortly.", TextFormat.NotBold);
        cell6.Borders.Right.Visible = false;

        

        cell6 = row6.Cells[3];
        cell6.Format.Font.Size = 9;
        cell6.AddParagraph("Y");
        cell6.Borders.Right.Visible = false;
        

        cell6 = row6.Cells[4];
        cell6.Format.Font.Size = 9;
        cell6.AddParagraph("0.50");
        cell6.Borders.Right.Visible = false;

        cell6 = row6.Cells[5];
        cell6.Format.Font.Size = 9;
        cell6.AddParagraph("125.00");
        cell6.Borders.Right.Visible = false;

        cell6 = row6.Cells[6];
        cell6.Format.Font.Size = 9;
        cell6.AddParagraph("$62.50");

        //adding horizontal line 
        row5 = timeDetailTable2.AddRow();
        row5.Borders.Visible = false;


        row6 = timeDetailTable2.AddRow();

        for(int i = 0; i <= 5; i++)
        {
            cell6 = row6.Cells[i];
            cell6.Borders.Right.Visible = false;
        }

        cell6 = row6.Cells[5];
        cell6.Format.Font.Size = 9;
        cell6.MergeRight = 1;
        cell6.Format.Alignment = ParagraphAlignment.Right;
        cell6.Format.Font.Bold = true;
        cell6.AddParagraph("Subtotal: $62.50");

        for (int i = 0; i <= 6; i++)
        {
            cell6 = row6.Cells[i];
            cell6.Borders.Bottom.Visible = false;
            cell6.Borders.Left.Visible = false;
            cell6.Borders.Right.Visible = false;
        }

        //adding horizontal line 
        row6 = timeDetailTable2.AddRow();
        row6.Borders.Visible = false;

Я создал в общей сложности 8 таблиц, поэтому вы можете увидеть, сколько времени это занимает есть и сколько у меня было кода, который составлял около 1400 строк. Опять же, есть ли более простой способ сделать это?

1 Ответ

0 голосов
/ 22 июня 2020

Если таблицы выглядят одинаково, просто создайте процедуру CreateTable, которая получает массив заголовков столбцов и массив ширины столбцов.

Вы можете определить стили для строк заголовков, нечетных строк и даже ряды. Затем вы просто назначаете свойство Style для строки вместо того, чтобы назначать имя шрифта, размер шрифта, жирность шрифта и другие свойства для каждой ячейки. Это также уменьшает код.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...