void InsertColumnNumbers(TableRow row)
 {
     foreach (TableCell _cell in row.Cells)
     {
         if (_cell.Index == 0)
         {
             continue;
         }
         _cell.BackgroundColor = Color.Empty;
         SubDocument   doc   = _cell.Range.BeginUpdateDocument();
         DocumentRange range = doc.InsertSingleLineText(_cell.Range.Start, String.Format("{0}", _cell.Index + 1));
         range.EndUpdateDocument(doc);
     }
 }
        static void buttonCustomAction_ItemClick_Replace(object sender, ItemClickEventArgs e)
        {
            RichEditControl richEdit  = e.Item.Tag as RichEditControl;
            DocumentRange   range     = richEdit.Document.Selection;
            int             selLength = range.Length;
            string          s         = new String('*', selLength);
            SubDocument     doc       = range.BeginUpdateDocument();

            doc.InsertSingleLineText(range.Start, s);
            DocumentRange rangeToRemove = doc.CreateRange(range.Start, selLength);

            doc.Delete(rangeToRemove);
            range.EndUpdateDocument(doc);
        }
 void InsertColumnNumbers(TableRow row)
 {
     foreach (TableCell _cell in row.Cells)
     {
         if (_cell.Index == 0)
         {
             continue;
         }
         _cell.BackgroundColor = Colors.Transparent;
         SubDocument         doc   = _cell.Range.BeginUpdateDocument();
         DocumentRange       range = doc.InsertSingleLineText(_cell.Range.Start, String.Format("{0}", _cell.Index + 1));
         CharacterProperties cp    = doc.BeginUpdateCharacters(range);
         cp.Bold      = true;
         cp.FontName  = "Comic Sans";
         cp.FontSize  = 12;
         cp.ForeColor = Colors.Red;
         doc.EndUpdateCharacters(cp);
         range.EndUpdateDocument(doc);
     }
 }
        public void SendEmail()
        {
            DataTable zamok = Zamowienia_DoWyslaniaOK().Result;

            foreach (DataRow r in zamok.Rows)
            {
                #region Pobieranie danych i tworzenie ciała maila.
                rec1.CreateNewDocument();
                rec1.LoadDocument("Zamowienie_OK.docx");
                string Status_Zam = "";
                string Producent;
                string PH_Producent;
                string Numer_Zam;
                string FileDate;
                string ZamDate;
                string Uwagi;
                string Rodzaj_zam;
                string XL_Akronim;
                string Prod_Akronim;
                string Prod_Adres;
                if (bool.TryParse(r["Zpn_Alert"].ToString(), out bool al))
                {
                    if (al)
                    {
                        Status_Zam = "Błędy mapowania towarów. Obsługa CallCenter";
                    }
                    else
                    {
                        Status_Zam = "OK. Wysłane do Mobiusa.";
                    }
                }
                else
                {
                    Status_Zam = "OK. Wysłane do Mobiusa.";
                }
                if (r["Zpn_Producent"].ToString().Length > 1)
                {
                    Producent = r["Zpn_Producent"].ToString();
                }
                else
                {
                    Producent = "BŁĄD";
                }
                if (r["Zpn_NAZWISKO"].ToString().Length > 1)
                {
                    PH_Producent = r["Zpn_NAZWISKO"].ToString() + " " + r["Zpn_IMIE"].ToString();
                }
                else
                {
                    PH_Producent = "BŁĄD";
                }
                if (r["Zpn_NR_ZAM"].ToString().Length > 1)
                {
                    Numer_Zam = r["Zpn_NR_ZAM"].ToString();
                }
                else
                {
                    Numer_Zam = "BŁĄD";
                }
                if (DateTime.TryParse(r["Zpn_DataPliku"].ToString(), out DateTime Data_Otrz))
                {
                    FileDate = Data_Otrz.ToShortDateString() + " " + Data_Otrz.ToShortTimeString();
                }
                else
                {
                    FileDate = "BŁĄD";
                }
                if (DateTime.TryParse(r["Zpn_DATA_ZAM"].ToString(), out DateTime Data_Zam))
                {
                    ZamDate = Data_Zam.ToShortDateString() + " " + Data_Zam.ToShortTimeString();
                }
                else
                {
                    ZamDate = "BŁĄD";
                }
                if (r["Zpn_UWAGI"].ToString().Length > 1)
                {
                    Uwagi = r["Zpn_UWAGI"].ToString();
                }
                else
                {
                    Uwagi = "BŁĄD";
                }
                if (r["Zpn_TYP_ZAM"].ToString().Length > 1)
                {
                    if (r["Zpn_TYP_ZAM"].ToString() == "ZG")
                    {
                        Rodzaj_zam = "Gratisowe";
                    }
                    else
                    {
                        Rodzaj_zam = "Standard";
                    }
                }
                else
                {
                    Rodzaj_zam = "BŁĄD";
                }
                if (r["Knt_Akronim"].ToString().Length > 1)
                {
                    XL_Akronim = r["Knt_Akronim"].ToString();
                }
                else
                {
                    XL_Akronim = "BŁĄD";
                }
                if (r["Zpn_NAZWA"].ToString().Length > 1)
                {
                    Prod_Akronim = r["Zpn_NAZWA"].ToString();
                }
                else
                {
                    Prod_Akronim = "BŁĄD";
                }
                if ((r["Zpn_MIASTO"].ToString() + r["Zpn_ULICA"].ToString()).Length > 1)
                {
                    Prod_Adres = r["Zpn_MIASTO"].ToString() + ", " + r["Zpn_ULICA"].ToString();
                }
                else
                {
                    Prod_Adres = "BŁĄD";
                }
                if (Int32.TryParse(r["Zpn_Id"].ToString(), out int Zpn_id))
                {
                }
                else
                {
                    Zpn_id = 0;
                }

                rec1.Document.ReplaceAll("<@Status_Zam>", Status_Zam, SearchOptions.None);
                rec1.Document.ReplaceAll("<@Producent>", Producent, SearchOptions.None);
                rec1.Document.ReplaceAll("<@PH_Producent>", PH_Producent, SearchOptions.None);
                rec1.Document.ReplaceAll("<@Numer_Zam>", Numer_Zam, SearchOptions.None);
                rec1.Document.ReplaceAll("<@Data_Otrz>", FileDate, SearchOptions.None);
                rec1.Document.ReplaceAll("<@Data_Zam>", ZamDate, SearchOptions.None);
                rec1.Document.ReplaceAll("<@Uwagi>", Uwagi, SearchOptions.None);
                rec1.Document.ReplaceAll("<@Rodzaj_Zam>", Rodzaj_zam, SearchOptions.None);
                rec1.Document.ReplaceAll("<@Knt_XLAkr>", XL_Akronim, SearchOptions.None);
                rec1.Document.ReplaceAll("<@Knt_ZamProd>", Prod_Akronim, SearchOptions.None);
                rec1.Document.ReplaceAll("<@Knt_Adres>", Prod_Adres, SearchOptions.None);

                DocumentRange[] wyniki = rec1.Document.FindAll("<@TABELA>", SearchOptions.None);
                if (wyniki.Length > 0)
                {
                    // Paragraph par = rec1.Document.Paragraphs.Get(wyniki[0].Start);
                    DocumentPosition pos = wyniki[0].Start;
                    SubDocument      doc = pos.BeginUpdateDocument();


                    // Add the table
                    rec1.Document.Tables.Create(pos, 1, 9, AutoFitBehaviorType.AutoFitToContents);
                    // Format the table
                    Table tbl = rec1.Document.Tables[0];

                    pos.EndUpdateDocument(doc);
                    try
                    {
                        tbl.BeginUpdate();

                        CharacterProperties cp_Tbl = doc.BeginUpdateCharacters(tbl.Range);
                        cp_Tbl.FontSize = 10;
                        cp_Tbl.FontName = "Calibri";
                        doc.EndUpdateCharacters(cp_Tbl);
                        //tbl.BeginUpdate();

                        // Insert header caption and format the columns
                        tbl.Rows[0].HeightType = HeightType.Exact;
                        tbl.Rows[0].Height     = 80f;
                        doc.InsertSingleLineText(tbl[0, 0].Range.Start, "Lp.");
                        doc.InsertSingleLineText(tbl[0, 1].Range.Start, "Alert");
                        doc.InsertSingleLineText(tbl[0, 2].Range.Start, "Kod");
                        doc.InsertSingleLineText(tbl[0, 3].Range.Start, "Towar");
                        doc.InsertSingleLineText(tbl[0, 4].Range.Start, "il. szt.");
                        doc.InsertSingleLineText(tbl[0, 5].Range.Start, "Ilość");
                        doc.InsertSingleLineText(tbl[0, 6].Range.Start, "JM");
                        doc.InsertSingleLineText(tbl[0, 7].Range.Start, "Cena");
                        doc.InsertSingleLineText(tbl[0, 8].Range.Start, "Gratis");
                        tbl[0, 0].PreferredWidthType = WidthType.Fixed;
                        tbl[0, 0].PreferredWidth     = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.2f);
                        tbl[0, 0].VerticalAlignment  = TableCellVerticalAlignment.Center;
                        tbl[0, 1].PreferredWidthType = WidthType.Fixed;
                        tbl[0, 1].PreferredWidth     = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.4f);
                        tbl[0, 1].VerticalAlignment  = TableCellVerticalAlignment.Center;
                        tbl[0, 2].PreferredWidthType = WidthType.Fixed;
                        tbl[0, 2].PreferredWidth     = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.8f);
                        tbl[0, 2].VerticalAlignment  = TableCellVerticalAlignment.Center;
                        tbl[0, 3].PreferredWidthType = WidthType.Fixed;
                        tbl[0, 3].PreferredWidth     = DevExpress.Office.Utils.Units.InchesToDocumentsF(2.6f);
                        tbl[0, 3].VerticalAlignment  = TableCellVerticalAlignment.Center;
                        tbl[0, 4].PreferredWidthType = WidthType.Fixed;
                        tbl[0, 4].PreferredWidth     = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.5f);
                        tbl[0, 4].VerticalAlignment  = TableCellVerticalAlignment.Center;
                        tbl[0, 5].PreferredWidthType = WidthType.Fixed;
                        tbl[0, 5].PreferredWidth     = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.6f);
                        tbl[0, 5].VerticalAlignment  = TableCellVerticalAlignment.Center;
                        tbl[0, 6].PreferredWidthType = WidthType.Fixed;
                        tbl[0, 6].PreferredWidth     = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.3f);
                        tbl[0, 6].VerticalAlignment  = TableCellVerticalAlignment.Center;
                        tbl[0, 7].PreferredWidthType = WidthType.Fixed;
                        tbl[0, 7].PreferredWidth     = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.6f);
                        tbl[0, 7].VerticalAlignment  = TableCellVerticalAlignment.Center;
                        tbl[0, 8].PreferredWidthType = WidthType.Fixed;
                        tbl[0, 8].PreferredWidth     = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.4f);
                        tbl[0, 8].VerticalAlignment  = TableCellVerticalAlignment.Center;

                        /*
                         * //Apply formatting to the "Active Customers" cell
                         * CharacterProperties properties = rec1.Document.BeginUpdateCharacters(tbl[0, 1].ContentRange);
                         * properties.FontName = "Segoe UI";
                         * properties.FontSize = 16;
                         * document.EndUpdateCharacters(properties);
                         * ParagraphProperties alignment = document.BeginUpdateParagraphs(table[0, 1].ContentRange);
                         * alignment.Alignment = ParagraphAlignment.Center;
                         * document.EndUpdateParagraphs(alignment);
                         * table[0, 1].VerticalAlignment = TableCellVerticalAlignment.Center;
                         */

                        int       wiersz = 0;
                        DataTable dt_poz = ZamowieniaPozycje_DoWyslaniaOK(Zpn_id).Result;
                        foreach (DataRow r_poz in dt_poz.Rows)
                        {
                            wiersz++;
                            tbl.Rows.InsertAfter(wiersz - 1);
                            tbl.Rows[wiersz].HeightType = HeightType.Auto;
                            if (bool.TryParse(r_poz["Alert"].ToString(), out bool ale))
                            {
                                if (ale)
                                {
                                    doc.Images.Insert(tbl[wiersz, 1].Range.Start, DocumentImageSource.FromFile("alert.png"));
                                }
                            }
                            else
                            {
                                ale = false;
                            }
                            doc.InsertSingleLineText(tbl[wiersz, 0].Range.Start, r_poz["Poz"].ToString());
                            doc.InsertSingleLineText(tbl[wiersz, 2].Range.Start, r_poz["Kod"].ToString());
                            doc.InsertSingleLineText(tbl[wiersz, 3].Range.Start, r_poz["Towar_MAG"].ToString());
                            doc.InsertSingleLineText(tbl[wiersz, 4].Range.Start, r_poz["Ilosc_szt"].ToString());
                            doc.InsertSingleLineText(tbl[wiersz, 5].Range.Start, r_poz["Ilosc_JM"].ToString());
                            doc.InsertSingleLineText(tbl[wiersz, 6].Range.Start, r_poz["JM"].ToString().ToUpper());
                            doc.InsertSingleLineText(tbl[wiersz, 7].Range.Start, r_poz["Cena_Netto"].ToString());
                            if (bool.TryParse(r_poz["Gratis"].ToString(), out bool gratt))
                            {
                            }
                            else
                            {
                                gratt = false;
                            }
                            if (bool.TryParse(r_poz["Promocja"].ToString(), out bool dol))
                            {
                            }
                            else
                            {
                                ale = false;
                            }

                            if (gratt || dol || r["Zpn_TYP_ZAM"].ToString() == "ZG")
                            {
                                doc.Images.Insert(tbl[wiersz, 8].Range.Start, DocumentImageSource.FromFile("gift.png"));
                            }
                        }
                        //Apply formatting to the header cells
                        CharacterProperties headerRowProperties = rec1.Document.BeginUpdateCharacters(tbl.Rows[0].Range);
                        headerRowProperties.FontName = "Calibri";
                        headerRowProperties.FontSize = 10;
                        headerRowProperties.Bold     = true;
                        //headerRowProperties.ForeColor = Color.FromArgb(212, 236, 183);
                        rec1.Document.EndUpdateCharacters(headerRowProperties);

                        ParagraphProperties headerRowParagraphProperties = rec1.Document.BeginUpdateParagraphs(tbl.Rows[0].Range);
                        headerRowParagraphProperties.Alignment = ParagraphAlignment.Center;
                        float f = 0.4f;
                        headerRowParagraphProperties.LeftIndent    = f;
                        headerRowParagraphProperties.SpacingBefore = 2;
                        headerRowParagraphProperties.SpacingAfter  = 2;
                        rec1.Document.EndUpdateParagraphs(headerRowParagraphProperties);
                        //Apply formatting to Row cells
                        if (tbl.Rows.Count > 1)
                        {
                            DocumentRange       targetRange            = rec1.Document.CreateRange(tbl[1, 0].Range.Start, tbl[tbl.Rows.Count - 1, 8].Range.End.ToInt());
                            ParagraphProperties RowParagraphProperties = rec1.Document.BeginUpdateParagraphs(targetRange);

                            RowParagraphProperties.LeftIndent    = DevExpress.Office.Utils.Units.InchesToDocumentsF(0.1f);;
                            RowParagraphProperties.SpacingBefore = 8;
                            RowParagraphProperties.SpacingAfter  = 8;
                            rec1.Document.EndUpdateParagraphs(RowParagraphProperties);
                            CharacterProperties infoProperties = rec1.Document.BeginUpdateCharacters(targetRange);
                            infoProperties.FontSize = 10;
                            infoProperties.FontName = "Calibri";

                            rec1.Document.EndUpdateCharacters(infoProperties);
                        }
                        //Apply formatting to Number cells
                        for (int i = 1; i < tbl.Rows.Count; i++)
                        {
                            ParagraphProperties RrParagraphProperties = rec1.Document.BeginUpdateParagraphs(tbl[i, 4].Range);
                            RrParagraphProperties.Alignment = ParagraphAlignment.Right;
                            rec1.Document.EndUpdateParagraphs(RrParagraphProperties);
                            RrParagraphProperties           = rec1.Document.BeginUpdateParagraphs(tbl[i, 5].Range);
                            RrParagraphProperties.Alignment = ParagraphAlignment.Right;
                            rec1.Document.EndUpdateParagraphs(RrParagraphProperties);
                            RrParagraphProperties           = rec1.Document.BeginUpdateParagraphs(tbl[i, 7].Range);
                            RrParagraphProperties.Alignment = ParagraphAlignment.Right;
                            rec1.Document.EndUpdateParagraphs(RrParagraphProperties);
                            RrParagraphProperties           = rec1.Document.BeginUpdateParagraphs(tbl[i, 0].Range);
                            RrParagraphProperties.Alignment = ParagraphAlignment.Right;
                            rec1.Document.EndUpdateParagraphs(RrParagraphProperties);
                        }
                    }
                    finally
                    {
                        tbl.EndUpdate();
                        doc.BeginUpdate();
                        TableStyle tStyleMain = rec1.Document.TableStyles.CreateNew();

                        //Specify style options
                        tStyleMain.TableBorders.InsideHorizontalBorder.LineStyle = TableBorderLineStyle.Single;
                        tStyleMain.TableBorders.InsideHorizontalBorder.LineColor = Color.White;

                        tStyleMain.TableBorders.InsideVerticalBorder.LineStyle = TableBorderLineStyle.Single;
                        tStyleMain.TableBorders.InsideVerticalBorder.LineColor = Color.White;
                        tStyleMain.CellBackgroundColor = Color.FromArgb(227, 238, 220);
                        tStyleMain.Name = "MyTableStyle";

                        //Add the style to the document collection
                        rec1.Document.TableStyles.Add(tStyleMain);

                        //Create conditional styles (styles for specific table elements)
                        TableConditionalStyle myNewStyleForOddRows = tStyleMain.ConditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.OddRowBanding);
                        myNewStyleForOddRows.CellBackgroundColor = Color.FromArgb(196, 220, 182);

                        TableConditionalStyle myNewStyleForBottomRightCell = tStyleMain.ConditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.BottomRightCell);
                        myNewStyleForBottomRightCell.CellBackgroundColor = Color.FromArgb(188, 214, 201);
                        doc.EndUpdate();

                        doc.BeginUpdate();

                        // Apply a previously defined style to the table
                        tbl.Style = tStyleMain;
                        doc.EndUpdate();
                        rec1.Document.ReplaceAll("<@TABELA>", "", SearchOptions.None);
                    }
                }
                #endregion
                #region Wysyłanie Maila i update statusów
                if (r["prc_email"].ToString().Length > 0)
                {
                    string Temat = "Nowe zamówienie producenckie.";
                    try
                    {
                        MailMessage mailMessage = new MailMessage("*****@*****.**", r["prc_email"].ToString());
                        mailMessage.Subject = Temat;

                        RichEditMailMessageExporter exporter = new RichEditMailMessageExporter(rec1, mailMessage);
                        exporter.Export();

                        SmtpClient mailSender = new SmtpClient();
                        mailSender.Port                  = 587;
                        mailSender.Host                  = "mag-ol.home.pl";
                        mailSender.Timeout               = 10000;
                        mailSender.DeliveryMethod        = SmtpDeliveryMethod.Network;
                        mailSender.UseDefaultCredentials = false;
                        mailSender.Credentials           = new NetworkCredential("*****@*****.**", "!Raporty123");
                        mailSender.EnableSsl             = true;
                        mailMessage.From                 = new MailAddress("*****@*****.**");
                        //specify your login/password to log on to the SMTP server, if required
                        //mailSender.Credentials = new NetworkCredential("login", "password");
                        mailSender.Send(mailMessage);
                        int.TryParse(r["Zpn_ID"].ToString(), out int id);
                        if (id > 0)
                        {
                            SQL.UpdateDocumentMailStatus(id);
                        }
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message);
                    }
                }
                #endregion
            }
        }