Example #1
0
        /// <summary>
        /// Insert/Add/Remove hyperlinks from paragraphs.
        /// </summary>
        public static void Hyperlinks()
        {
            Console.WriteLine("\tHyperlinks()");

            // Create a document
            using (DocX document = DocX.Create(HyperlinkSample.HyperlinkSampleOutputDirectory + @"Hyperlinks.docx"))
            {
                // Add a title
                document.InsertParagraph("Insert/Remove Hyperlinks").FontSize(15d).SpacingAfter(50d).Alignment = Alignment.center;

                // Add an Hyperlink into this document.
                var h = document.AddHyperlink("google", new Uri("http://www.google.com"));

                // Add a paragraph.
                var p = document.InsertParagraph("The  hyperlink has been inserted in this paragraph.");
                // insert an hyperlink at specific index in this paragraph.
                p.InsertHyperlink(h, 4);
                p.SpacingAfter(40d);

                // Get the first hyperlink in the document.
                var hyperlink = document.Hyperlinks.FirstOrDefault();
                if (hyperlink != null)
                {
                    // Modify its text and Uri.
                    hyperlink.Text = "xceed";
                    hyperlink.Uri  = new Uri("http://www.xceed.com/");
                }

                // Add an Hyperlink to this document.
                var h2 = document.AddHyperlink("xceed", new Uri("http://www.xceed.com/"));
                // Add a paragraph.
                var p2 = document.InsertParagraph("A formatted hyperlink has been added at the end of this paragraph: ");
                // Append an hyperlink to a paragraph.
                p2.AppendHyperlink(h2).Color(Color.Blue).UnderlineStyle(UnderlineStyle.singleLine);
                p2.Append(".").SpacingAfter(40d);

                // Add an Hyperlink to this document.
                var h3 = document.AddHyperlink("microsoft", new Uri("http://www.microsoft.com"));
                // Add a paragraph
                var p3 = document.InsertParagraph("The hyperlink from this paragraph has been removed. ");
                // Append an hyperlink to a paragraph.
                p3.AppendHyperlink(h3).Color(Color.Green).UnderlineStyle(UnderlineStyle.singleLine).Italic();

                // Remove the first hyperlink of paragraph 3.
                p3.RemoveHyperlink(0);

                document.Save();
                Console.WriteLine("\tCreated: Hyperlinks.docx\n");
            }
        }
        public void CopyFromFile_Test_CopiesCorrectHyperlinksToBookmarks()
        {
            _tmpFile = GetNewRandomFilePath(GetOrCreateTmpDirectory());
            using (DocX doc = DocX.Create(_tmpFile)) {
                Hyperlink hyperlink = doc.AddHyperlink("hypelink text", "bookmark");
                doc.InsertParagraph()
                .AppendBookmark("bookmark")
                .Append("bookmark text");
                doc.InsertParagraph()
                .AppendHyperlink(hyperlink);
                doc.Save();
            }

            Range targetRange = _application.Documents.Add().Range();

            targetRange.Text = "unexpected";

            new OpenXmlHelper().CopyFromFile(_tmpFile, targetRange);

            Bookmarks  bookmarks  = targetRange.Document.Bookmarks;
            Hyperlinks hyperlinks = targetRange.Document.Hyperlinks;

            Assert.NotZero(bookmarks.Count);
            Assert.NotZero(hyperlinks.Count);
            Assert.That(hyperlinks[1].SubAddress, Is.EqualTo(bookmarks[1].Name));
        }
        private static void ReplaceEmail(string tag, string email, DocX doc)
        {
            var link = doc.AddHyperlink(email, new Uri($"mailto:{email}"));
            var p    = doc.InsertParagraph("");

            p.AppendHyperlink(link).Color(Color.Blue).UnderlineStyle(UnderlineStyle.singleLine);
            doc.ReplaceTextWithObject(tag, p.Hyperlinks.FirstOrDefault());
            doc.RemoveParagraph(p);
        }
