Exemplo n.º 1
0
        public static OpenXmlElement CreateNewParagraph(string text, ParagraphProperties prp)
        {
            Paragraph paragraph = new Paragraph();

            paragraph.PrependChild <ParagraphProperties>((ParagraphProperties)prp.CloneNode(true));
            Run run = paragraph.AppendChild(new Run());

            run.AppendChild(new Text(text));

            return(paragraph);
        }
Exemplo n.º 2
0
    /// <summary>
    /// 增加段落
    /// </summary>
    public OpenXmlHelper AddParagraph()
    {
        Paragraph           NewPar      = new Paragraph();
        ParagraphProperties LastParProp = outDoc.MainDocumentPart.RootElement.Descendants <ParagraphProperties>().LastOrDefault();

        if (LastParProp != null)
        {
            NewPar.Append(LastParProp.CloneNode(true));
        }

        Run           LastRun     = new Run();
        RunProperties LastRunProp = outDoc.MainDocumentPart.RootElement.Descendants <RunProperties>().LastOrDefault();

        if (LastRunProp != null)
        {
            LastRun.Append(LastRunProp.CloneNode(true));
        }
        NewPar.Append(LastRun);
        outBody.Append(NewPar);
        //outBody.Append(new Paragraph(new Run()));
        return(this);
    }
Exemplo n.º 3
0
        /// <summary>
        /// Creates the paragraph.
        /// </summary>
        /// <param name="openXmlCompositeElement">The open XML composite element.</param>
        /// <param name="runs">The runs.</param>
        /// <returns></returns>
        private static Paragraph CreateParagraph(OpenXmlCompositeElement openXmlCompositeElement, List <Run> runs)
        {
            ParagraphProperties paragraphProperties = openXmlCompositeElement.Descendants <ParagraphProperties>().FirstOrDefault();
            Paragraph           para = null;

            if (paragraphProperties != null)
            {
                para = new Paragraph(paragraphProperties.CloneNode(true));
                foreach (Run run in runs)
                {
                    para.AppendChild <Run>(run);
                }
            }
            else
            {
                para = new Paragraph();
                foreach (Run run in runs)
                {
                    para.AppendChild <Run>(run);
                }
            }
            return(para);
        }
