Esempio n. 1
0
        public void ApplyNewListToParagraphs()
        {
            //ExStart
            //ExFor:ListCollection.Add(ListTemplate)
            //ExSummary:Shows how to create a list by applying a new list format to a collection of paragraphs.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.Writeln("Paragraph 1");
            builder.Writeln("Paragraph 2");
            builder.Write("Paragraph 3");

            NodeCollection paras = doc.GetChildNodes(NodeType.Paragraph, true);

            Assert.AreEqual(0, paras.Count(n => (n as Paragraph).ListFormat.IsListItem));

            List list = doc.Lists.Add(ListTemplate.NumberUppercaseLetterDot);

            foreach (Paragraph paragraph in paras.OfType <Paragraph>())
            {
                paragraph.ListFormat.List            = list;
                paragraph.ListFormat.ListLevelNumber = 1;
            }

            Assert.AreEqual(3, paras.Count(n => (n as Paragraph).ListFormat.IsListItem));
            //ExEnd

            doc   = DocumentHelper.SaveOpen(doc);
            paras = doc.GetChildNodes(NodeType.Paragraph, true);

            Assert.AreEqual(3, paras.Count(n => (n as Paragraph).ListFormat.IsListItem));
            Assert.AreEqual(3, paras.Count(n => (n as Paragraph).ListFormat.ListLevelNumber == 1));
        }
Esempio n. 2
0
        public void ApplyExistingListToParagraphs()
        {
            //ExStart
            //ExFor:ListCollection.Item(Int32)
            //ExSummary:Shows how to apply list formatting of an existing list to a collection of paragraphs.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.Writeln("Paragraph 1");
            builder.Writeln("Paragraph 2");
            builder.Write("Paragraph 3");

            NodeCollection paras = doc.GetChildNodes(NodeType.Paragraph, true);

            Assert.AreEqual(0, paras.Count(n => (n as Paragraph).ListFormat.IsListItem));

            doc.Lists.Add(ListTemplate.NumberDefault);
            List list = doc.Lists[0];

            foreach (Paragraph paragraph in paras.OfType <Paragraph>())
            {
                paragraph.ListFormat.List            = list;
                paragraph.ListFormat.ListLevelNumber = 2;
            }

            Assert.AreEqual(3, paras.Count(n => (n as Paragraph).ListFormat.IsListItem));
            //ExEnd

            doc   = DocumentHelper.SaveOpen(doc);
            paras = doc.GetChildNodes(NodeType.Paragraph, true);

            Assert.AreEqual(3, paras.Count(n => (n as Paragraph).ListFormat.IsListItem));
            Assert.AreEqual(3, paras.Count(n => (n as Paragraph).ListFormat.ListLevelNumber == 2));
        }
Esempio n. 3
0
        public void RemoveBulletsFromParagraphs()
        {
            //ExStart
            //ExFor:ListFormat.RemoveNumbers
            //ExSummary:Shows how to remove bullets and numbering from all paragraphs in the main text of a section.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.ListFormat.ApplyNumberDefault();
            builder.Writeln("Numbered list item 1");
            builder.Writeln("Numbered list item 2");
            builder.Writeln("Numbered list item 3");
            builder.ListFormat.RemoveNumbers();

            NodeCollection paras = doc.GetChildNodes(NodeType.Paragraph, true);

            Assert.AreEqual(3, paras.Count(n => (n as Paragraph).ListFormat.IsListItem));

            foreach (Paragraph paragraph in paras)
            {
                paragraph.ListFormat.RemoveNumbers();
            }

            Assert.AreEqual(0, paras.Count(n => (n as Paragraph).ListFormat.IsListItem));
            //ExEnd
        }
Esempio n. 4
0
        public void ExtractImagesToFiles()
        {
            //ExStart
            //ExFor:Shape
            //ExFor:Shape.ImageData
            //ExFor:Shape.HasImage
            //ExFor:ImageData
            //ExFor:FileFormatUtil.ImageTypeToExtension(ImageType)
            //ExFor:ImageData.ImageType
            //ExFor:ImageData.Save(String)
            //ExFor:CompositeNode.GetChildNodes(NodeType, bool)
            //ExSummary:Shows how to extract images from a document and save them as files.
            Document doc = new Document(MyDir + "Images.docx");

            NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

            Assert.AreEqual(9, shapes.Count(s => ((Shape)s).HasImage));

            int imageIndex = 0;

            foreach (Shape shape in shapes.OfType <Shape>())
            {
                if (shape.HasImage)
                {
                    string imageFileName =
                        $"File.ExtractImagesToFiles.{imageIndex}{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}";
                    shape.ImageData.Save(ArtifactsDir + imageFileName);
                    imageIndex++;
                }
            }
            //ExEnd

            Assert.AreEqual(9, Directory.GetFiles(ArtifactsDir).
                            Count(s => Regex.IsMatch(s, @"^.+\.(jpeg|png|emf|wmf)$") && s.StartsWith(ArtifactsDir + "File.ExtractImagesToFiles")));
        }
