public TDS2(DataTDS2 Data, Client NewClient, BackgroundWorker bw, string owner, string location, string device) { try { Owner = owner; Location = location; Device = device; _client = NewClient; #region start Word //Create an instance for word app _wordDocument = new Microsoft.Office.Interop.Word.Application(); //Set animation status for word application //2013 //_wordDocument.ShowAnimation = false; //Set status for word application is to be visible or not. _wordDocument.Visible = false; _missing = System.Reflection.Missing.Value; //Create a new document _document = _wordDocument.Documents.Add(ref _missing, ref _missing, ref _missing, ref _missing); //Поля _document.PageSetup.RightMargin = 35; _document.PageSetup.LeftMargin = 35; _document.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphJustify; _wordDocument.ActiveWindow.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle; _wordDocument.ActiveWindow.Selection.ParagraphFormat.SpaceAfter = 0.0F; bw.ReportProgress(30); #endregion #region Header //Add header into the document foreach (Microsoft.Office.Interop.Word.Section section in _document.Sections) { //Get the header range and add the header details. Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage); headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; headerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlack; headerRange.Font.Name = "Times New Roman"; //headerRange.Font.ColorIndexBi = headerRange.Font.Size = 14; headerRange.Text = "КГБУЗ «КМКБ №20 им. И. С. Берзона»\n" + "Дуплексное сканирование с ЦДК аорты и висцеральных ветвей"; } _document.Content.SetRange(1, 1); #endregion Microsoft.Office.Interop.Word.Paragraph mainParagraph = _document.Content.Paragraphs.Add(ref _missing); mainParagraph.Range.Text = ""; mainParagraph.Range.Font.Size = 12; mainParagraph.Range.Font.Name = "Times New Roman"; mainParagraph.Range.InsertParagraphAfter(); #region InfoTable Table infotable = _document.Tables.Add(mainParagraph.Range, 3, 2, ref _missing, ref _missing); infotable.Rows.Alignment = WdRowAlignment.wdAlignRowCenter; infotable.Cell(1, 1).Range.Text = $"Дата {DateTime.Now.ToShortDateString()}"; infotable.Cell(1, 2).Range.Text = $"Аппарат: {Device}"; infotable.Cell(2, 1).Range.Text = $"Ф.И.О. {NewClient.Name}"; infotable.Cell(2, 2).Range.Text = $"Условия локации: {Location}"; infotable.Cell(3, 1).Range.Text = NewClient.HistoryNumber.Trim().Length != 0 ? $"№ истории болезни {NewClient.HistoryNumber}\tВозраст {NewClient.Age}" : $"Возраст {NewClient.Age}"; infotable.Cell(3, 2).Range.Text = $"Врач: {Owner}"; foreach (Row row in infotable.Rows) { foreach (Cell cell in row.Cells) { cell.Range.Font.Size = 12; cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; if (cell.ColumnIndex == 1) { cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;// WdParagraphAlignment.wdAlignParagraphCenter; } if (cell.ColumnIndex == 2) { cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;// WdParagraphAlignment.wdAlignParagraphCenter; } } } #endregion mainParagraph.Range.InsertParagraphAfter(); bw.ReportProgress(20); Microsoft.Office.Interop.Word.Paragraph par1 = _document.Content.Paragraphs.Add(ref _missing); par1.Range.Font.Size = 12; par1.Range.Font.Name = "Times New Roman"; par1.Range.Bold = 1; par1.Range.Text = $" Аорта"; par1.Range.InsertParagraphAfter(); par1.Range.InsertParagraphAfter(); par1.Range.Bold = 0; par1.Range.Text = $" {Data.Paragraphs[0]}"; par1.Range.InsertParagraphAfter(); par1.Range.InsertParagraphAfter(); par1.Range.Text = $" {Data.Paragraphs[1]}"; par1.Range.InsertParagraphAfter(); par1.Range.InsertParagraphAfter(); par1.Range.Text = $" {Data.Paragraphs[2]}"; par1.Range.InsertParagraphAfter(); par1.Range.InsertParagraphAfter(); par1.Range.Text = $" {Data.Paragraphs[3]}"; par1.Range.InsertParagraphAfter(); par1.Range.InsertParagraphAfter(); if (Data.Paragraphs[4] != "") { par1.Range.Text = $" Дополнительные данные: {Data.Paragraphs[4]}"; par1.Range.InsertParagraphAfter(); par1.Range.InsertParagraphAfter(); } bw.ReportProgress(40); #region 1stTable //Table header Microsoft.Office.Interop.Word.Paragraph titleTable = _document.Content.Paragraphs.Add(ref _missing); titleTable.Range.Font.Size = 12; titleTable.Range.Font.Name = "Times New Roman"; titleTable.Range.Font.Bold = 1; // titleTable.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; ; titleTable.Range.Text = "\t\tКоличественные характеристики кровотока в висцеральных аретриях"; titleTable.Range.InsertParagraphAfter(); titleTable.Range.InsertParagraphAfter(); int Table1NumRows = 5; int Table1NumCols = 4; Table Table1 = _document.Tables.Add(mainParagraph.Range, Table1NumRows, Table1NumCols, ref _missing, ref _missing); Table1.Borders.Enable = 1; Table1.Rows.Alignment = WdRowAlignment.wdAlignRowCenter; for (int i = 0; i < Data.TableColumns.Count; i++) { Table1.Cell(1, i + 1).Range.Text = Data.TableColumns[i]; } for (int i = 0; i < Data.Table.Count; i++) { Table1.Cell(i + 2, 1).Range.Text = Data.Table[i].Name; var criterias = Data.Table[i].GetVluesList(); for (int j = 0; j < Table1NumCols - 1; j++) { Table1.Cell(i + 2, j + 2).Range.Text = criterias[j]; } } foreach (Row row in Table1.Rows) { foreach (Cell cell in row.Cells) { cell.Range.Font.Size = 11; cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; cell.Height = 30;// WdRowHeightRule.wdRowHeightAtLeast; if (cell.ColumnIndex == 1) { cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; cell.Width = 120f; } else { cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; cell.Width = 95f; } } } #endregion mainParagraph.Range.InsertParagraphAfter(); bw.ReportProgress(60); if (Data.Paragraphs[4] != "") { par1.Range.Text = $"Заключение: {Data.Paragraphs[5]}"; par1.Range.InsertParagraphAfter(); } bw.ReportProgress(85); titleTable.Range.InsertParagraphAfter(); bw.ReportProgress(90); } catch (Exception ex) { _wordDocument?.Quit(ref _missing, ref _missing, ref _missing); _wordDocument = null; // Compose a string that consists of three lines. string lines = ex.ToString(); // Write the string to a file. System.IO.StreamWriter file = new System.IO.StreamWriter($@"{Directory.GetCurrentDirectory()}\err.txt"); file.WriteLine(lines); file.Close(); } }
private void FormatingDocument() { try { var client = new Client { Age = (int)((dateTimePicker1.Value - DateTime.Now).Duration().Days / 365.2425), Name = ClientTextBox.Text, HistoryNumber = HistoryTextBox.Text }; FileWorker.ReportProgress(5); switch (_selectedTab) { case 0: var tdsData = new DataTDS(gridTDS1.DataSource as List <Criteria>, gridTDS2.DataSource as List <Criteria>, gridTDS3.DataSource as List <Criteria>); FileWorker.ReportProgress(20); tdsData.P1 = metroTextBox1.Text; tdsData.P2 = metroTextBox2.Text; tdsData.P3 = metroTextBox3.Text; _currentDocument = new TDS(tdsData, client, FileWorker, _owner, _location, _device); break; case 1: var tds1 = new DataTDS1(gridControl2.DataSource as List <Criteria>); FileWorker.ReportProgress(20); var paragraphs1 = new string[7]; paragraphs1[0] = $"Диаметр на уровне висцеральных ветвей {metroTextBox14.Text.Trim()} мм, на уровне бифуркации {metroTextBox13.Text.Trim()} мм."; var wall1 = metroCheckBox2.Checked ? metroCheckBox2.Text + ", " : ""; paragraphs1[1] = $"Стенка: {wall1}АБС ({GetCheckedValueFromPanel(metroPanel4)})."; paragraphs1[2] = $"Просвет: {GetCheckedValueFromPanel(metroPanel5)}."; paragraphs1[3] = $"Кровоток Vps {metroTextBox12.Text.Trim()} см/сек, магистральный {metroTextBox11.Text.Trim()} см/сек."; paragraphs1[4] = metroTextBox10.Text.Trim(); paragraphs1[5] = $"RAR = {metroTextBox16.Text.Trim()}."; paragraphs1[6] = metroTextBox15.Text.Trim(); tds1.Paragraphs = paragraphs1; _currentDocument = new TDS1(tds1, client, FileWorker, _owner, _location, _device); break; case 2: var tds2 = new DataTDS2(gridControl1.DataSource as List <Criteria>); FileWorker.ReportProgress(20); var paragraphs2 = new string[6]; paragraphs2[0] = $"Диаметр на уровне висцеральных ветвей {metroTextBox4.Text.Trim()} мм, на уровне бифуркации {metroTextBox5.Text.Trim()} мм."; var wall = metroCheckBox1.Checked ? metroCheckBox1.Text + ", " : ""; paragraphs2[1] = $"Стенка: {wall}АБС ({GetCheckedValueFromPanel(metroPanel2)})."; paragraphs2[2] = $"Просвет: {GetCheckedValueFromPanel(metroPanel3)}."; paragraphs2[3] = $"Кровоток Vps {metroTextBox6.Text.Trim()} см/сек, магистральный {metroTextBox7.Text.Trim()} см/сек."; paragraphs2[4] = metroTextBox8.Text.Trim(); paragraphs2[5] = metroTextBox9.Text.Trim(); tds2.Paragraphs = paragraphs2; _currentDocument = new TDS2(tds2, client, FileWorker, _owner, _location, _device); break; default: break; } FileWorker.ReportProgress(95); _canPreview = _currentDocument.SavePreview(); FileWorker.ReportProgress(100); } catch (Exception ex) { } }