Esempio n. 1
0
        static Queue <NonnullRichTextBuilder> ReadAllLinesDocx(string path)
        {
            Queue <NonnullRichTextBuilder> richTexts = new Queue <NonnullRichTextBuilder>();
            WordprocessingDocument         doc       = null;

            try
            {
                doc = WordprocessingDocument.Open(path, false);
            }
            catch (OpenXmlPackageException)
            {
                return(richTexts);
            }
            catch (System.IO.IOException)
            {
                return(richTexts);
            }
            Body body = doc.MainDocumentPart.Document.Body;

            foreach (Paragraph paragraph in body.ChildElements.OfType <Paragraph>())
            {
                DocumentFormat.OpenXml.Drawing.Blip hasImage =
                    paragraph.Descendants <DocumentFormat.OpenXml.Drawing.Blip>().FirstOrDefault();
                if (hasImage == null)
                {
                    richTexts.Enqueue(new NonnullRichTextBuilder(paragraph.InnerText));
                }
                else
                {
                    List <object> runs = new List <object>();
                    foreach (Run docRun in paragraph.ChildElements.OfType <Run>())
                    {
                        DocumentFormat.OpenXml.Drawing.Blip imgContainer =
                            docRun.Descendants <DocumentFormat.OpenXml.Drawing.Blip>().FirstOrDefault();
                        if (imgContainer == null)
                        {
                            runs.Add(docRun.InnerText);
                        }
                        else
                        {
                            string           imgId      = imgContainer.Embed.Value;
                            ImagePart        imgPart    = doc.MainDocumentPart.GetPartById(imgId) as ImagePart;
                            System.IO.Stream imgStream  = imgPart.GetStream();
                            byte[]           imgInBytes = new byte[imgStream.Length];
                            imgStream.Read(imgInBytes, 0, (int)imgStream.Length);
                            runs.Add(imgInBytes);
                        }
                    }
                    NonnullRichTextBuilder richTextRuns = new NonnullRichTextBuilder(CompactRuns(runs));
                    richTexts.Enqueue(richTextRuns);
                }
            }
            doc.Close();
            return(richTexts);
        }
Esempio n. 2
0
 public static Queue <NonnullRichTextBuilder> ReadAllLines(string path)
 {
     if (Path.GetExtension(path) == ".docx")
     {
         return(ReadAllLinesDocx(path));
     }
     else
     {
         return(NonnullRichTextBuilder.NewWith(File.ReadAllLines(path)));
     }
 }
Esempio n. 3
0
 public MultiChoiceData(Queue <NonnullRichTextBuilder> richTexts, int n_options)
 {
     if (richTexts.Count < 5)
     {
         throw new ArgumentException();
     }
     Stem    = richTexts.Dequeue();
     Options = new NonnullRichTextBuilder[n_options];
     for (int i = 0; i < n_options; ++i)
     {
         Options[i] = richTexts.Dequeue();
     }
 }
Esempio n. 4
0
 public NonnullRichText(NonnullRichTextBuilder richText)
 {
     Runs = richText.Runs.AsReadOnly();
 }