Esempio n. 5
0
        public void ExtractImages()
        {
            //ExStart
            //ExFor:Shape
            //ExFor:Shape.ImageData
            //ExFor:Shape.HasImage
            //ExFor:ImageData
            //ExFor:FileFormatUtil.ImageTypeToExtension(ImageType)
            //ExFor:ImageData.ImageType
            //ExFor:ImageData.Save(String)
            //ExFor:CompositeNode.GetChildNodes(NodeType, bool)
            //ExSummary:Shows how to extract images from a document, and save them to the local file system as individual files.
            Document doc = new Document(MyDir + "Images.docx");

            // Get the collection of shapes from the document,
            // and save the image data of every shape with an image as a file to the local file system.
            NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

            Assert.AreEqual(9, shapes.Count(s => ((Shape)s).HasImage));

            int imageIndex = 0;

            foreach (Shape shape in shapes.OfType <Shape>())
            {
                if (shape.HasImage)
                {
                    // The image data of shapes may contain images of many possible image formats.
                    // We can determine a file extension for each image automatically, based on its format.
                    string imageFileName =
                        $"File.ExtractImages.{imageIndex}{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}";
                    shape.ImageData.Save(ArtifactsDir + imageFileName);
                    imageIndex++;
                }
            }
            //ExEnd

            Assert.AreEqual(9, Directory.GetFiles(ArtifactsDir).
                            Count(s => Regex.IsMatch(s, @"^.+\.(jpeg|png|emf|wmf)$") && s.StartsWith(ArtifactsDir + "File.ExtractImages")));
        }
Esempio n. 6
0
        public void DetectBulletedParagraphs()
        {
            //ExStart
            //ExFor:Paragraph.ListFormat
            //ExFor:ListFormat.IsListItem
            //ExFor:CompositeNode.GetText
            //ExFor:List.ListId
            //ExSummary:Shows how to output all paragraphs in a document that are bulleted or numbered.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.ListFormat.ApplyNumberDefault();
            builder.Writeln("Numbered list item 1");
            builder.Writeln("Numbered list item 2");
            builder.Writeln("Numbered list item 3");
            builder.ListFormat.RemoveNumbers();

            builder.ListFormat.ApplyBulletDefault();
            builder.Writeln("Bulleted list item 1");
            builder.Writeln("Bulleted list item 2");
            builder.Writeln("Bulleted list item 3");
            builder.ListFormat.RemoveNumbers();

            NodeCollection paras = doc.GetChildNodes(NodeType.Paragraph, true);

            foreach (Paragraph para in paras.OfType <Paragraph>().Where(p => p.ListFormat.IsListItem))
            {
                Console.WriteLine($"This paragraph belongs to list ID# {para.ListFormat.List.ListId}, number style \"{para.ListFormat.ListLevel.NumberStyle}\"");
                Console.WriteLine($"\t\"{para.GetText().Trim()}\"");
            }
            //ExEnd

            doc   = DocumentHelper.SaveOpen(doc);
            paras = doc.GetChildNodes(NodeType.Paragraph, true);

            Assert.AreEqual(6, paras.Count(n => (n as Paragraph).ListFormat.IsListItem));
        }
Esempio n. 7
0
        /// <summary>
        /// 书签替换,字典和word书签必须一致,如果字典包含word模板不存在的书签,这些不存在的字典将在word的最后一个书签进行叠加显示
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="asTemplates">字典</param>
        /// <param name="tableData">表格</param>
        /// <param name="replaceType">bookmark:书签替换,placeholder:占位符替换</param>
        public static void FillWordData(string wordFile, string pdfFile, List <ASTemplate> asTemplates, List <TempClass> tableData, ReplaceTypeEnum replaceType = ReplaceTypeEnum.bookmark)
        {
            Document        doc     = new Document(wordFile);
            DocumentBuilder builder = new DocumentBuilder(doc);   //操作word


            if (replaceType == ReplaceTypeEnum.bookmark)
            {
                //书签替换
                asTemplates.ForEach(a =>
                {
                    builder.MoveToBookmark(a.Key); //将光标移入书签的位置
                    builder.Write(a.Value);        //填充值
                });
            }
            else
            {
                //占位符替换
                asTemplates.ForEach(a =>
                {
                    //doc.Range.Replace(a.Key, a.Value, false, false);
                    doc.Range.Replace(a.Key, a.Value);
                });
            }

            //获取表格节点集合
            NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);

            //填充表格数据
            for (int i = 0; i < tables.Count(); i++)
            {
                AddTableRow(tables[i] as Table, tableData, doc, builder);
            }

            doc.Save(wordFile);//保存word
            //doc.Save(pdfFile, SaveFormat.Pdf);//保存pdf
        }