private List <Paragraph> MultipleChoiceQuestion(Question question, int number)
        {
            var paragraphs = new List <Paragraph>();

            var json    = question.Options;
            var options = JsonConvert.DeserializeObject <Dictionary <string, string> >(json);

            var run = new Run();

            var iconIfMultiple = question.QuestionTypeId == 4 ? "*" : "";
            var text           = new Text($"    {number}. {question.Title}. {iconIfMultiple}");

            text.Space = SpaceProcessingModeValues.Preserve;
            run.Append(text);

            paragraphs.Add(new Paragraph(run));

            foreach (var option in options)
            {
                var optionText = new Text($"      {option.Key} - {option.Value}");
                optionText.Space = SpaceProcessingModeValues.Preserve;
                paragraphs.Add(new Paragraph(new Run(optionText)));
            }

            paragraphs.Add(new Paragraph(new Run(new Text(""))));

            return(paragraphs);
        }
        private List <Paragraph> testRun(Question question, int number)
        {
            var run = new Run();

            var text = new Text($"{number}. {question.Title}");

            run.AppendChild(text);

            return(new List <Paragraph> {
                new Paragraph(run)
            });
        }
        private List <Paragraph> TrueOrFalseQuestion(Question question, int number)
        {
            var paragraphs = new List <Paragraph>();
            var run        = new Run();
            var text       = new Text($"    {number}. {question.Title}. _______");

            text.Space = SpaceProcessingModeValues.Preserve;
            run.Append(text);

            paragraphs.Add(new Paragraph(run));
            paragraphs.Add(new Paragraph(new Run(new Text(""))));

            return(paragraphs);
        }
        private List <Paragraph> OpenEndedAnswerQuestions(Question question, int number)
        {
            var paragraphs = new List <Paragraph>();
            var run        = new Run();

            var text = new Text($"    {number}. {question.Title}. ");

            text.Space = SpaceProcessingModeValues.Preserve;

            run.Append(text);
            paragraphs.Add(new Paragraph(run));
            paragraphs.Add(new Paragraph(new Run(new Break())));
            paragraphs.Add(new Paragraph(new Run(new Break())));

            return(paragraphs);
        }
예제 #5
0
        /// <summary>
        /// Sets the text of the OpenXmlElement and removes the default placeholder style that is associated by default with content controls.
        /// If there are new lines (\n, \r\n, \n\r) in the text, it will insert a Break between them.
        /// If no text element is found, it is created and added as a child of the element
        /// </summary>
        protected static void SetTextAndRemovePlaceholderFormat(OpenXmlElement element, string newValue)
        {
            if (newValue == null)
            {
                return;
            }

            string[] newlineArray = { Environment.NewLine, "\\r\\n", "\\n\\r", "\\n" };
            var      textArray    = newValue.Split(newlineArray, StringSplitOptions.None);

            var texts = element.Descendants <Text>().ToList();


            Text textElement = null;

            if (texts.Count > 0)
            {
                textElement = texts[0];
                texts.RemoveAt(0);
            }
            else
            {
                textElement = new Text();

                var lastRun = element.Descendants <Run>().LastOrDefault();
                if (lastRun != null)
                {
                    lastRun.AppendChild(textElement);
                }
                else
                {
                    var lastPar = element.Descendants <Paragraph>().LastOrDefault();
                    if (lastPar != null)
                    {
                        lastPar.AppendChild(new Run(textElement));
                    }
                    else
                    {
                        return;
                    }
                }
            }

            foreach (var descendant in texts)
            {
                descendant.Remove();
            }

            var textElementParent = textElement.Parent;

            textElement.Remove();

            var first = true;

            foreach (var line in textArray)
            {
                if (!first)
                {
                    textElementParent.Append(new Break());
                }

                textElementParent.Append(new Text(line));

                first = false;
            }

            //Check if the style is the default placeholder style and remove it if it is
            if (textElementParent is Run run && run.RunProperties?.RunStyle?.Val == "PlaceholderText")
            {
                run.RunProperties.RunStyle.Val = "";
            }
        }
예제 #6
0
 public override IEnumerable<OpenXmlElement> ToOpenXmlElements(DocumentFormat.OpenXml.Packaging.MainDocumentPart mainDocumentPart)
 {
     var result = new DocumentFormat.OpenXml.Wordprocessing.Text(Content);
     result.Space = SpaceProcessingModeValues.Preserve;
     return new List<OpenXmlElement> { result };
 }