Example #4
0
        public void replaceHyperLink(DocX document)
        {
            var hyperlinks = document.Hyperlinks;
            var h3         = document.AddHyperlink("hyperlink", new Uri("http://www.xceed.com/"));
            // Add a paragraph.
            var p3 = document.InsertParagraph("An hyperlink pointing to a bookmark of this Document has been added at the end of this paragraph: ");

            p3.ReplaceTextWithObject("hyperlink", h3);
        }
Example #5
0
        /// <summary>
        /// Ajouter un lien Hypertexte dans un document.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="word"></param>
        /// <param name="linkword"></param>
        /// <param name="sentence"></param>
        public static void Hyperlinks(DocX document, string word, string linkword, string sentence)
        {
            // Ajout d'un lien dans le document.
            Hyperlink hyperlink = document.AddHyperlink(word, new Uri(linkword));
            // Ajout d'un paragraphe
            Paragraph paragraph = document.InsertParagraph();

            paragraph.Append(sentence);
            // Insérer un hyperlien à un index spécifique dans le présent paragraphe.
            paragraph.AppendHyperlink(hyperlink);
        }
        public static Paragraph TestReportSection(this DocX doc, string testReport)
        {
            var hyperlink = doc.AddHyperlink(testReport, new Uri(testReport));
            var paragraph = doc
                            .InsertParagraph()
                            .CreateHeadingSection("Test Report")
                            .InsertParagraphAfterSelf("")
                            .AppendHyperlink(hyperlink);

            return(paragraph);
        }
Example #7
0
        /// <summary>
        /// append some stuff: Hyperlink, Table, Image
        /// </summary>
        public static void CreateDocX_SomeStuff(FileInfo fi)
        {
            using (DocX document = DocX.Create(fi.FullName))
            {
                // Add a hyperlink into the document.
                Hyperlink link = document.AddHyperlink("link", new Uri("http://www.google.com"));

                // Add a Table into the document.
                Table table = document.AddTable(2, 2);
                table.Design    = TableDesign.ColorfulGridAccent2;
                table.Alignment = Alignment.center;
                table.Rows[0].Cells[0].Paragraphs[0].Append("1");
                table.Rows[0].Cells[1].Paragraphs[0].Append("2");
                table.Rows[1].Cells[0].Paragraphs[0].Append("3");
                table.Rows[1].Cells[1].Paragraphs[0].Append("4");

                // Add an image into the document.
                Image image = document.AddImage("Image.jpg");

                // Create a picture (A custom view of an Image).
                Picture picture = image.CreatePicture();
                picture.Rotation = 10;
                picture.SetPictureShape(BasicShapes.cube);

                // Insert a new Paragraph into the document.
                Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new Font("Comic Sans MS"));
                title.Alignment = Alignment.center;

                // Insert a new Paragraph into the document.
                Paragraph p1 = document.InsertParagraph();
                p1.AppendLine("This line contains a ").Append("bold").Bold().Append(" word.");
                p1.AppendLine("Here is a cool ").AppendHyperlink(link).Color(System.Drawing.Color.Blue).Append(".");
                p1.AppendLine();
                p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
                p1.AppendLine();
                p1.AppendLine("Can you check this Table of figures for me?");
                p1.AppendLine();

                // Insert the Table after Paragraph 1.
                p1.InsertTableAfterSelf(table);

                // Insert a new paragraph
                Paragraph p2 = document.InsertParagraph();
                p2.AppendLine("Is it correct?");

                // Save this document.
                document.Save();
            }
        }
