From 1306038ac939170f9868d75e206db04542efe469 Mon Sep 17 00:00:00 2001 From: "muhammad.sabir" Date: Wed, 12 Jun 2024 15:20:13 +0500 Subject: [PATCH] v24.6.0 - https://github.com/openize-words/Openize.Words-for-.NET/issues/12 - https://github.com/openize-words/Openize.Words-for-.NET/issues/13 - https://github.com/openize-words/Openize.Words-for-.NET/issues/14 -https://github.com/openize-words/Openize.Words-for-.NET/issues/15 --- OpenXML.Templates.cs | 6 +- OpenXML.Words.Data.cs | 18 ++-- OpenXML.Words.cs | 206 +++++++++++++++++++++++++++++-------- Openize.Words.IElements.cs | 49 +++++++++ Openize.Words.cs | 119 ++++++++++----------- Openize.Words.csproj | 16 +++ 6 files changed, 302 insertions(+), 112 deletions(-) diff --git a/OpenXML.Templates.cs b/OpenXML.Templates.cs index bf64ce0..3f3223e 100644 --- a/OpenXML.Templates.cs +++ b/OpenXML.Templates.cs @@ -12816,7 +12816,7 @@ private void GeneratePartContent(CoreFilePropertiesPart part, var subject = dict["Subject"];// "My Subject";//dict["Subject"]; var keywords = dict["Keywords"];//"My Keyword";//dict["Keywords"]; var description = dict["Description"];// "My Description";//dict["Description"]; - var creator = dict["Creator"];// "FileFormat.Words"; //dict["Creator"]; + var creator = dict["Creator"];// "Openize.Words"; //dict["Creator"]; var created = dict["Created"]; var modified = dict["Modified"]; var writer = new System.Xml.XmlTextWriter(part.GetStream( @@ -12850,7 +12850,7 @@ private void GeneratePartContent(ExtendedFilePropertiesPart part) var characters1 = new Ap.Characters(); characters1.Text = "0"; var application1 = new Ap.Application(); - application1.Text = "FileFormat.Words"; + application1.Text = "Openize.Words"; var documentSecurity1 = new Ap.DocumentSecurity(); documentSecurity1.Text = "0"; var lines1 = new Ap.Lines(); @@ -12860,7 +12860,7 @@ private void GeneratePartContent(ExtendedFilePropertiesPart part) var scaleCrop1 = new Ap.ScaleCrop(); scaleCrop1.Text = "false"; var company1 = new Ap.Company(); - company1.Text = "FileFormat.Words"; + company1.Text = "Openize.Words"; var linksUpToDate1 = new Ap.LinksUpToDate(); linksUpToDate1.Text = "false"; var charactersWithSpaces1 = new Ap.CharactersWithSpaces(); diff --git a/OpenXML.Words.Data.cs b/OpenXML.Words.Data.cs index 3b6ea17..81a53d4 100644 --- a/OpenXML.Words.Data.cs +++ b/OpenXML.Words.Data.cs @@ -57,7 +57,7 @@ internal void Insert(FF.IElement newElement, int position, Document doc) { _staticDocDict.TryGetValue(doc.GetInstanceInfo(), out WordprocessingDocument staticDoc); - if (staticDoc?.MainDocumentPart?.Document?.Body == null) throw new FileFormatException("Package or Document or Body is null", new NullReferenceException()); + if (staticDoc?.MainDocumentPart?.Document?.Body == null) throw new OpenizeException("Package or Document or Body is null", new NullReferenceException()); _ooxmlDoc = OwDocument.CreateInstance(staticDoc); @@ -95,7 +95,7 @@ internal void Insert(FF.IElement newElement, int position, Document doc) staticDoc.MainDocumentPart.Document.Body.RemoveAllChildren(); staticDoc.MainDocumentPart.Document.Body.Append(originalElements); var errorMessage = ConstructMessage(ex, "Remove OOXML Element(s)"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -106,7 +106,7 @@ internal void Update(FF.IElement newElement, int position, Document doc) { _staticDocDict.TryGetValue(doc.GetInstanceInfo(), out WordprocessingDocument staticDoc); - if (staticDoc?.MainDocumentPart?.Document?.Body == null) throw new FileFormatException("Package or Document or Body is null", new NullReferenceException()); + if (staticDoc?.MainDocumentPart?.Document?.Body == null) throw new OpenizeException("Package or Document or Body is null", new NullReferenceException()); _ooxmlDoc = OwDocument.CreateInstance(staticDoc); @@ -147,7 +147,7 @@ internal void Update(FF.IElement newElement, int position, Document doc) staticDoc.MainDocumentPart.Document.Body.RemoveAllChildren(); staticDoc.MainDocumentPart.Document.Body.Append(originalElements); var errorMessage = ConstructMessage(ex, "Update OOXML Element(s)"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -158,7 +158,7 @@ internal void Remove(int position, Document doc) { _staticDocDict.TryGetValue(doc.GetInstanceInfo(), out WordprocessingDocument staticDoc); - if (staticDoc?.MainDocumentPart?.Document?.Body == null) throw new FileFormatException("Package or Document or Body is null", new NullReferenceException()); + if (staticDoc?.MainDocumentPart?.Document?.Body == null) throw new OpenizeException("Package or Document or Body is null", new NullReferenceException()); var enumerable = staticDoc.MainDocumentPart.Document.Body.Elements().ToList(); var originalElements = new List(enumerable); @@ -174,7 +174,7 @@ internal void Remove(int position, Document doc) staticDoc.MainDocumentPart.Document.Body.RemoveAllChildren(); staticDoc.MainDocumentPart.Document.Body.Append(originalElements); var errorMessage = ConstructMessage(ex, "Remove OOXML Element(s)"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -185,7 +185,7 @@ internal void Append(FF.IElement newElement, Document doc) { _staticDocDict.TryGetValue(doc.GetInstanceInfo(), out WordprocessingDocument staticDoc); - if (staticDoc?.MainDocumentPart?.Document?.Body == null) throw new FileFormatException("Package or Document or Body is null", new NullReferenceException()); + if (staticDoc?.MainDocumentPart?.Document?.Body == null) throw new OpenizeException("Package or Document or Body is null", new NullReferenceException()); _ooxmlDoc = OwDocument.CreateInstance(staticDoc); @@ -225,7 +225,7 @@ internal void Append(FF.IElement newElement, Document doc) staticDoc.MainDocumentPart.Document.Body.RemoveAllChildren(); staticDoc.MainDocumentPart.Document.Body.Append(originalElements); var errorMessage = ConstructMessage(ex, "Append OOXML Element(s)"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -245,7 +245,7 @@ internal void Save(System.IO.Stream stream, Document doc) catch (Exception ex) { var errorMessage = ConstructMessage(ex, "Save OOXML OWDocument"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } diff --git a/OpenXML.Words.cs b/OpenXML.Words.cs index cc03165..6c82c0e 100644 --- a/OpenXML.Words.cs +++ b/OpenXML.Words.cs @@ -11,6 +11,7 @@ using FF = Openize.Words.IElements; using OWD = OpenXML.Words.Data; using OT = OpenXML.Templates; +using Openize.Words; namespace OpenXML.Words { @@ -51,7 +52,7 @@ private OwDocument() catch (Exception ex) { var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Initialize OOXML Element(s)"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -83,7 +84,7 @@ private OwDocument(WordprocessingDocument pkg) catch (Exception ex) { var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Initialize OOXML Element(s)"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -108,7 +109,7 @@ internal void CreateProperties(WordprocessingDocument pkgDocument) ["Subject"] = "WordProcessing OWDocument Generation", ["Keywords"] = "DOCX", ["Description"] = "A WordProcessing OWDocument Created from Scratch.", - ["Creator"] = "FileFormat.Words" + ["Creator"] = "Openize.Words" }; var currentTime = System.DateTime.UtcNow; dictCoreProp["Created"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ"); @@ -129,7 +130,7 @@ public static OwDocument CreateInstance(WordprocessingDocument pkg) return new OwDocument(pkg); } - #region Create OpenXML Word Document Contents Based on FileFormat.Words.IElements + #region Create OpenXML Word Document Contents Based on Openize.Words.IElements #region Main Method internal void CreateDocument(List lst) @@ -139,7 +140,7 @@ internal void CreateDocument(List lst) _wpBody = _mainPart.Document.Body; if (_wpBody == null) - throw new FileFormatException("Package or Document or Body is null", new NullReferenceException()); + throw new OpenizeException("Package or Document or Body is null", new NullReferenceException()); var sectionProperties = _wpBody.Elements().FirstOrDefault(); @@ -171,7 +172,7 @@ internal void CreateDocument(List lst) catch (Exception ex) { var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Initialize OOXML Element(s)"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } @@ -189,8 +190,11 @@ internal WP.Paragraph CreateParagraph(FF.Paragraph ffP) if (ffP.Style != null) { var paragraphProperties = new WP.ParagraphProperties(); + var paragraphStyleId = new WP.ParagraphStyleId { Val = ffP.Style }; paragraphProperties.Append(paragraphStyleId); + + #region Create List Paragraph if (ffP.Style == "ListParagraph") { @@ -320,15 +324,54 @@ internal WP.Paragraph CreateParagraph(FF.Paragraph ffP) numberingProperties.Append(numberingId); paragraphProperties.Append(numberingProperties); } - WP.JustificationValues justificationValue = CreateJustification(ffP.Alignment); + #endregion - paragraphProperties.Append(new WP.Justification { Val = justificationValue }); - if (ffP.Indentation != null) + // Create Borders + if (ffP.ParagraphBorder.Size > 0) { - CreateIndentation(paragraphProperties, ffP.Indentation); + WP.ParagraphBorders paragraphBorders = new WP.ParagraphBorders(); + WP.TopBorder topBorder = new WP.TopBorder() + { Val = CreateBorder(ffP.ParagraphBorder.Width), + Color = ffP.ParagraphBorder.Color, + Size = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size, + Space = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size + }; + WP.LeftBorder leftBorder = new WP.LeftBorder() + { + Val = CreateBorder(ffP.ParagraphBorder.Width), + Color = ffP.ParagraphBorder.Color, + Size = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size, + Space = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size + }; + WP.BottomBorder bottomBorder = new WP.BottomBorder() + { + Val = CreateBorder(ffP.ParagraphBorder.Width), + Color = ffP.ParagraphBorder.Color, + Size = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size, + Space = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size + }; + WP.RightBorder rightBorder = new WP.RightBorder() + { + Val = CreateBorder(ffP.ParagraphBorder.Width), + Color = ffP.ParagraphBorder.Color, + Size = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size, + Space = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size + }; + + paragraphBorders.Append(topBorder); + paragraphBorders.Append(leftBorder); + paragraphBorders.Append(bottomBorder); + paragraphBorders.Append(rightBorder); + + paragraphProperties.Append(paragraphBorders); } + // Create Justification + WP.JustificationValues justificationValue = CreateJustification(ffP.Alignment); + paragraphProperties.Append(new WP.Justification { Val = justificationValue }); + // Create Indentation + CreateIndentation(paragraphProperties, ffP.Indentation); wpParagraph.Append(paragraphProperties); } @@ -391,7 +434,7 @@ internal WP.Paragraph CreateParagraph(FF.Paragraph ffP) catch (Exception ex) { var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Create Paragraph"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -413,6 +456,23 @@ private WP.JustificationValues CreateJustification(FF.ParagraphAlignment alignme } } + private WP.BorderValues CreateBorder(FF.BorderWidth borderWidth) + { + switch (borderWidth) + { + case FF.BorderWidth.Single: + return WP.BorderValues.Single; + case FF.BorderWidth.Double: + return WP.BorderValues.Double; + case FF.BorderWidth.Dotted: + return WP.BorderValues.Dotted; + case FF.BorderWidth.DotDash: + return WP.BorderValues.DotDash; + default: + return WP.BorderValues.Single; + } + } + private void CreateIndentation(WP.ParagraphProperties paragraphProperties, FF.Indentation ffIndentation) { var indentation = new WP.Indentation(); @@ -491,7 +551,7 @@ internal WP.Table CreateTable(FF.Table ffTable) catch (Exception ex) { var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Create Table"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -597,14 +657,14 @@ internal WP.Paragraph CreateImage(FF.Image ffImg, MainDocumentPart mainPart) catch (Exception ex) { var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Create Image"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } #endregion #endregion - #region Load OpenXML Word Document Content into FileFormat.Words.IElements + #region Load OpenXML Word Document Content into Openize.Words.IElements #region Main Method internal List LoadDocument(Stream stream) @@ -615,7 +675,7 @@ internal WP.Paragraph CreateImage(FF.Image ffImg, MainDocumentPart mainPart) { _pkgDocument = WordprocessingDocument.Open(stream, true); - if (_pkgDocument.MainDocumentPart?.Document?.Body == null) throw new FileFormatException("Package or Document or Body is null", new NullReferenceException()); + if (_pkgDocument.MainDocumentPart?.Document?.Body == null) throw new OpenizeException("Package or Document or Body is null", new NullReferenceException()); OWD.OoxmlDocData.CreateInstance(_pkgDocument); @@ -697,11 +757,12 @@ internal WP.Paragraph CreateImage(FF.Image ffImg, MainDocumentPart mainPart) } return elements; + } catch (Exception ex) { var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Load OOXML Elements"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -761,34 +822,44 @@ internal FF.Paragraph LoadParagraph(WP.Paragraph wpPara, int id) } } + // Load Border + if(isBordered(paraProps)) + { + var topBorder = paraProps.ParagraphBorders?.TopBorder; + if (topBorder != null) + { + ffP.ParagraphBorder.Width = LoadBorder(topBorder.Val); + ffP.ParagraphBorder.Color = topBorder.Color; + ffP.ParagraphBorder.Size = (int)(uint)topBorder.Size; + } + } - var justificationElement = paraProps.Elements().FirstOrDefault(); - if (justificationElement != null) + // Load Justification + if (isJustified(paraProps)) { - ffP.Alignment = LoadAlignment(justificationElement.Val); + var justificationElement = paraProps.Elements().FirstOrDefault(); + if (justificationElement != null) + ffP.Alignment = LoadAlignment(justificationElement.Val); } - var Indentation = paraProps.Elements().FirstOrDefault(); - if (Indentation != null) + else ffP.Alignment = FF.ParagraphAlignment.Left; + + // Load Indentation + if (isIndented(paraProps)) { - if (Indentation.Left != null) + var Indentation = paraProps.Elements().FirstOrDefault(); + if (Indentation != null) { - ffP.Indentation.Left = int.Parse(Indentation.Left); - } - if (Indentation.Right != null) - { - ffP.Indentation.Right = int.Parse(Indentation.Right); - } - if (Indentation.Hanging != null) - { - ffP.Indentation.Hanging = int.Parse(Indentation.Hanging); - } - if (Indentation.FirstLine != null) - { - ffP.Indentation.FirstLine = int.Parse(Indentation.FirstLine); + if (Indentation.Left != null) + ffP.Indentation.Left = int.Parse(Indentation.Left); + if (Indentation.Right != null) + ffP.Indentation.Right = int.Parse(Indentation.Right); + if (Indentation.Hanging != null) + ffP.Indentation.Hanging = int.Parse(Indentation.Hanging); + if (Indentation.FirstLine != null) + ffP.Indentation.FirstLine = int.Parse(Indentation.FirstLine); } } - var runs = wpPara.Elements(); foreach (var wpR in runs) @@ -812,10 +883,11 @@ internal FF.Paragraph LoadParagraph(WP.Paragraph wpPara, int id) return ffP; } + catch (Exception ex) { var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Load Paragraph"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -834,6 +906,58 @@ private FF.ParagraphAlignment LoadAlignment(WP.JustificationValues justification return FF.ParagraphAlignment.Left; } + private FF.BorderWidth LoadBorder(WP.BorderValues borderValue) + { + if (borderValue == WP.BorderValues.Single) + return FF.BorderWidth.Single; + else if (borderValue == WP.BorderValues.Double) + return FF.BorderWidth.Double; + else if (borderValue == WP.BorderValues.Dotted) + return FF.BorderWidth.Dotted; + else if (borderValue == WP.BorderValues.DotDash) + return FF.BorderWidth.DotDash; + else + return FF.BorderWidth.Single; + } + + private bool isBordered(WP.ParagraphProperties prop) + { + try + { + var paragraphBorders = prop.ParagraphBorders; + return true; + } + catch (Exception ex) + { + return false; + } + } + + private bool isJustified(WP.ParagraphProperties prop) + { + try + { + var justification = prop.Justification; + return true; + } + catch (Exception ex) + { + return false; + } + } + + private bool isIndented(WP.ParagraphProperties prop) + { + try + { + var indentation = prop.Indentation; + return true; + } + catch (Exception ex) + { + return false; + } + } #endregion @@ -886,7 +1010,7 @@ internal FF.Image LoadImage(WP.Drawing drawing, int sequence) catch (Exception ex) { var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Load Image"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -946,7 +1070,7 @@ internal FF.Table LoadTable(WP.Table wpTable, int id) catch (Exception ex) { var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Load Table"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -996,7 +1120,7 @@ internal FF.Section LoadSection(WP.SectionProperties sectPr, int id) catch (Exception ex) { var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Load Section"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -1093,7 +1217,7 @@ internal void SaveDocument(Stream stream) catch (Exception ex) { //var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Save OOXML OWDocument"); - //throw new FileFormatException(errorMessage, ex); + //throw new OpenizeException(errorMessage, ex); throw new Exception(ex.Message); } } diff --git a/Openize.Words.IElements.cs b/Openize.Words.IElements.cs index cf994ff..617cec1 100644 --- a/Openize.Words.IElements.cs +++ b/Openize.Words.IElements.cs @@ -37,6 +37,11 @@ public class Indentation /// Gets or sets the distance of the hanging indentation. /// public double Hanging { get; set; } + + //public Indentation() + //{ + //Left = 0; + //} } /// @@ -65,6 +70,44 @@ public enum ParagraphAlignment Justify } + /// + /// Specifies the border width of an element within a text block or document. + /// + public enum BorderWidth + { + /// + /// Aligns the paragraph to the left. + /// + Single, + + /// + /// Centers the paragraph within the available space. + /// + Double, + + /// + /// Aligns the paragraph to the right. + /// + Dotted, + + /// + /// Justifies the text within the paragraph, aligning both the left and right edges. + /// + DotDash + } + + public class Border + { + public BorderWidth Width { get; set; } + public string Color { get; set; } + public int Size { get; set; } + + public Border() + { + Size = 0; + } + } + /// /// Represents a paragraph element in a Word document. /// @@ -130,6 +173,11 @@ public class Paragraph : IElement /// public bool IsAlphabeticNumber { get; set; } + /// + /// Gets or sets whether the paragraph border. + /// + public Border ParagraphBorder { get; set; } + /// /// Initializes a new instance of the class. /// @@ -137,6 +185,7 @@ public Paragraph() { Runs = new List(); Style = "Normal"; + ParagraphBorder = new Border(); Indentation = new Indentation(); NumberingId = null; NumberingLevel = null; diff --git a/Openize.Words.cs b/Openize.Words.cs index 449aacb..1abc2e2 100644 --- a/Openize.Words.cs +++ b/Openize.Words.cs @@ -11,14 +11,14 @@ namespace Openize.Words /// /// Custom exception class for file format-related exceptions. /// - public class FileFormatException : Exception + public class OpenizeException : Exception { /// - /// Initializes a new instance of the class with a specified error message and a reference to the inner exception. + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception. /// /// The error message that explains the reason for the exception. /// The exception that is the cause of the current exception, or a null reference if no inner exception is specified. - public FileFormatException(string message, Exception innerException) : base(message, innerException) + public OpenizeException(string message, Exception innerException) : base(message, innerException) { //Do nothing } @@ -86,7 +86,7 @@ public Document() catch (Exception ex) { var errorMessage = ConstructMessage(ex, "Initializing OWDocument"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -94,7 +94,7 @@ public Document() /// Initializes a new instance of the class by loading content from a file specified by its filename. /// /// The path to the file from which to load the document content. - /// + /// /// Thrown if an error occurs while loading the document. /// public Document(string filename) @@ -116,11 +116,12 @@ public Document(string filename) _lstStructure = _ooxmlDoc.LoadDocument(_ms); _elementStyles = _ooxmlDoc.LoadStyles(); _originalSize = _lstStructure.Count; + } catch (Exception ex) { var errorMessage = ConstructMessage(ex, "Loading Document from file"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -128,7 +129,7 @@ public Document(string filename) /// Initializes a new instance of the class by loading content from a . /// /// The input stream from which to load the document content. - /// + /// /// Thrown if an error occurs while loading the document. /// public Document(IO.Stream stream) @@ -150,7 +151,7 @@ public Document(IO.Stream stream) catch (Exception ex) { var errorMessage = ConstructMessage(ex, "Loading Document from stream"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -158,7 +159,7 @@ public Document(IO.Stream stream) /// Saves the document to a file specified by its filename. /// /// The path to the file where the document will be saved. - /// + /// /// Thrown if an error occurs while saving the document. /// public void Save(string filename) @@ -183,7 +184,7 @@ public void Save(string filename) catch (Exception ex) { var errorMessage = ConstructMessage(ex, "Saving Document to file"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -192,7 +193,7 @@ public void Save(string filename) /// Saves the document to the specified . /// /// The stream to which the document will be saved. - /// + /// /// Thrown if an error occurs while saving the document. /// public void Save(IO.Stream stream) @@ -215,13 +216,13 @@ public void Save(IO.Stream stream) catch (Exception ex) { var errorMessage = ConstructMessage(ex, "Saving Document to stream"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } private string ConstructMessage(Exception ex, string operation) { - return $"Error in Operation {operation} at FileFormat.Words: {ex.Message} \n Inner Exception: {ex.InnerException?.Message ?? "N/A"}"; + return $"Error in Operation {operation} at Openize.Words: {ex.Message} \n Inner Exception: {ex.InnerException?.Message ?? "N/A"}"; } internal int GetInstanceInfo() @@ -254,7 +255,7 @@ public ElementStyles GetElementStyles() /// /// true if the element is successfully updated; otherwise, false. /// If the element is not found in the structure, false is returned. - /// If an error occurs during the update operation, a is thrown. + /// If an error occurs during the update operation, a is thrown. /// /// /// This method updates an existing element within the structure based on the element's unique identifier. @@ -262,7 +263,7 @@ public ElementStyles GetElementStyles() /// If the update operation is successful, the method returns true>. /// If the element is not found in the structure, false is returned, and no changes are made to the structure. /// If an exception occurs during the update operation, the method attempts to restore the structure to its previous state - /// and throws a with detailed error information. + /// and throws a with detailed error information. /// public bool Update(IElement element) { @@ -270,7 +271,7 @@ public bool Update(IElement element) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } var position = _lstStructure.FindIndex(e => e.ElementId == element.ElementId); @@ -290,7 +291,7 @@ public bool Update(IElement element) { _lstStructure[position] = backupElement; var errorMessage = ConstructMessage(ex, "Update"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -302,8 +303,8 @@ public bool Update(IElement element) /// /// true if the element is successfully updated; otherwise, false. /// If the element with the specified ID is not found in the structure, false is returned. - /// If the elements' IDs do not match, a is thrown. - /// If an error occurs during the update operation, a is thrown. + /// If the elements' IDs do not match, a is thrown. + /// If an error occurs during the update operation, a is thrown. /// /// /// This method updates an existing element within the structure based on the provided element's unique identifier. @@ -311,9 +312,9 @@ public bool Update(IElement element) /// If the IDs match, the method attempts to locate the element in the structure and replace it with the updated element. /// If the update operation is successful, the method returns true>. /// If the element with the specified ID is not found, false is returned, and no changes are made to the structure. - /// If the provided element's ID does not match the specified element ID, a is thrown. + /// If the provided element's ID does not match the specified element ID, a is thrown. /// If an exception occurs during the update operation, the method attempts to restore the structure to its previous state - /// and throws a with detailed error information. + /// and throws a with detailed error information. /// public bool Update(int elementId, IElement element) { @@ -321,13 +322,13 @@ public bool Update(int elementId, IElement element) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } if (elementId != element.ElementId) { var ex = new Exception("The elements mismatch: Update is only available for same element"); - throw new FileFormatException(ex.Message, new InvalidOperationException()); + throw new OpenizeException(ex.Message, new InvalidOperationException()); } var position = _lstStructure.FindIndex(e => e.ElementId == elementId); @@ -347,7 +348,7 @@ public bool Update(int elementId, IElement element) { _lstStructure[position] = backupElement; var errorMessage = ConstructMessage(ex, "Update"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -359,7 +360,7 @@ public bool Update(int elementId, IElement element) /// /// The unique identifier of inserted element if the new element is successfully inserted before the reference element. /// If the reference element is not found in the structure, -1 is returned. - /// If an error occurs during the insertion operation, a is thrown. + /// If an error occurs during the insertion operation, a is thrown. /// /// /// This method inserts the specified element before the provided reference element in the structure. @@ -367,7 +368,7 @@ public bool Update(int elementId, IElement element) /// If the insertion operation is successful, the method returns of the inserted element. /// If the reference element is not found, -1 is returned, and no changes are made to the structure. /// If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state - /// and throws a with detailed error information. + /// and throws a with detailed error information. /// public int InsertBefore(IElement newElement, IElement element) { @@ -375,7 +376,7 @@ public int InsertBefore(IElement newElement, IElement element) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } var position = _lstStructure.FindIndex(e => e.ElementId == element.ElementId); @@ -410,7 +411,7 @@ public int InsertBefore(IElement newElement, IElement element) { _lstStructure.RemoveAt(position); var errorMessage = ConstructMessage(ex, "InsertBefore"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -422,7 +423,7 @@ public int InsertBefore(IElement newElement, IElement element) /// /// The unique identifier of the inserted element if the new element is successfully inserted before the specified element. /// If the specified element with the provided ID is not found, -1 is returned. - /// If an error occurs during the insertion operation, a is thrown. + /// If an error occurs during the insertion operation, a is thrown. /// /// /// This method inserts the specified element before the element with the provided unique ID in the structure. @@ -430,7 +431,7 @@ public int InsertBefore(IElement newElement, IElement element) /// If the insertion operation is successful, the method returns of the inserted element. /// If the element with the specified ID is not found, -1 is returned, and no changes are made to the structure. /// If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state - /// and throws a with detailed error information. + /// and throws a with detailed error information. /// public int InsertBefore(IElement newElement, int elementId) { @@ -438,7 +439,7 @@ public int InsertBefore(IElement newElement, int elementId) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } var position = _lstStructure.FindIndex(e => e.ElementId == elementId); @@ -468,7 +469,7 @@ public int InsertBefore(IElement newElement, int elementId) { _lstStructure.RemoveAt(position); var errorMessage = ConstructMessage(ex, "InsertBefore"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -478,7 +479,7 @@ internal int Append(IElement newElement) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } //Console.WriteLine($"Number of Elements in List: {_lstStructure.Count}"); @@ -530,7 +531,7 @@ internal int Append(IElement newElement) } var errorMessage = ConstructMessage(ex, "Append"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -542,7 +543,7 @@ internal int Append(IElement newElement) /// /// The unique identifier if the new element is successfully inserted after the specified element. /// If the specified element is not found, -1 is returned. - /// If an error occurs during the insertion operation, a is thrown. + /// If an error occurs during the insertion operation, a is thrown. /// /// /// This method inserts the specified element after the provided element in the structure. @@ -550,7 +551,7 @@ internal int Append(IElement newElement) /// If the insertion operation is successful, the method returns of the inserted element. /// If the specified element is not found, -1 is returned, and no changes are made to the structure. /// If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state - /// and throws a with detailed error information. + /// and throws a with detailed error information. /// public int InsertAfter(IElement newElement, IElement element) { @@ -558,7 +559,7 @@ public int InsertAfter(IElement newElement, IElement element) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } var position = _lstStructure.FindIndex(e => e.ElementId == element.ElementId); @@ -588,7 +589,7 @@ public int InsertAfter(IElement newElement, IElement element) { _lstStructure.RemoveAt(position + 1); var errorMessage = ConstructMessage(ex, "InsertAfter"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -600,7 +601,7 @@ public int InsertAfter(IElement newElement, IElement element) /// /// The unique identifier of the inserted element if the new element is successfully inserted after the specified element. /// If the specified element is not found, -1 is returned. - /// If an error occurs during the insertion operation, a is thrown. + /// If an error occurs during the insertion operation, a is thrown. /// /// /// This method inserts the specified element after the element with the provided unique ID in the structure. @@ -608,7 +609,7 @@ public int InsertAfter(IElement newElement, IElement element) /// If the insertion operation is successful, the method returns of the inserted element. /// If the specified element is not found, -1 is returned, and no changes are made to the structure. /// If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state - /// and throws a with detailed error information. + /// and throws a with detailed error information. /// public int InsertAfter(IElement newElement, int elementId) { @@ -616,7 +617,7 @@ public int InsertAfter(IElement newElement, int elementId) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } var position = _lstStructure.FindIndex(e => e.ElementId == elementId); if (position < 0) return -1; @@ -645,7 +646,7 @@ public int InsertAfter(IElement newElement, int elementId) { _lstStructure.RemoveAt(position + 1); var errorMessage = ConstructMessage(ex, "InsertAfter"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -656,7 +657,7 @@ public int InsertAfter(IElement newElement, int elementId) /// /// true if the element following the specified element is successfully removed; otherwise, false. /// If the specified element is not found or if there is no element following it, false is returned. - /// If an error occurs during the removal operation, a is thrown. + /// If an error occurs during the removal operation, a is thrown. /// /// /// This method removes the element that comes after the specified element in the structure. It is essential to ensure that the @@ -664,7 +665,7 @@ public int InsertAfter(IElement newElement, int elementId) /// If the removal operation is successful, the method returns true. /// If the element is not found or there is no element following it, false is returned, and no changes are made to the structure. /// If an exception occurs during the removal operation, the method attempts to restore the structure to its previous state - /// and throws a with detailed error information. + /// and throws a with detailed error information. /// public bool RemoveAfter(IElement element) { @@ -672,7 +673,7 @@ public bool RemoveAfter(IElement element) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } var position = _lstStructure.FindIndex(e => e.ElementId == element.ElementId); if (position >= 0 && position < _lstStructure.Count - 1) @@ -688,7 +689,7 @@ public bool RemoveAfter(IElement element) { _lstStructure.Insert(position + 1, backupElement); var errorMessage = ConstructMessage(ex, "RemoveAfter"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } @@ -702,7 +703,7 @@ public bool RemoveAfter(IElement element) /// /// true if the element following the specified element is successfully removed; otherwise, false. /// If the specified element is not found or if there is no element following it, false is returned. - /// If an error occurs during the removal operation, a is thrown. + /// If an error occurs during the removal operation, a is thrown. /// /// /// This method removes the element that comes after the specified element with the provided unique ID in the structure. @@ -710,7 +711,7 @@ public bool RemoveAfter(IElement element) /// If the removal operation is successful, the method returns true. /// If the element is not found or there is no element following it, false is returned, and no changes are made to the structure. /// If an exception occurs during the removal operation, the method attempts to restore the structure to its previous state - /// and throws a with detailed error information. + /// and throws a with detailed error information. /// public bool RemoveAfter(int elementId) { @@ -718,7 +719,7 @@ public bool RemoveAfter(int elementId) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } var position = _lstStructure.FindIndex(e => e.ElementId == elementId); @@ -735,7 +736,7 @@ public bool RemoveAfter(int elementId) { _lstStructure.Insert(position + 1, backupElement); var errorMessage = ConstructMessage(ex, "RemoveAfter"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } @@ -756,7 +757,7 @@ public bool RemoveAfter(int elementId) /// The element whose predecessor should be removed. /// /// true if the preceding element is successfully removed; otherwise, false if the element is not found. - /// Throws a if an exception occurs during the operation. + /// Throws a if an exception occurs during the operation. /// /// public bool RemoveBefore(IElement element) @@ -765,7 +766,7 @@ public bool RemoveBefore(IElement element) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } var position = _lstStructure.FindIndex(e => e.ElementId == element.ElementId); @@ -781,7 +782,7 @@ public bool RemoveBefore(IElement element) { _lstStructure.Insert(position - 1, backupElement); var errorMessage = ConstructMessage(ex, "RemoveBefore"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -791,7 +792,7 @@ public bool RemoveBefore(IElement element) /// The unique identifier of the element after which you want to remove the preceding element. /// /// true if the preceding element is successfully removed; otherwise, false if the element is not found. - /// Throws a if an exception occurs during the operation. + /// Throws a if an exception occurs during the operation. /// /// /// If the specified element is not found in the document, this method returns false. @@ -804,7 +805,7 @@ public bool RemoveBefore(int elementId) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } var position = _lstStructure.FindIndex(e => e.ElementId == elementId); @@ -820,7 +821,7 @@ public bool RemoveBefore(int elementId) { _lstStructure.Insert(position - 1, backupElement); var errorMessage = ConstructMessage(ex, "RemoveBefore"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } @@ -830,7 +831,7 @@ public bool RemoveBefore(int elementId) /// The unique identifier of the element you want to remove. /// /// true if the element is successfully removed; otherwise, false if the element is not found. - /// Throws a if an exception occurs during the operation. + /// Throws a if an exception occurs during the operation. /// /// /// If the specified element is not found in the document, this method returns false. @@ -843,7 +844,7 @@ public bool Remove(int elementId) { if (_lstStructure == null) { - throw new FileFormatException("Structure is unavailable...", new NullReferenceException()); + throw new OpenizeException("Structure is unavailable...", new NullReferenceException()); } var position = _lstStructure.FindIndex(e => e.ElementId == elementId); @@ -859,7 +860,7 @@ public bool Remove(int elementId) { _lstStructure.Insert(position, backupElement); var errorMessage = ConstructMessage(ex, "Remove"); - throw new FileFormatException(errorMessage, ex); + throw new OpenizeException(errorMessage, ex); } } } diff --git a/Openize.Words.csproj b/Openize.Words.csproj index 4dca54f..b004091 100644 --- a/Openize.Words.csproj +++ b/Openize.Words.csproj @@ -2,6 +2,22 @@ netcoreapp3.1 + 24.6.0 + false + 24.6.0 + Openize + (C) Openize Pty Ltd 2024. All Rights Reserved. + Openize + Written in native C#, Openize.Words for .NET is a free, open-source SDK designed to easily create, load, and modify Microsoft Word documents with just a few lines of code. + Openize.Words + https://github.com/openize-words/Openize.Words-for-.NET/blob/main/LICENSE + true + https://www.openize.com + A C# library to work with word documents without requiring MS word be installed. + openxml document word-documents word-processing word-processing-file-api word-processing-document-api document-automation word-api word-document-api word-sdk word-document-sdk word-document-library word-processing-sdk word-processing-library open-source-word-sdk + Openize.Words for .NET + true + true