Exemplo n.º 4
0
        public static FileStream GeneratePersonalGuidanceReport(List <SpecialPerson> persons,
                                                                MilitaryComissariat militaryComissariat)
        {
            const string templateFile = TemplatePath + "PersonalGuidance/person_report.docx";
            var          tempFile     = TempPath + $"PersonalGuidance/{militaryComissariat.Name}.docx";

            if (!Directory.Exists(TempPath + "PersonalGuidance"))
            {
                Directory.CreateDirectory(TempPath + "PersonalGuidance");
            }
            CopyTemplateFileToTempDirectory(templateFile, tempFile);
            var replaceTextDictionary = new Dictionary <string, string>
            {
                { "MilitaryComissariat", militaryComissariat.Name },
            };

            using (var document = WordprocessingDocument.Open(tempFile, true))
            {
                try
                {
                    var bookMarks = FindBookmarks(document.MainDocumentPart.Document);
                    RemoveBookMarkContent(bookMarks);
                    var runFonts = new RunFonts()
                    {
                        Ascii = "Times New Roman", HighAnsi = "Times New Roman"
                    };
                    var fontSize = new FontSize()
                    {
                        Val = "28"
                    };
                    var fontSizeComplexScript = new FontSizeComplexScript()
                    {
                        Val = "28"
                    };
                    var runProperties = new RunProperties(new Bold(), runFonts, fontSize, fontSizeComplexScript);
                    foreach (var(bookmarkStart, bookmarkEnd) in bookMarks)
                    {
                        if (!replaceTextDictionary.Select(m => m.Key).Contains <string>(bookmarkStart.Name))
                        {
                            continue;
                        }
                        var replaceText = replaceTextDictionary.First(m => m.Key == bookmarkStart.Name).Value;


                        var run = new Run();

                        var text = new Text(replaceText);
                        run.Append(runProperties.CloneNode(true), text);
                        bookmarkEnd.InsertAfterSelf(run);
                    }

                    var body          = document.MainDocumentPart.Document.Body;
                    var index         = 1;
                    var runFontsTable = new RunFonts()
                    {
                        Ascii = "Times New Roman", HighAnsi = "Times New Roman"
                    };
                    var fontSizeTable = new FontSize()
                    {
                        Val = "24"
                    };
                    var fontSizeComplexScriptTable = new FontSizeComplexScript()
                    {
                        Val = "24"
                    };
                    var runPropertiesTable =
                        new RunProperties(runFontsTable, fontSizeTable, fontSizeComplexScriptTable);
                    var paragraphProperties = new ParagraphProperties(new SpacingBetweenLines()
                    {
                        After = "0"
                    });

                    foreach (var table in body.Descendants <Table>())
                    {
                        foreach (var person in persons)
                        {
                            var tableCellIndex = new TableCell(new Paragraph(paragraphProperties.CloneNode(true),
                                                                             new Run(runPropertiesTable.CloneNode(true), new Text(index.ToString()))));
                            var tableCellFullName = new TableCell(new Paragraph(paragraphProperties.CloneNode(true),
                                                                                new Run(runPropertiesTable.CloneNode(true), new Text(person.FullName))));
                            var tableCellBirthYear = new TableCell(new Paragraph(paragraphProperties.CloneNode(true),
                                                                                 new Run(runPropertiesTable.CloneNode(true), new Text(person.BirthYear.ToString()))));
                            var tableCellMilitaryUnitCode = new TableCell(new Paragraph(
                                                                              paragraphProperties.CloneNode(true),
                                                                              new Run(runPropertiesTable.CloneNode(true),
                                                                                      new Text(person.Requirement.MilitaryUnitCode))));
                            var tableCellMilitaryUnitName = new TableCell(new Paragraph(
                                                                              paragraphProperties.CloneNode(true),
                                                                              new Run(runPropertiesTable.CloneNode(true),
                                                                                      new Text(person.Requirement.MilitaryUnit.Name))));
                            var tableCellSendDate = new TableCell(new Paragraph(paragraphProperties.CloneNode(true),
                                                                                new Run(runPropertiesTable.CloneNode(true), new Text(person.Notice))));
                            var tableRow = new TableRow();
                            tableRow.Append(tableCellIndex, tableCellFullName, tableCellBirthYear,
                                            tableCellMilitaryUnitCode, tableCellMilitaryUnitName, tableCellSendDate);
                            table.AppendChild(tableRow);
                            index++;
                        }
                    }
                    document.MainDocumentPart.Document.Save();
                }
                catch
                {
                    document.MainDocumentPart.Document.Save();
                    throw new Exception("Неизвестная ошибка");
                }
            }

            return(new FileStream(tempFile, FileMode.Open));
        }