Example #8
0
        /// <summary>
        /// Initialize Word Report --> Support Method
        /// </summary>
        public static void InsertSummaryResults()
        {
            try
            {
                Table table = resultSummarydocument.AddTable(1, 4);
                // Specify some properties for this Table.
                table.Alignment = Alignment.left;
                table.AutoFit   = AutoFit.Contents;
                table.Design    = TableDesign.TableGrid;
                table.SetColumnWidth(0, 667.87);
                table.SetColumnWidth(1, 5347.87);
                table.SetColumnWidth(2, 1255.87);
                table.SetColumnWidth(3, 1825.82);

                table.Rows[0].Cells[0].Paragraphs.First().Append(smrw.ToString() + ".");
                smrw += 1;
                table.Rows[0].Cells[1].Paragraphs.First().Append(BaseUtilities.scenarioName);
                switch (BaseUtilities.scenarioStatus.ToLower())
                {
                case "pass":

                    table.Rows[0].Cells[2].Paragraphs.First().Append("Pass").Color(System.Drawing.Color.FromArgb(52, 168, 83));
                    break;

                case "fail":

                    table.Rows[0].Cells[2].Paragraphs.First().Append("Fail").Color(System.Drawing.Color.FromArgb(234, 67, 53));
                    break;

                case "skip":

                    table.Rows[0].Cells[2].Paragraphs.First().Append("Skip").Color(System.Drawing.Color.FromArgb(234, 67, 53));
                    break;
                }


                Hyperlink link = resultSummarydocument.AddHyperlink("Click here", new Uri(reportpath));
                table.Rows[0].Cells[3].Paragraphs.First().AppendHyperlink(link).Color(System.Drawing.Color.FromArgb(1, 91, 168));

                resultSummarydocument.InsertTable(table);

                resultSummarydocument.Save();
            }
            catch (Exception e)
            {
                Reports.SetupErrorLog(e);
            }
        }
Example #9
0
        public void HyperLinkReplaceSet(DocX document, DataGridView ReplaceLinkGridView)
        {
            foreach (DataGridViewRow row in ReplaceLinkGridView.Rows)
            {
                string source = row.Cells[0].Value == null ? "" : row.Cells[0].Value.ToString();
                Uri    target = row.Cells[1].Value == null ? null : GetUri(row.Cells[1].Value.ToString().Trim());
                if (string.IsNullOrWhiteSpace(source) || target == null)
                {
                    continue;
                }
                if (document.FindUniqueByPattern(@source, RegexOptions.None).Count > 0)
                {
                    var linkBlock = document.AddHyperlink(@source, target);

                    document.ReplaceTextWithObject(@source, linkBlock);
                }
            }
        }
        public void CopyFromFile_Test_CopiesNoHyperlinksToBookmarks()
        {
            _tmpFile = GetNewRandomFilePath(GetOrCreateTmpDirectory());
            using (DocX doc = DocX.Create(_tmpFile)) {
                Hyperlink hyperlink = doc.AddHyperlink("hypelink text", "bookmark");
                doc.InsertParagraph()
                .AppendBookmark("bookmark")
                .Append("bookmark text");
                doc.InsertParagraph()
                .AppendHyperlink(hyperlink);
                doc.Save();
            }

            Range targetRange = _application.Documents.Add().Range();

            targetRange.Text = "unexpected";

            new OpenXmlHelper().CopyFromFileTextOnly(_tmpFile, targetRange);

            Assert.Zero(targetRange.Document.Bookmarks.Count);
            Assert.Zero(targetRange.Document.Hyperlinks.Count);
        }
