From d91f9ac4484cd87d6a0b0aefe6c9547f92e12f2a Mon Sep 17 00:00:00 2001 From: vderyushev Date: Wed, 17 Jul 2024 13:08:16 +0300 Subject: [PATCH] Updated documentation examples according to tutorials --- .../Base conversions.cs | 23 ++++----- .../Working with HtmlSaveOptions.cs | 41 ++++++++------- .../Working with PdfSaveOptions.cs | 7 +-- .../Base operations.cs | 8 ++- .../Working with Bookmarks.cs | 51 ++++--------------- .../Add content using DocumentBuilder.cs | 12 ++--- .../Working with Fields.cs | 3 +- .../Working with Fonts.cs | 13 +++-- .../Working with Headers and Footers.cs | 8 +-- .../Working with Markdown.cs | 11 ++-- .../Working with Tables.cs | 11 ++-- 11 files changed, 76 insertions(+), 112 deletions(-) diff --git a/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Base conversions.cs b/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Base conversions.cs index a533e3993..d2ad45d0a 100644 --- a/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Base conversions.cs +++ b/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Base conversions.cs @@ -35,24 +35,23 @@ public void DocxToRtf() //ExStart:OpenFromStream //GistId:1d626c7186a318d22d022dc96dd91d55 // Read only access is enough for Aspose.Words to load a document. - Stream stream = File.OpenRead(MyDir + "Document.docx"); - - Document doc = new Document(stream); - // You can close the stream now, it is no longer needed because the document is in memory. - stream.Close(); + Document doc; + using (Stream stream = File.OpenRead(MyDir + "Document.docx")) + doc = new Document(stream); //ExEnd:OpenFromStream // ... do something with the document. // Convert the document to a different format and save to stream. - MemoryStream dstStream = new MemoryStream(); - doc.Save(dstStream, SaveFormat.Rtf); - - // Rewind the stream position back to zero so it is ready for the next reader. - dstStream.Position = 0; + using (MemoryStream dstStream = new MemoryStream()) + { + doc.Save(dstStream, SaveFormat.Rtf); + // Rewind the stream position back to zero so it is ready for the next reader. + dstStream.Position = 0; + + File.WriteAllBytes(ArtifactsDir + "BaseConversions.DocxToRtf.rtf", dstStream.ToArray()); + } //ExEnd:LoadAndSaveToStream - - File.WriteAllBytes(ArtifactsDir + "BaseConversions.DocxToRtf.rtf", dstStream.ToArray()); } [Test] diff --git a/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Save Options/Working with HtmlSaveOptions.cs b/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Save Options/Working with HtmlSaveOptions.cs index ca547abdb..d5f24175a 100644 --- a/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Save Options/Working with HtmlSaveOptions.cs +++ b/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Save Options/Working with HtmlSaveOptions.cs @@ -1,5 +1,7 @@ using System.IO; +using System.Text; using Aspose.Words; +using Aspose.Words.Loading; using Aspose.Words.Saving; using NUnit.Framework; @@ -53,26 +55,27 @@ public void ExportResources() } [Test] - public void ConvertMetafilesToEmfOrWmf() + public void ConvertMetafilesToPng() { - //ExStart:ConvertMetafilesToEmfOrWmf - Document doc = new Document(); - DocumentBuilder builder = new DocumentBuilder(doc); - - builder.Write("Here is an image as is: "); - builder.InsertHtml( - @""); - - HtmlSaveOptions saveOptions = new HtmlSaveOptions { MetafileFormat = HtmlMetafileFormat.EmfOrWmf }; - - doc.Save(ArtifactsDir + "WorkingWithHtmlSaveOptions.ConvertMetafilesToEmfOrWmf.html", saveOptions); - //ExEnd:ConvertMetafilesToEmfOrWmf + //ExStart:ConvertMetafilesToPng + string html = + @" + + Hello world! + + "; + + // Use 'ConvertSvgToEmf' to turn back the legacy behavior + // where all SVG images loaded from an HTML document were converted to EMF. + // Now SVG images are loaded without conversion + // if the MS Word version specified in load options supports SVG images natively. + HtmlLoadOptions loadOptions = new HtmlLoadOptions { ConvertSvgToEmf = true }; + Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), loadOptions); + + HtmlSaveOptions saveOptions = new HtmlSaveOptions { MetafileFormat = HtmlMetafileFormat.Png }; + + doc.Save(ArtifactsDir + "WorkingWithHtmlSaveOptions.ConvertMetafilesToPng.html", saveOptions); + //ExEnd:ConvertMetafilesToPng } [Test] diff --git a/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Save Options/Working with PdfSaveOptions.cs b/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Save Options/Working with PdfSaveOptions.cs index 1ad9fb2d5..518e5bf42 100644 --- a/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Save Options/Working with PdfSaveOptions.cs +++ b/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Save Options/Working with PdfSaveOptions.cs @@ -169,11 +169,8 @@ public void EscapeUri() Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); - builder.InsertHyperlink("Testlink", - "https://www.google.com/search?q=%2Fthe%20test", false); - builder.Writeln(); - builder.InsertHyperlink("https://www.google.com/search?q=%2Fthe%20test", - "https://www.google.com/search?q=%2Fthe%20test", false); + builder.InsertHyperlink("Testlink", + "https://www.google.com/search?q= aspose", false); doc.Save(ArtifactsDir + "WorkingWithPdfSaveOptions.EscapeUri.pdf"); //ExEnd:EscapeUri diff --git a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Base operations.cs b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Base operations.cs index 03f87157a..a099ab2da 100644 --- a/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Base operations.cs +++ b/Examples/DocsExamples/DocsExamples/Mail Merge and Reporting/Base operations.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using System.Data; using System.Data.OleDb; +using System.Linq; +using System.Xml.Linq; using Aspose.Words; using Aspose.Words.MailMerging; using NUnit.Framework; @@ -27,8 +29,8 @@ public void SimpleMailMerge() // Fill the fields in the document with user data. doc.MailMerge.Execute(new string[] { "CustomerName", "Item", "Quantity" }, - new object[] { "John Doe", "Hawaiian", "2" }); - + new object[] { "John Doe", "Hawaiian", "2" }); + doc.Save(ArtifactsDir + "BaseOperations.SimpleMailMerge.docx"); //ExEnd:ExecuteSimpleMailMerge } @@ -39,6 +41,8 @@ public void UseIfElseMustache() //ExStart:UseIfElseMustache //GistId:544788f602e697802e313a641cedb9b8 Document doc = new Document(MyDir + "Mail merge destinations - Mustache syntax.docx"); + doc.Sections.Clear(); + doc.Save(ArtifactsDir + "output.docx"); doc.MailMerge.UseNonMergeFields = true; doc.MailMerge.Execute(new[] { "GENDER" }, new object[] { "MALE" }); diff --git a/Examples/DocsExamples/DocsExamples/Programming with Documents/Contents Management/Working with Bookmarks.cs b/Examples/DocsExamples/DocsExamples/Programming with Documents/Contents Management/Working with Bookmarks.cs index 61d118bb6..52ce2c456 100644 --- a/Examples/DocsExamples/DocsExamples/Programming with Documents/Contents Management/Working with Bookmarks.cs +++ b/Examples/DocsExamples/DocsExamples/Programming with Documents/Contents Management/Working with Bookmarks.cs @@ -165,7 +165,7 @@ public void CreateBookmark() options.OutlineOptions.BookmarksOutlineLevels.Add("My Bookmark", 1); options.OutlineOptions.BookmarksOutlineLevels.Add("Nested Bookmark", 2); - doc.Save(ArtifactsDir + "WorkingWithBookmarks.CreateBookmark.pdf", options); + doc.Save(ArtifactsDir + "WorkingWithBookmarks.CreateBookmark.docx", options); //ExEnd:CreateBookmark } @@ -175,58 +175,27 @@ public void ShowHideBookmarks() //ExStart:ShowHideBookmarks Document doc = new Document(MyDir + "Bookmarks.docx"); - ShowHideBookmarkedContent(doc, "MyBookmark1", false); + ShowHideBookmarkedContent(doc, "MyBookmark1", true); doc.Save(ArtifactsDir + "WorkingWithBookmarks.ShowHideBookmarks.docx"); //ExEnd:ShowHideBookmarks } //ExStart:ShowHideBookmarkedContent - public void ShowHideBookmarkedContent(Document doc, string bookmarkName, bool showHide) + public void ShowHideBookmarkedContent(Document doc, string bookmarkName, bool isHidden) { Bookmark bm = doc.Range.Bookmarks[bookmarkName]; - DocumentBuilder builder = new DocumentBuilder(doc); - builder.MoveToDocumentEnd(); - - // {IF "{MERGEFIELD bookmark}" = "true" "" ""} - Field field = builder.InsertField("IF \"", null); - builder.MoveTo(field.Start.NextSibling); - builder.InsertField("MERGEFIELD " + bookmarkName + "", null); - builder.Write("\" = \"true\" "); - builder.Write("\""); - builder.Write("\""); - builder.Write(" \"\""); - - Node currentNode = field.Start; - bool flag = true; - while (currentNode != null && flag) + Node currentNode = bm.BookmarkStart; + while (currentNode != null && currentNode.NodeType != NodeType.BookmarkEnd) { if (currentNode.NodeType == NodeType.Run) - if (currentNode.ToString(SaveFormat.Text).Trim() == "\"") - flag = false; - - Node nextNode = currentNode.NextSibling; - - bm.BookmarkStart.ParentNode.InsertBefore(currentNode, bm.BookmarkStart); - currentNode = nextNode; - } - - Node endNode = bm.BookmarkEnd; - flag = true; - while (currentNode != null && flag) - { - if (currentNode.NodeType == NodeType.FieldEnd) - flag = false; - - Node nextNode = currentNode.NextSibling; - - bm.BookmarkEnd.ParentNode.InsertAfter(currentNode, endNode); - endNode = currentNode; - currentNode = nextNode; + { + Run run = currentNode as Run; + run.Font.Hidden = isHidden; + } + currentNode = currentNode.NextSibling; } - - doc.MailMerge.Execute(new[] { bookmarkName }, new object[] { showHide }); } //ExEnd:ShowHideBookmarkedContent diff --git a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Document/Add content using DocumentBuilder.cs b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Document/Add content using DocumentBuilder.cs index a37d18e9d..3b60cbb09 100644 --- a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Document/Add content using DocumentBuilder.cs +++ b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Document/Add content using DocumentBuilder.cs @@ -52,7 +52,6 @@ public void BuildTable() Table table = builder.StartTable(); builder.InsertCell(); - table.AutoFit(AutoFitBehavior.FixedColumnWidths); builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; builder.Write("This is row 1 cell 1"); @@ -76,6 +75,8 @@ public void BuildTable() builder.EndRow(); builder.EndTable(); + table.AutoFit(AutoFitBehavior.FixedColumnWidths); + doc.Save(ArtifactsDir + "AddContentUsingDocumentBuilder.BuildTable.docx"); //ExEnd:BuildTable } @@ -196,14 +197,13 @@ public void InsertHyperlink() //ExStart:InsertHyperlink Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); - + builder.Write("Please make sure to visit "); - builder.Font.Color = Color.Blue; - builder.Font.Underline = Underline.Single; - + + builder.Font.Style = doc.Styles[StyleIdentifier.Hyperlink]; builder.InsertHyperlink("Aspose Website", "http://www.aspose.com", false); - builder.Font.ClearFormatting(); + builder.Write(" for more information."); doc.Save(ArtifactsDir + "AddContentUsingDocumentBuilder.InsertHyperlink.docx"); diff --git a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fields.cs b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fields.cs index b8d308e67..7f3e3f4a2 100644 --- a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fields.cs +++ b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fields.cs @@ -192,8 +192,7 @@ public void InsertMergeFieldUsingDOM() Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); - Paragraph para = (Paragraph) doc.GetChildNodes(NodeType.Paragraph, true)[0]; - + Paragraph para = (Paragraph) doc.GetChild(NodeType.Paragraph, 0, true); builder.MoveTo(para); // We want to insert a merge field like this: diff --git a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fonts.cs b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fonts.cs index 9de63daff..4fc5b056e 100644 --- a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fonts.cs +++ b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Fonts.cs @@ -189,8 +189,8 @@ public void MultipleFolders() public void SetFontsFoldersSystemAndCustomFolder() { //ExStart:SetFontsFoldersSystemAndCustomFolder - Document doc = new Document(MyDir + "Rendering.docx"); - + Document doc = new Document(MyDir + "Rendering.docx"); + FontSettings fontSettings = new FontSettings(); // Retrieve the array of environment-dependent font sources that are searched by default. // For example this will contain a "Windows\Fonts\" source on a Windows machines. @@ -199,15 +199,14 @@ public void SetFontsFoldersSystemAndCustomFolder() // Add a new folder source which will instruct Aspose.Words to search the following folder for fonts. FolderFontSource folderFontSource = new FolderFontSource("C:\\MyFonts\\", true); - // Add the custom folder which contains our fonts to the list of existing font sources. fontSources.Add(folderFontSource); FontSourceBase[] updatedFontSources = fontSources.ToArray(); - fontSettings.SetFontsSources(updatedFontSources); - - doc.FontSettings = fontSettings; - + fontSettings.SetFontsSources(updatedFontSources); + + doc.FontSettings = fontSettings; + doc.Save(ArtifactsDir + "WorkingWithFonts.SetFontsFoldersSystemAndCustomFolder.pdf"); //ExEnd:SetFontsFoldersSystemAndCustomFolder } diff --git a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Headers and Footers.cs b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Headers and Footers.cs index 7f007d731..f25e6aaa2 100644 --- a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Headers and Footers.cs +++ b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Headers and Footers.cs @@ -155,7 +155,7 @@ public void LinkToPreviousHeaderFooter() builder.Font.Size = 14; builder.Write("Header for the first page."); - builder.MoveToDocumentEnd(); + builder.MoveToDocumentEnd(); builder.InsertBreak(BreakType.SectionBreakNewPage); Section currentSection = builder.CurrentSection; @@ -173,7 +173,7 @@ public void LinkToPreviousHeaderFooter() builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary); builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; - builder.Font.Name = "Arial"; + builder.Font.Name = "Arial"; builder.Font.Size = 12; builder.Write("New Header for the first page."); @@ -184,8 +184,8 @@ public void LinkToPreviousHeaderFooter() [Test] public void SectionsWithDifferentHeaders() { - //ExStart:SectionsWithDifferentHeaders - //GistId:1afca4d3da7cb4240fb91c3d93d8c30d + //ExStart:SectionsWithDifferentHeaders + //GistId:1afca4d3da7cb4240fb91c3d93d8c30d Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); diff --git a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Markdown.cs b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Markdown.cs index e197651a7..8d923f62f 100644 --- a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Markdown.cs +++ b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Markdown.cs @@ -99,11 +99,8 @@ public void Image() DocumentBuilder builder = new DocumentBuilder(); // Insert image. - Shape shape = new Shape(builder.Document, ShapeType.Image); - shape.WrapType = WrapType.Inline; - shape.ImageData.SourceFullName = "/attachment/1456/pic001.png"; + Shape shape = builder.InsertImage(ImagesDir + "Logo.jpg"); shape.ImageData.Title = "title"; - builder.InsertNode(shape); //ExEnd:Image } @@ -249,9 +246,7 @@ public void OrderedList() Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); - builder.ListFormat.ApplyBulletDefault(); - builder.ListFormat.List.ListLevels[0].NumberFormat = $"{(char) 0}."; - builder.ListFormat.List.ListLevels[1].NumberFormat = $"{(char) 1}."; + builder.ListFormat.ApplyNumberDefault(); builder.Writeln("Item 1"); builder.Writeln("Item 2"); @@ -276,6 +271,8 @@ public void Table() builder.InsertCell(); builder.Writeln("b"); + builder.EndRow(); + // Add the second row. builder.InsertCell(); builder.Writeln("c"); diff --git a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Tables/Working with Tables.cs b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Tables/Working with Tables.cs index 366f47e1d..323b9c1f8 100644 --- a/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Tables/Working with Tables.cs +++ b/Examples/DocsExamples/DocsExamples/Programming with Documents/Working with Tables/Working with Tables.cs @@ -391,9 +391,6 @@ public void InsertTableDirectly() row.RowFormat.AllowBreakAcrossPages = true; table.AppendChild(row); - // We can now apply any auto fit settings. - table.AutoFit(AutoFitBehavior.FixedColumnWidths); - Cell cell = new Cell(doc); cell.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue; cell.CellFormat.Width = 80; @@ -407,7 +404,10 @@ public void InsertTableDirectly() row.AppendChild(cell.Clone(false)); row.LastCell.AppendChild(new Paragraph(doc)); row.LastCell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 2 Text")); - + + // We can now apply any auto fit settings. + table.AutoFit(AutoFitBehavior.FixedColumnWidths); + doc.Save(ArtifactsDir + "WorkingWithTables.InsertTableDirectly.docx"); //ExEnd:InsertTableDirectly } @@ -623,7 +623,6 @@ public void SplitTable() firstTable.ParentNode.InsertAfter(new Paragraph(doc), firstTable); Row currentRow; - do { currentRow = firstTable.LastRow; @@ -1048,8 +1047,6 @@ public void PreferredWidthSettings() DocumentBuilder builder = new DocumentBuilder(doc); // Insert a table row made up of three cells which have different preferred widths. - builder.StartTable(); - // Insert an absolute sized cell. builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(40);