Exemplo n.º 5
0
        public static FileStream GenerateMedicalTestProtocol(List <Recruit> recruits, string teamNumber,
                                                             DateTime sendDate)
        {
            const string templateFile = TemplatePath + "MedicalTest/Protocol.docx";
            const string tempFile     = TempPath + "Protocol.docx";

            CopyTemplateFileToTempDirectory(templateFile, tempFile);
            var replaceTextDictionary = new Dictionary <string, string>
            {
                { "TEAMNUM", teamNumber },
                { "CURRENTDATE", sendDate.Day.ToString() },
                { "CURRENTMONTH", sendDate.GetMonthName() },
                { "CURRENTYEAR", sendDate.Year.ToString() },
                {
                    "RESULT",
                    $"в {recruits.Count} исследуемых пробах из {recruits.Count} не обнаружены антитела SARS-CoV-2."
                }
            };

            using (var document = WordprocessingDocument.Open(tempFile, true))
            {
                try
                {
                    var bookMarks = FindBookmarks(document.MainDocumentPart.Document);
                    RemoveBookMarkContent(bookMarks);
                    var runFonts = new RunFonts()
                    {
                        Ascii = "Times New Roman", HighAnsi = "Times New Roman"
                    };
                    var fontSize = new FontSize()
                    {
                        Val = "24"
                    };
                    var fontSizeComplexScript = new FontSizeComplexScript()
                    {
                        Val = "24"
                    };
                    var runProperties = new RunProperties(new Bold(), runFonts, fontSize, fontSizeComplexScript);
                    foreach (var(bookmarkStart, bookmarkEnd) in bookMarks)
                    {
                        if (!replaceTextDictionary.Select(m => m.Key).Contains <string>(bookmarkStart.Name))
                        {
                            continue;
                        }
                        var replaceText = replaceTextDictionary.First(m => m.Key == bookmarkStart.Name).Value;


                        var run = new Run();

                        var text = new Text(replaceText);
                        run.Append(runProperties.CloneNode(true), text);
                        bookmarkEnd.InsertAfterSelf(run);
                    }

                    var body          = document.MainDocumentPart.Document.Body;
                    var index         = 1;
                    var runFontsTable = new RunFonts()
                    {
                        Ascii = "Times New Roman", HighAnsi = "Times New Roman"
                    };
                    var fontSizeTable = new FontSize()
                    {
                        Val = "22"
                    };
                    var fontSizeComplexScriptTable = new FontSizeComplexScript()
                    {
                        Val = "22"
                    };
                    var runPropertiesTable =
                        new RunProperties(runFontsTable, fontSizeTable, fontSizeComplexScriptTable);
                    var paragraphProperties = new ParagraphProperties(new SpacingBetweenLines()
                    {
                        After = "0"
                    });

                    foreach (var table in body.Descendants <Table>())
                    {
                        foreach (var recruit in recruits)
                        {
                            var tableCellIndex = new TableCell(new Paragraph(paragraphProperties.CloneNode(true),
                                                                             new Run(runPropertiesTable.CloneNode(true), new Text(index.ToString()))));
                            var tableCellFullName = new TableCell(new Paragraph(paragraphProperties.CloneNode(true),
                                                                                new Run(runPropertiesTable.CloneNode(true), new Text(recruit.FullName))));
                            var tableCellDisc = new TableCell(new Paragraph(paragraphProperties.CloneNode(true),
                                                                            new Run(runPropertiesTable.CloneNode(true), new Text("антитела SARS-CoV-2"))));
                            var tableCellTestNum = new TableCell(new Paragraph(paragraphProperties.CloneNode(true),
                                                                               new Run(runPropertiesTable.CloneNode(true), new Text(
                                                                                           $"{recruit.AdditionalData.TestNum} от {recruit.AdditionalData.TestDate?.ToShortDateString()}"))));
                            var tableCellResult = new TableCell(new Paragraph(paragraphProperties.CloneNode(true),
                                                                              new Run(runPropertiesTable.CloneNode(true), new Text("не обнаружены"))));
                            var tableRow = new TableRow();
                            tableRow.Append(tableCellIndex, tableCellFullName, tableCellDisc, tableCellTestNum,
                                            tableCellResult);
                            table.AppendChild(tableRow);
                            index++;
                        }

                        var nullTCell = new TableCell(new Paragraph(paragraphProperties.CloneNode(true),
                                                                    new Run(new Text(""))));
                        var nullTRow = new TableRow(nullTCell.CloneNode(true),
                                                    nullTCell.CloneNode(true), nullTCell.CloneNode(true),
                                                    nullTCell.CloneNode(true), nullTCell.CloneNode(true));
                        table.AppendChild(nullTRow.CloneNode(true));
                        table.AppendChild(nullTRow.CloneNode(true));
                        table.AppendChild(nullTRow.CloneNode(true));
                    }
                    document.MainDocumentPart.Document.Save();
                }
                catch
                {
                    document.MainDocumentPart.Document.Save();
                    throw new Exception("Неизвестная ошибка");
                }
            }

            return(new FileStream(tempFile, FileMode.Open));
        }