Example #11
0
        /// <summary>
        /// Insert/Add/Remove hyperlinks from paragraphs.
        /// </summary>
        public static void Hyperlinks()
        {
            Console.WriteLine("\tHyperlinks()");

            // Create a document
            using (DocX document = DocX.Create(HyperlinkSample.HyperlinkSampleOutputDirectory + @"Hyperlinks.docx"))
            {
                // Add a title
                document.InsertParagraph("Insert/Remove Hyperlinks").FontSize(15d).SpacingAfter(50d).Alignment = Alignment.center;

                // Add an Hyperlink into this document.
                var h = document.AddHyperlink("google", new Uri("http://www.google.com"));

                // Add a paragraph.
                var p = document.InsertParagraph("The  hyperlink has been inserted in this paragraph.");
                // insert an hyperlink at specific index in this paragraph.
                p.InsertHyperlink(h, 4);
                p.SpacingAfter(40d);

                // Get the first hyperlink in the document.
                var hyperlink = document.Hyperlinks.FirstOrDefault();
                if (hyperlink != null)
                {
                    // Modify its text and Uri.
                    hyperlink.Text = "xceed";
                    hyperlink.Uri  = new Uri("http://www.xceed.com/");
                }

                // Add an Hyperlink to this document.
                var h2 = document.AddHyperlink("xceed", new Uri("http://www.xceed.com/"));
                // Add a paragraph.
                var p2 = document.InsertParagraph("A formatted hyperlink has been added at the end of this paragraph: ");
                // Append an hyperlink to a paragraph.
                p2.AppendHyperlink(h2).Color(Color.Blue).UnderlineStyle(UnderlineStyle.singleLine);
                p2.Append(".").SpacingAfter(40d);

                // Create a bookmark anchor.
                var bookmarkAnchor = "bookmarkAnchor";
                // Add an Hyperlink to this document pointing to a bookmark anchor.
                var h3 = document.AddHyperlink("Special Data", bookmarkAnchor);
                // Add a paragraph.
                var p3 = document.InsertParagraph("An hyperlink pointing to a bookmark of this Document has been added at the end of this paragraph: ");
                // Append an hyperlink to a paragraph.
                p3.AppendHyperlink(h3).Color(Color.Red).UnderlineStyle(UnderlineStyle.singleLine);
                p3.Append(".").SpacingAfter(40d);

                // Add an Hyperlink to this document.
                var h4 = document.AddHyperlink("microsoft", new Uri("http://www.microsoft.com"));
                // Add a paragraph
                var p4 = document.InsertParagraph("The hyperlink from this paragraph has been removed. ");
                // Append an hyperlink to a paragraph.
                p4.AppendHyperlink(h4).Color(Color.Green).UnderlineStyle(UnderlineStyle.singleLine).Italic();

                // Remove the first hyperlink of paragraph 4.
                p4.RemoveHyperlink(0);

                // Add a paragraph.
                var p5 = document.InsertParagraph("This is a paragraph containing a");
                // Add a bookmark into the paragraph by setting its bookmark anchor.
                p5.AppendBookmark(bookmarkAnchor);
                p5.Append(" bookmark ");
                p5.Append("referenced by a hyperlink defined in an earlier paragraph.");
                p5.SpacingBefore(200d);

                document.Save();
                Console.WriteLine("\tCreated: Hyperlinks.docx\n");
            }
        }
Example #12
0
        /// <summary>
        /// Creates a document with a Hyperlink, an Image and a Table.
        /// </summary>
        private static void HyperlinksImagesTables()
        {
            Console.WriteLine("\tHyperlinksImagesTables()");

            // Create a document.
            using (DocX document = DocX.Create(@"docs\HyperlinksImagesTables.docx"))
            {
                // Add a hyperlink into the document.
                Hyperlink link = document.AddHyperlink("link", new Uri("http://www.google.com"));

                // Add a Table into the document.
                Table table = document.AddTable(2, 2);
                table.Design    = TableDesign.ColorfulGridAccent2;
                table.Alignment = Alignment.center;
                table.Rows[0].Cells[0].Paragraphs[0].Append("1");
                table.Rows[0].Cells[1].Paragraphs[0].Append("2");
                table.Rows[1].Cells[0].Paragraphs[0].Append("3");
                table.Rows[1].Cells[1].Paragraphs[0].Append("4");

                Row newRow = table.InsertRow(table.Rows[1]);
                newRow.ReplaceText("4", "5");

                // Add an image into the document.
                Novacode.Image image = document.AddImage(@"images\logo_template.png");

                // Create a picture (A custom view of an Image).
                Picture picture = image.CreatePicture();
                picture.Rotation = 10;
                picture.SetPictureShape(BasicShapes.cube);

                // Insert a new Paragraph into the document.
                Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS"));
                title.Alignment = Alignment.center;

                // Insert a new Paragraph into the document.
                Paragraph p1 = document.InsertParagraph();

                // Append content to the Paragraph
                p1.AppendLine("This line contains a ").Append("bold").Bold().Append(" word.");
                p1.AppendLine("Here is a cool ").AppendHyperlink(link).Append(".");
                p1.AppendLine();
                p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
                p1.AppendLine();
                p1.AppendLine("Can you check this Table of figures for me?");
                p1.AppendLine();

                // Insert the Table after Paragraph 1.
                p1.InsertTableAfterSelf(table);

                // Insert a new Paragraph into the document.
                Paragraph p2 = document.InsertParagraph();

                // Append content to the Paragraph.
                p2.AppendLine("Is it correct?");

                // Save this document.
                document.Save();

                Console.WriteLine("\tCreated: docs\\HyperlinksImagesTables.docx\n");
            }
        }
