From 59fb086ff1e0fd547ff00e17386b7d2eb7be5e61 Mon Sep 17 00:00:00 2001 From: Michael Bowen Date: Mon, 13 May 2024 16:40:56 -0700 Subject: [PATCH] closes #209 --- ...ackage-part-to-a-document-part-in-a-dif.md | 2 +- ...heme-part-in-a-word-processing-document.md | 2 +- ...rch-and-replace-text-in-a-document-part.md | 2 +- ...add-tables-to-word-processing-documents.md | 2 +- ...ientation-of-a-word-processing-document.md | 250 ++++-------------- .../cs/Program.cs | 61 +++-- .../vb/Program.vb | 53 +++- 7 files changed, 131 insertions(+), 241 deletions(-) diff --git a/docs/general/how-to-copy-the-contents-of-an-open-xml-package-part-to-a-document-part-in-a-dif.md b/docs/general/how-to-copy-the-contents-of-an-open-xml-package-part-to-a-document-part-in-a-dif.md index dbfbf88a..11d86267 100644 --- a/docs/general/how-to-copy-the-contents-of-an-open-xml-package-part-to-a-document-part-in-a-dif.md +++ b/docs/general/how-to-copy-the-contents-of-an-open-xml-package-part-to-a-document-part-in-a-dif.md @@ -33,7 +33,7 @@ programmatically. To open an existing document, instantiate the class as shown in the following two **using** statements. In the same statement, you open the word processing file with the specified -file name by using the [Open](/dotnet/api/documentformat.openxml.packaging.wordprocessingdocument.open) method, with the Boolean parameter. +file name by using the method, with the Boolean parameter. For the source file that set the parameter to **false** to open it for read-only access. For the target file, set the parameter to **true** in order to enable editing the document. diff --git a/docs/general/how-to-replace-the-theme-part-in-a-word-processing-document.md b/docs/general/how-to-replace-the-theme-part-in-a-word-processing-document.md index 5b499b48..41d38e3b 100644 --- a/docs/general/how-to-replace-the-theme-part-in-a-word-processing-document.md +++ b/docs/general/how-to-replace-the-theme-part-in-a-word-processing-document.md @@ -29,7 +29,7 @@ In the sample code, you start by opening the word processing file by instantiating the class as shown in the following **using** statement. In the same statement, you open the word processing file *document* by using the -[Open](/dotnet/api/documentformat.openxml.packaging.wordprocessingdocument.open) method, with the Boolean parameter set + method, with the Boolean parameter set to **true** to enable editing the document. ### [C#](#tab/cs-0) diff --git a/docs/general/how-to-search-and-replace-text-in-a-document-part.md b/docs/general/how-to-search-and-replace-text-in-a-document-part.md index 9e57abad..d970c5fc 100644 --- a/docs/general/how-to-search-and-replace-text-in-a-document-part.md +++ b/docs/general/how-to-search-and-replace-text-in-a-document-part.md @@ -32,7 +32,7 @@ In the sample code, you start by opening the word processing file by instantiating the **** class as shown in the following **using** statement. In the same statement, you open the word processing file *document* by using the -**[Open](/dotnet/api/documentformat.openxml.packaging.wordprocessingdocument.open)** method, with the Boolean parameter set +**** method, with the Boolean parameter set to **true** to enable editing the document. ### [C#](#tab/cs-0) diff --git a/docs/word/how-to-add-tables-to-word-processing-documents.md b/docs/word/how-to-add-tables-to-word-processing-documents.md index e242dcc2..42342073 100644 --- a/docs/word/how-to-add-tables-to-word-processing-documents.md +++ b/docs/word/how-to-add-tables-to-word-processing-documents.md @@ -165,7 +165,7 @@ Before you can insert a table into a document, you must create the [Table](/dotn *** -The constructor for the **TableProperties** class allows you to specify as many child elements as you like (much like the [XElement](/dotnet/api/system.xml.linq.xelement) constructor). In this case, the code creates [TopBorder](/dotnet/api/documentformat.openxml.wordprocessing.topborder), [BottomBorder](/dotnet/api/documentformat.openxml.wordprocessing.bottomborder), [LeftBorder](/dotnet/api/documentformat.openxml.wordprocessing.leftborder), [RightBorder](/dotnet/api/documentformat.openxml.wordprocessing.rightborder), [InsideHorizontalBorder](/dotnet/api/documentformat.openxml.wordprocessing.insidehorizontalborder), and [InsideVerticalBorder](/dotnet/api/documentformat.openxml.wordprocessing.insideverticalborder) child elements, each describing one of the border elements for the table. For each element, the code sets the **Val** and **Size** properties as part of calling the constructor. Setting the size is simple, but setting the **Val** property requires a bit more effort: this property, for this particular object, represents the border style, and you must set it to an enumerated value. To do that, create an instance of the [EnumValue\](/dotnet/api/documentformat.openxml.enumvalue-1) generic type, passing the specific border type ([Single](/dotnet/api/documentformat.openxml.wordprocessing.bordervalues) as a parameter to the constructor. Once the code has set all the table border value it needs to set, it calls the [AppendChild\](/dotnet/api/documentformat.openxml.openxmlelement.appendchild) method of the table, indicating that the generic type is [TableProperties](/dotnet/api/ ocumentformat.openxml.wordprocessing.tableproperties)—that is, it is appending an instance of the **TableProperties** class, using the variable **props** as the value. +The constructor for the **TableProperties** class allows you to specify as many child elements as you like (much like the [XElement](/dotnet/api/system.xml.linq.xelement) constructor). In this case, the code creates [TopBorder](/dotnet/api/documentformat.openxml.wordprocessing.topborder), [BottomBorder](/dotnet/api/documentformat.openxml.wordprocessing.bottomborder), [LeftBorder](/dotnet/api/documentformat.openxml.wordprocessing.leftborder), [RightBorder](/dotnet/api/documentformat.openxml.wordprocessing.rightborder), [InsideHorizontalBorder](/dotnet/api/documentformat.openxml.wordprocessing.insidehorizontalborder), and [InsideVerticalBorder](/dotnet/api/documentformat.openxml.wordprocessing.insideverticalborder) child elements, each describing one of the border elements for the table. For each element, the code sets the **Val** and **Size** properties as part of calling the constructor. Setting the size is simple, but setting the **Val** property requires a bit more effort: this property, for this particular object, represents the border style, and you must set it to an enumerated value. To do that, create an instance of the generic type, passing the specific border type ([Single](/dotnet/api/documentformat.openxml.wordprocessing.bordervalues) as a parameter to the constructor. Once the code has set all the table border value it needs to set, it calls the [AppendChild\](/dotnet/api/documentformat.openxml.openxmlelement.appendchild) method of the table, indicating that the generic type is [TableProperties](/dotnet/api/ ocumentformat.openxml.wordprocessing.tableproperties)—that is, it is appending an instance of the **TableProperties** class, using the variable **props** as the value. ## Fill the table with data diff --git a/docs/word/how-to-change-the-print-orientation-of-a-word-processing-document.md b/docs/word/how-to-change-the-print-orientation-of-a-word-processing-document.md index 75bf8f58..0c614792 100644 --- a/docs/word/how-to-change-the-print-orientation-of-a-word-processing-document.md +++ b/docs/word/how-to-change-the-print-orientation-of-a-word-processing-document.md @@ -12,7 +12,7 @@ ms.suite: office ms.author: o365devx author: o365devx ms.topic: conceptual -ms.date: 06/28/2021 +ms.date: 05/13/2024 ms.localizationpriority: medium --- @@ -20,7 +20,7 @@ ms.localizationpriority: medium This topic shows how to use the classes in the Open XML SDK for Office to programmatically set the print orientation of a Microsoft Word document. It contains an example -**SetPrintOrientation** method to illustrate this task. +`SetPrintOrientation` method to illustrate this task. @@ -28,25 +28,17 @@ Office to programmatically set the print orientation of a Microsoft Word documen ## SetPrintOrientation Method -You can use the **SetPrintOrientation** method +You can use the `SetPrintOrientation` method to change the print orientation of a word processing document. The method accepts two parameters that indicate the name of the document to -modify (string) and the new print orientation ([PageOrientationValues](/dotnet/api/documentformat.openxml.wordprocessing.pageorientationvalues)). +modify (string) and the new print orientation (). -The following code shows the **SetPrintOrientation** method. +The following code shows the `SetPrintOrientation` method. ### [C#](#tab/cs-0) -```csharp - public static void SetPrintOrientation( - string fileName, PageOrientationValues newOrientation) -``` - +[!code-csharp[](../../samples/word/change_the_print_orientation/cs/Program.cs#snippet1)] ### [Visual Basic](#tab/vb-0) -```vb - Public Sub SetPrintOrientation( - ByVal fileName As String, - ByVal newOrientation As PageOrientationValues) -``` +[!code-vb[](../../samples/word/change_the_print_orientation/vb/Program.vb#snippet1)] *** @@ -59,21 +51,14 @@ the width, height, and margins for each section. ## Calling the Sample SetPrintOrientation Method -To call the sample **SetPrintOrientation** -method, pass a string that contains the name of the file to convert. The -following code shows an example method call. +To call the sample `SetPrintOrientation` +method, pass a string that contains the name of the file to convert and the string "landscape" or "portrait" +depending on which orientation you want. The following code shows an example method call. ### [C#](#tab/cs-1) -```csharp - SetPrintOrientation(@"C:\Users\Public\Documents\ChangePrintOrientation.docx", - PageOrientationValues.Landscape); -``` - +[!code-csharp[](../../samples/word/change_the_print_orientation/cs/Program.cs#snippet2)] ### [Visual Basic](#tab/vb-1) -```vb - SetPrintOrientation("C:\Users\Public\Documents\ChangePrintOrientation.docx", - PageOrientationValues.Landscape) -``` +[!code-vb[](../../samples/word/change_the_print_orientation/vb/Program.vb#snippet2)] *** @@ -81,40 +66,22 @@ following code shows an example method call. ## How the Code Works -The following code first opens the document by using the [Open](/dotnet/api/documentformat.openxml.packaging.wordprocessingdocument.open) method and sets the **isEditable** parameter to -**true** to indicate that the document should +The following code first determines which orientation to apply and +then opens the document by using the +method and sets the `isEditable` parameter to +`true` to indicate that the document should be read/write. The code maintains a Boolean variable that tracks whether the document has changed (so that it can save the document later, if the document has changed). The code retrieves a reference to the main document part, and then uses that reference to retrieve a collection of -all of the descendants of type [SectionProperties](/dotnet/api/documentformat.openxml.wordprocessing.sectionproperties) within the content of the +all of the descendants of type within the content of the document. Later code will use this collection to set the orientation for each section in turn. ### [C#](#tab/cs-2) -```csharp - using (var document = - WordprocessingDocument.Open(fileName, true)) - { - bool documentChanged = false; - - var docPart = document.MainDocumentPart; - var sections = docPart.Document.Descendants(); - // Code removed here... - } -``` - +[!code-csharp[](../../samples/word/change_the_print_orientation/cs/Program.cs#snippet3)] ### [Visual Basic](#tab/vb-2) -```vb - Using document = - WordprocessingDocument.Open(fileName, True) - Dim documentChanged As Boolean = False - - Dim docPart = document.MainDocumentPart - Dim sections = docPart.Document.Descendants(Of SectionProperties)() - ' Code removed here... - End Using -``` +[!code-vb[](../../samples/word/change_the_print_orientation/vb/Program.vb#snippet3)] *** @@ -122,35 +89,12 @@ each section in turn. ## Iterating Through All the Sections -The next block of code iterates through all the sections in the collection of **SectionProperties** elements. For each section, the code initializes a variable that tracks whether the page orientation for the section was changed so the code can update the page size and margins. (If the new orientation matches the original orientation, the code will not update the page.) The code continues by retrieving a reference to the first [PageSize](/dotnet/api/documentformat.openxml.wordprocessing.pagesize) descendant of the **SectionProperties** element. If the reference is not null, the code updates the orientation as required. +The next block of code iterates through all the sections in the collection of `SectionProperties` elements. For each section, the code initializes a variable that tracks whether the page orientation for the section was changed so the code can update the page size and margins. (If the new orientation matches the original orientation, the code will not update the page.) The code continues by retrieving a reference to the first descendant of the `SectionProperties` element. If the reference is not null, the code updates the orientation as required. ### [C#](#tab/cs-3) -```csharp - foreach (SectionProperties sectPr in sections) - { - bool pageOrientationChanged = false; - - PageSize pgSz = sectPr.Descendants().FirstOrDefault(); - if (pgSz != null) - { - // Code removed here... - } - } -``` - +[!code-csharp[](../../samples/word/change_the_print_orientation/cs/Program.cs#snippet4)] ### [Visual Basic](#tab/vb-3) -```vb - For Each sectPr As SectionProperties In sections - - Dim pageOrientationChanged As Boolean = False - - Dim pgSz As PageSize = - sectPr.Descendants(Of PageSize).FirstOrDefault - If pgSz IsNot Nothing Then - ' Code removed here... - End If - Next -``` +[!code-vb[](../../samples/word/change_the_print_orientation/vb/Program.vb#snippet4)] *** @@ -158,61 +102,26 @@ The next block of code iterates through all the sections in the collection of ** ## Setting the Orientation for the Section -The next block of code first checks whether the [Orient](/dotnet/api/documentformat.openxml.wordprocessing.pagesize.orient) property of the **PageSize** element exists. As with many properties +The next block of code first checks whether the +property of the `PageSize` element exists. As with many properties of Open XML elements, the property or attribute might not exist yet. In that case, retrieving the property returns a null reference. By default, if the property does not exist, and the new orientation is Portrait, the -code will not update the page. If the **Orient** property already exists, and its value +code will not update the page. If the `Orient` property already exists, and its value differs from the new orientation value supplied as a parameter to the -method, the code sets the **Value** property of -the **Orient** property, and sets both the -**pageOrientationChanged** and the **documentChanged** flags. (The code uses the **pageOrientationChanged** flag to determine whether it -must update the page size and margins. It uses the **documentChanged** flag to determine whether it must +method, the code sets the `Value` property of +the `Orient` property, and sets both the +`pageOrientationChanged` and the `documentChanged` flags. (The code uses the `pageOrientationChanged` flag to determine whether it +must update the page size and margins. It uses the `documentChanged` flag to determine whether it must save the document at the end.) > [!NOTE] -> If the code must create the **Orient** property, it must also create the value to store in the property, as a new [EnumValue\](/dotnet/api/documentformat.openxml.enumvalue-1) instance, supplying the new orientation in the **EnumValue** constructor. +> If the code must create the `Orient` property, it must also create the value to store in the property, as a new instance, supplying the new orientation in the `EnumValue` constructor. ### [C#](#tab/cs-4) -```csharp - if (pgSz.Orient == null) - { - if (newOrientation != PageOrientationValues.Portrait) - { - pageOrientationChanged = true; - documentChanged = true; - pgSz.Orient = - new EnumValue(newOrientation); - } - } - else - { - if (pgSz.Orient.Value != newOrientation) - { - pgSz.Orient.Value = newOrientation; - pageOrientationChanged = true; - documentChanged = true; - } - } -``` - +[!code-csharp[](../../samples/word/change_the_print_orientation/cs/Program.cs#snippet5)] ### [Visual Basic](#tab/vb-4) -```vb - If pgSz.Orient Is Nothing Then - If newOrientation <> PageOrientationValues.Portrait Then - pageOrientationChanged = True - documentChanged = True - pgSz.Orient = - New EnumValue(Of PageOrientationValues)(newOrientation) - End If - Else - If pgSz.Orient.Value <> newOrientation Then - pgSz.Orient.Value = newOrientation - pageOrientationChanged = True - documentChanged = True - End If - End If -``` +[!code-vb[](../../samples/word/change_the_print_orientation/vb/Program.vb#snippet5)] *** @@ -224,34 +133,12 @@ At this point in the code, the page orientation may have changed. If so, the code must complete two more tasks. It must update the page size, and update the page margins for the section. The first task is easy—the following code just swaps the page height and width, storing the values -in the **PageSize** element. +in the `PageSize` element. ### [C#](#tab/cs-5) -```csharp - if (pageOrientationChanged) - { - // Changing the orientation is not enough. You must also - // change the page size. - var width = pgSz.Width; - var height = pgSz.Height; - pgSz.Width = height; - pgSz.Height = width; - // Code removed here... - } -``` - +[!code-csharp[](../../samples/word/change_the_print_orientation/cs/Program.cs#snippet6)] ### [Visual Basic](#tab/vb-5) -```vb - If pageOrientationChanged Then - ' Changing the orientation is not enough. You must also - ' change the page size. - Dim width = pgSz.Width - Dim height = pgSz.Height - pgSz.Width = height - pgSz.Height = width - ' Code removed here... - End If -``` +[!code-vb[](../../samples/word/change_the_print_orientation/vb/Program.vb#snippet6)] *** @@ -261,53 +148,17 @@ in the **PageSize** element. The next step in the sample procedure handles margins for the section. If the page orientation has changed, the code must rotate the margins to -match. To do so, the code retrieves a reference to the [PageMargin](/dotnet/api/documentformat.openxml.wordprocessing.pagemargin) element for the section. If the -element exists, the code rotates the margins. Note that the code rotates +match. To do so, the code retrieves a reference to the element for the section. If the element exists, the code rotates the margins. Note that the code rotates the margins by 90 degrees—some printers rotate the margins by 270 degrees instead and you could modify the code to take that into account. -Also be aware that the [Top](/dotnet/api/documentformat.openxml.wordprocessing.pagemargin.top) and [Bottom](/dotnet/api/documentformat.openxml.wordprocessing.pagemargin.bottom) properties of the **PageMargin** object are signed values, and the -[Left](/dotnet/api/documentformat.openxml.wordprocessing.pagemargin.left) and [Right](/dotnet/api/documentformat.openxml.wordprocessing.pagemargin.right) properties are unsigned values. The -code must convert between the two types of values as it rotates the +Also be aware that the and properties of the `PageMargin` object are signed values, and the + and properties are unsigned values. The code must convert between the two types of values as it rotates the margin settings, as shown in the following code. ### [C#](#tab/cs-6) -```csharp - PageMargin pgMar = - sectPr.Descendants().FirstOrDefault(); - if (pgMar != null) - { - var top = pgMar.Top.Value; - var bottom = pgMar.Bottom.Value; - var left = pgMar.Left.Value; - var right = pgMar.Right.Value; - - pgMar.Top = new Int32Value((int)left); - pgMar.Bottom = new Int32Value((int)right); - pgMar.Left = - new UInt32Value((uint)System.Math.Max(0, bottom)); - pgMar.Right = - new UInt32Value((uint)System.Math.Max(0, top)); - } -``` - +[!code-csharp[](../../samples/word/change_the_print_orientation/cs/Program.cs#snippet7)] ### [Visual Basic](#tab/vb-6) -```vb - Dim pgMar As PageMargin = - sectPr.Descendants(Of PageMargin).FirstOrDefault() - If pgMar IsNot Nothing Then - Dim top = pgMar.Top.Value - Dim bottom = pgMar.Bottom.Value - Dim left = pgMar.Left.Value - Dim right = pgMar.Right.Value - - pgMar.Top = CType(left, Int32Value) - pgMar.Bottom = CType(right, Int32Value) - pgMar.Left = CType(System.Math.Max(0, - CType(bottom, Int32Value)), UInt32Value) - pgMar.Right = CType(System.Math.Max(0, - CType(top, Int32Value)), UInt32Value) - End If -``` +[!code-vb[](../../samples/word/change_the_print_orientation/vb/Program.vb#snippet7)] *** @@ -319,19 +170,9 @@ After all the modifications, the code determines whether the document has changed. If the document has changed, the code saves it. ### [C#](#tab/cs-7) -```csharp - if (documentChanged) - { - docPart.Document.Save(); - } -``` - +[!code-csharp[](../../samples/word/change_the_print_orientation/cs/Program.cs#snippet8)] ### [Visual Basic](#tab/vb-7) -```vb - If documentChanged Then - docPart.Document.Save() - End If -``` +[!code-vb[](../../samples/word/change_the_print_orientation/vb/Program.vb#snippet8)] *** @@ -339,14 +180,15 @@ has changed. If the document has changed, the code saves it. ## Sample Code -The following is the complete **SetPrintOrientation** code sample in C\# and Visual +The following is the complete `SetPrintOrientation` code sample in C\# and Visual Basic. ### [C#](#tab/cs) -[!code-csharp[](../../samples/word/change_the_print_orientation/cs/Program.cs)] +[!code-csharp[](../../samples/word/change_the_print_orientation/cs/Program.cs#snippet0)] ### [Visual Basic](#tab/vb) -[!code-vb[](../../samples/word/change_the_print_orientation/vb/Program.vb)] +[!code-vb[](../../samples/word/change_the_print_orientation/vb/Program.vb#snippet0)] +*** ----------------------------------------------------------------------------- diff --git a/samples/word/change_the_print_orientation/cs/Program.cs b/samples/word/change_the_print_orientation/cs/Program.cs index fa990838..4d9ca6cc 100644 --- a/samples/word/change_the_print_orientation/cs/Program.cs +++ b/samples/word/change_the_print_orientation/cs/Program.cs @@ -1,49 +1,62 @@ -#nullable enable - using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using System; +using System.Collections.Generic; using System.Linq; +// SetPrintOrientation(args[0], args[1]); +// // Given a document name, set the print orientation for // all the sections of the document. -static void SetPrintOrientation(string fileName, string no) +// +// +static void SetPrintOrientation(string fileName, string orientation) + // { - PageOrientationValues newOrientation = no.ToLower() switch + // + PageOrientationValues newOrientation = orientation.ToLower() switch { "landscape" => PageOrientationValues.Landscape, "portrait" => PageOrientationValues.Portrait, - _ => throw new System.ArgumentException("Invalid argument: " + no) + _ => throw new System.ArgumentException("Invalid argument: " + orientation) }; - using (var document = - WordprocessingDocument.Open(fileName, true)) + using (var document = WordprocessingDocument.Open(fileName, true)) { bool documentChanged = false; - if (document.MainDocumentPart is null) + if (document?.MainDocumentPart?.Document.Body is null) { throw new ArgumentNullException("MainDocumentPart and/or Body is null."); } - var docPart = document.MainDocumentPart; + Body docBody = document.MainDocumentPart.Document.Body; + IEnumerable sections = docBody.ChildElements.OfType(); - var sections = docPart.Document.Descendants(); + if (sections.Count() == 0) + { + docBody.AddChild(new SectionProperties()); + sections = docBody.ChildElements.OfType(); + } + // + + // foreach (SectionProperties sectPr in sections) { bool pageOrientationChanged = false; - PageSize pgSz = sectPr.Descendants().First(); + PageSize pgSz = sectPr.ChildElements.OfType().FirstOrDefault() ?? sectPr.AppendChild(new PageSize() { Width = 12240, Height = 15840 }); // No Orient property? Create it now. Otherwise, just - // set its value. Assume that the default orientation - // is Portrait. + // set its value. Assume that the default orientation is Portrait. + // if (pgSz.Orient is null) + // { // Need to create the attribute. You do not need to // create the Orient property if the property does not @@ -53,8 +66,7 @@ static void SetPrintOrientation(string fileName, string no) { pageOrientationChanged = true; documentChanged = true; - pgSz.Orient = - new EnumValue(newOrientation); + pgSz.Orient = new EnumValue(newOrientation); } } else @@ -67,7 +79,9 @@ static void SetPrintOrientation(string fileName, string no) pageOrientationChanged = true; documentChanged = true; } + // + // if (pageOrientationChanged) { // Changing the orientation is not enough. You must also @@ -76,8 +90,10 @@ static void SetPrintOrientation(string fileName, string no) var height = pgSz.Height; pgSz.Width = height; pgSz.Height = width; + // - PageMargin pgMar = (sectPr.Descendants().FirstOrDefault()) ?? throw new ArgumentNullException("There are no PageMargin elements in the section."); + // + PageMargin? pgMar = sectPr.Descendants().FirstOrDefault(); if (pgMar is not null) { @@ -98,17 +114,20 @@ static void SetPrintOrientation(string fileName, string no) pgMar.Top = new Int32Value((int)left); pgMar.Bottom = new Int32Value((int)right); - pgMar.Left = - new UInt32Value((uint)System.Math.Max(0, bottom)); - pgMar.Right = - new UInt32Value((uint)System.Math.Max(0, top)); + pgMar.Left = new UInt32Value((uint)System.Math.Max(0, bottom)); + pgMar.Right = new UInt32Value((uint)System.Math.Max(0, top)); } + // } } } + + // if (documentChanged) { - docPart.Document.Save(); + document.MainDocumentPart.Document.Save(); } + // } } +// \ No newline at end of file diff --git a/samples/word/change_the_print_orientation/vb/Program.vb b/samples/word/change_the_print_orientation/vb/Program.vb index 587b64ae..dba52b5c 100644 --- a/samples/word/change_the_print_orientation/vb/Program.vb +++ b/samples/word/change_the_print_orientation/vb/Program.vb @@ -4,28 +4,52 @@ Imports DocumentFormat.OpenXml.Wordprocessing Module Program Sub Main(args As String()) + ' + SetPrintOrientation(args(0), args(1)) + ' End Sub ' Given a document name, set the print orientation for ' all the sections of the document. - Public Sub SetPrintOrientation( - ByVal fileName As String, ByVal newOrientation As PageOrientationValues) - Using document = - WordprocessingDocument.Open(fileName, True) + + ' + ' + Public Sub SetPrintOrientation(ByVal fileName As String, ByVal orientation As String) + ' + + ' + Dim newOrientation As PageOrientationValues + + Select Case orientation + Case "landscape" + newOrientation = PageOrientationValues.Landscape + Case "portrait" + newOrientation = PageOrientationValues.Portrait + Case Else + Throw New ArgumentException("Invalid orientation") + End Select + + + Using document = WordprocessingDocument.Open(fileName, True) Dim documentChanged As Boolean = False Dim docPart = document.MainDocumentPart Dim sections = docPart.Document.Descendants(Of SectionProperties)() + ' + ' For Each sectPr As SectionProperties In sections Dim pageOrientationChanged As Boolean = False - Dim pgSz As PageSize = - sectPr.Descendants(Of PageSize).FirstOrDefault + Dim pgSz As PageSize = sectPr.Descendants(Of PageSize).FirstOrDefault + + ' If pgSz IsNot Nothing Then + ' + ' No Orient property? Create it now. Otherwise, just ' set its value. Assume that the default orientation ' is Portrait. @@ -49,7 +73,9 @@ Module Program documentChanged = True End If End If + ' + ' If pageOrientationChanged Then ' Changing the orientation is not enough. You must also ' change the page size. @@ -57,9 +83,10 @@ Module Program Dim height = pgSz.Height pgSz.Width = height pgSz.Height = width + ' - Dim pgMar As PageMargin = - sectPr.Descendants(Of PageMargin).FirstOrDefault() + ' + Dim pgMar As PageMargin = sectPr.Descendants(Of PageMargin).FirstOrDefault() If pgMar IsNot Nothing Then ' Rotate margins. Printer settings control how far you ' rotate when switching to landscape mode. Not having those @@ -73,18 +100,20 @@ Module Program pgMar.Top = CType(left, Int32Value) pgMar.Bottom = CType(right, Int32Value) - pgMar.Left = CType(System.Math.Max(0, - CType(bottom, Int32Value)), UInt32Value) - pgMar.Right = CType(System.Math.Max(0, - CType(top, Int32Value)), UInt32Value) + pgMar.Left = CType(System.Math.Max(0, CType(bottom, Int32Value)), UInt32Value) + pgMar.Right = CType(System.Math.Max(0, CType(top, Int32Value)), UInt32Value) End If + ' End If End If Next + ' If documentChanged Then docPart.Document.Save() End If + ' End Using End Sub + ' End Module \ No newline at end of file