Example #13
0
        /// <summary>
        /// Create a file and add a picture, a table, an hyperlink, paragraphs, a bulleted list and a numbered list.
        /// </summary>
        public static void CreateRecipe()
        {
            Console.WriteLine("\tCreateRecipe()");

            // Create a new document.
            using (DocX document = DocX.Create(MiscellaneousSample.MiscellaneousSampleOutputDirectory + @"CreateRecipe.docx"))
            {
                // Create a rotated picture from existing image.
                var image   = document.AddImage(MiscellaneousSample.MiscellaneousSampleResourcesDirectory + @"cupcake.png");
                var picture = image.CreatePicture();
                picture.Rotation = 20;

                // Create an hyperlink.
                var hyperlink = document.AddHyperlink("Food.com", new Uri("http://www.food.com/recipe/simple-vanilla-cupcakes-178370"));

                // Create a bulleted list for the ingredients.
                var bulletsList = document.AddList("2 cups of flour", 0, ListItemType.Bulleted);
                document.AddListItem(bulletsList, "3⁄4 cup of sugar");
                document.AddListItem(bulletsList, "1⁄2 cup of butter");
                document.AddListItem(bulletsList, "2 eggs");
                document.AddListItem(bulletsList, "1 cup of milk");
                document.AddListItem(bulletsList, "2 teaspoons of baking powder");
                document.AddListItem(bulletsList, "1⁄2 teaspoon of salt");
                document.AddListItem(bulletsList, "1 teaspoon of vanilla essence");

                // Create a table for text and the picture.
                var table = document.AddTable(1, 2);
                table.Design  = TableDesign.LightListAccent3;
                table.AutoFit = AutoFit.Window;
                table.Rows[0].Cells[0].Paragraphs[0].AppendLine().AppendLine().Append("Simple Vanilla Cupcakes Recipe").FontSize(20).Font(new Font("Comic Sans MS"));
                table.Rows[0].Cells[1].Paragraphs[0].AppendPicture(picture);

                // Create a numbered list for the directions.
                var recipeList = document.AddList("Preheat oven to 375F and fill muffin cups with papers.", 0, ListItemType.Numbered, 1);
                document.AddListItem(recipeList, "Mix butter and sugar until light and fluffy.");
                document.AddListItem(recipeList, "Beat in the eggs, one at a time.", 1);
                document.AddListItem(recipeList, "Add the flour, baking powder and salt, alternate with milk and beat well.");
                document.AddListItem(recipeList, "Add in vanilla.", 1);
                document.AddListItem(recipeList, "Divide in the pans and bake for 18 minutes.");
                document.AddListItem(recipeList, "Let cool 5 minutes an eat.", 1);

                // Insert the data in page.
                document.InsertTable(table);
                var paragraph = document.InsertParagraph();
                paragraph.AppendLine();
                paragraph.AppendLine();
                paragraph.AppendLine("Ingredients").FontSize(15).Bold().Color(Color.BlueViolet);
                document.InsertList(bulletsList);
                var paragraph2 = document.InsertParagraph();
                paragraph2.AppendLine();
                paragraph2.AppendLine("Directions").FontSize(15).Bold().Color(Color.BlueViolet);
                document.InsertList(recipeList);
                var paragraph3 = document.InsertParagraph();
                paragraph3.AppendLine();
                paragraph3.AppendLine("Reference: ").AppendHyperlink(hyperlink).Color(Color.Blue).UnderlineColor(Color.Blue).Append(".");

                // Save this document to disk.
                document.Save();
                Console.WriteLine("\tCreated: CreateRecipe.docx\n");
            }
        }