| EndProject | EndProject | ||||
| Global | Global | ||||
| GlobalSection(TeamFoundationVersionControl) = preSolution | GlobalSection(TeamFoundationVersionControl) = preSolution | ||||
| SccNumberOfProjects = 2 | |||||
| SccNumberOfProjects = 3 | |||||
| SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C} | SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C} | ||||
| SccTeamFoundationServer = https://tfs08.codeplex.com/ | SccTeamFoundationServer = https://tfs08.codeplex.com/ | ||||
| SccLocalPath0 = . | SccLocalPath0 = . | ||||
| SccProjectUniqueName1 = DocX\\DocX.csproj | SccProjectUniqueName1 = DocX\\DocX.csproj | ||||
| SccProjectName1 = DocX | SccProjectName1 = DocX | ||||
| SccLocalPath1 = DocX | SccLocalPath1 = DocX | ||||
| SccProjectUniqueName2 = ConsoleApplication1\\ConsoleApplication1.csproj | |||||
| SccProjectName2 = ConsoleApplication1 | |||||
| SccLocalPath2 = ConsoleApplication1 | |||||
| EndGlobalSection | EndGlobalSection | ||||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||
| Debug|Any CPU = Debug|Any CPU | Debug|Any CPU = Debug|Any CPU |
| { | { | ||||
| #region Namespaces | #region Namespaces | ||||
| static internal XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; | static internal XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; | ||||
| static internal XNamespace r = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; | |||||
| static internal XNamespace customPropertiesSchema = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"; | static internal XNamespace customPropertiesSchema = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"; | ||||
| static internal XNamespace customVTypesSchema = "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"; | static internal XNamespace customVTypesSchema = "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"; | ||||
| #endregion | #endregion | ||||
| return document; | return document; | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Set the content direction for all Paragraphs in this document. | |||||
| /// </summary> | |||||
| /// <param name="direction">(Left to Right) or (Right to Left)</param> | |||||
| /// <example> | |||||
| /// Set the direction for all content in a document to RightToLeft. | |||||
| /// <code> | |||||
| /// // Load a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Set the direction for all content in the document to RightToLeft. | |||||
| /// document.SetDirection(Direction.RightToLeft); | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public void SetDirection(Direction direction) | |||||
| { | |||||
| foreach (Paragraph p in Paragraphs) | |||||
| p.Direction = direction; | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Loads a document into a DocX object using a fully qualified or relative filename. | /// Loads a document into a DocX object using a fully qualified or relative filename. | ||||
| /// </summary> | /// </summary> | ||||
| return AddImage(stream as object); | return AddImage(stream as object); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Adds a hyperlink to a document and creates a Paragraph which uses it. | |||||
| /// </summary> | |||||
| /// <param name="text">The text as displayed by the hyperlink.</param> | |||||
| /// <param name="uri">The hyperlink itself.</param> | |||||
| /// <returns>Returns a hyperlink that can be inserted into a Paragraph.</returns> | |||||
| /// <example> | |||||
| /// Adds a hyperlink to a document and creates a Paragraph which uses it. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// // Add a hyperlink to this document. | |||||
| /// Hyperlink h = document.AddHyperlink("Google", new Uri("http://www.google.com")); | |||||
| /// | |||||
| /// // Add a new Paragraph to this document. | |||||
| /// Paragraph p = document.InsertParagraph(); | |||||
| /// p.Append("My favourite search engine is "); | |||||
| /// p.AppendHyperlink(h); | |||||
| /// p.Append(", I think it's great."); | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Hyperlink AddHyperlink(string text, Uri uri) | |||||
| { | |||||
| // Get the word\document.xml part | |||||
| PackagePart word_document = package.GetPart(new Uri("/word/document.xml", UriKind.Relative)); | |||||
| // Create a new image relationship | |||||
| PackageRelationship rel = word_document.CreateRelationship(uri, TargetMode.External, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"); | |||||
| string id = rel.Id; | |||||
| XElement i = new XElement | |||||
| ( | |||||
| XName.Get("hyperlink", DocX.w.NamespaceName), | |||||
| new XAttribute(r + "id", id), | |||||
| new XAttribute(w + "history", "1"), | |||||
| new XElement(XName.Get("r", DocX.w.NamespaceName), | |||||
| new XElement(XName.Get("rPr", DocX.w.NamespaceName), | |||||
| new XElement(XName.Get("rStyle", DocX.w.NamespaceName), | |||||
| new XAttribute(w + "val", "Hyperlink"))), | |||||
| new XElement(XName.Get("t", DocX.w.NamespaceName), text)) | |||||
| ); | |||||
| Hyperlink h = new Hyperlink(this, i); | |||||
| h.Text = text; | |||||
| h.Uri = uri; | |||||
| AddHyperlinkStyleIfNotPresent(); | |||||
| return h; | |||||
| } | |||||
| internal void AddHyperlinkStyleIfNotPresent() | |||||
| { | |||||
| Uri word_styles_Uri = new Uri("/word/styles.xml", UriKind.Relative); | |||||
| // If the internal document contains no /word/styles.xml create one. | |||||
| if (!package.PartExists(word_styles_Uri)) | |||||
| AddDefaultStylesXml(package); | |||||
| // Load the styles.xml into memory. | |||||
| XDocument word_styles; | |||||
| using (TextReader tr = new StreamReader(package.GetPart(word_styles_Uri).GetStream())) | |||||
| word_styles = XDocument.Load(tr); | |||||
| bool hyperlinkStyleExists = | |||||
| ( | |||||
| from s in word_styles.Element(w + "styles").Elements() | |||||
| let styleId = s.Attribute(XName.Get("styleId", w.NamespaceName)) | |||||
| where (styleId != null && styleId.Value == "Hyperlink") | |||||
| select s | |||||
| ).Count() > 0; | |||||
| if (!hyperlinkStyleExists) | |||||
| { | |||||
| XElement style = new XElement | |||||
| ( | |||||
| w + "style", | |||||
| new XAttribute(w + "type", "character"), | |||||
| new XAttribute(w + "styleId", "Hyperlink"), | |||||
| new XElement(w + "name", new XAttribute(w + "val", "Hyperlink")), | |||||
| new XElement(w + "basedOn", new XAttribute(w + "val", "DefaultParagraphFont")), | |||||
| new XElement(w + "uiPriority", new XAttribute(w + "val", "99")), | |||||
| new XElement(w + "unhideWhenUsed"), | |||||
| new XElement(w + "rsid", new XAttribute(w + "val", "0005416C")), | |||||
| new XElement | |||||
| ( | |||||
| w + "rPr", | |||||
| new XElement(w + "color", new XAttribute(w + "val", "0000FF"), new XAttribute(w + "themeColor", "hyperlink")), | |||||
| new XElement | |||||
| ( | |||||
| w + "u", | |||||
| new XAttribute(w + "val", "single") | |||||
| ) | |||||
| ) | |||||
| ); | |||||
| word_styles.Element(w + "styles").Add(style); | |||||
| // Save the styles document. | |||||
| using (TextWriter tw = new StreamWriter(package.GetPart(word_styles_Uri).GetStream())) | |||||
| word_styles.Save(tw); | |||||
| } | |||||
| } | |||||
| private string GetNextFreeRelationshipID() | |||||
| { | |||||
| // Get the word\document.xml part | |||||
| PackagePart word_document = package.GetPart(new Uri("/word/document.xml", UriKind.Relative)); | |||||
| string id = | |||||
| ( | |||||
| from r in word_document.GetRelationships() | |||||
| select r.Id | |||||
| ).Max(); | |||||
| // The convension for ids is rid01, rid02, etc | |||||
| string newId = id.Replace("rId", ""); | |||||
| int result; | |||||
| if (int.TryParse(newId, out result)) | |||||
| return ("rId" + (result + 1)); | |||||
| else | |||||
| return Guid.NewGuid().ToString(); | |||||
| } | |||||
| internal Image AddImage(object o) | internal Image AddImage(object o) | ||||
| { | { | ||||
| // Open a Stream to the new image being added. | // Open a Stream to the new image being added. |
| <SccProvider>SAK</SccProvider> | <SccProvider>SAK</SccProvider> | ||||
| <StartupObject> | <StartupObject> | ||||
| </StartupObject> | </StartupObject> | ||||
| <SignAssembly>true</SignAssembly> | |||||
| <AssemblyOriginatorKeyFile> | |||||
| </AssemblyOriginatorKeyFile> | |||||
| <SignAssembly>false</SignAssembly> | |||||
| <AssemblyOriginatorKeyFile>StrongNameKey.pfx</AssemblyOriginatorKeyFile> | |||||
| </PropertyGroup> | </PropertyGroup> | ||||
| <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||||
| <DebugSymbols>true</DebugSymbols> | <DebugSymbols>true</DebugSymbols> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <Compile Include="CustomProperty.cs" /> | <Compile Include="CustomProperty.cs" /> | ||||
| <Compile Include="DocProperty.cs" /> | <Compile Include="DocProperty.cs" /> | ||||
| <Compile Include="Hyperlink.cs" /> | |||||
| <Compile Include="_BaseClasses.cs" /> | <Compile Include="_BaseClasses.cs" /> | ||||
| <Compile Include="Table.cs" /> | <Compile Include="Table.cs" /> | ||||
| <Compile Include="_Enumerations.cs" /> | <Compile Include="_Enumerations.cs" /> | ||||
| </Compile> | </Compile> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <None Include="Help\DocX v1.0.0.8 - Documentation.chm" /> | |||||
| <None Include="Help\Changes in this version 1.0.0.9.docx" /> | |||||
| <None Include="Help\DocX v1.0.0.9 - Documentation.chm" /> | |||||
| <None Include="Help\Read Me.docx" /> | |||||
| <None Include="StrongNameKey.pfx" /> | <None Include="StrongNameKey.pfx" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> |
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Xml.Linq; | |||||
| namespace Novacode | |||||
| { | |||||
| public class Hyperlink: DocXElement | |||||
| { | |||||
| private string text; | |||||
| public string Text { get{return text;} set{text = value;} } | |||||
| private Uri uri; | |||||
| public Uri Uri { get{return uri;} set{uri = value;} } | |||||
| internal Hyperlink(DocX document, XElement i): base(document, i) | |||||
| { | |||||
| } | |||||
| } | |||||
| } |
| /// A unique id which identifies this Image. | /// A unique id which identifies this Image. | ||||
| /// </summary> | /// </summary> | ||||
| private string id; | private string id; | ||||
| private DocX document; | |||||
| /// <summary> | /// <summary> | ||||
| /// Returns the id of this Image. | /// Returns the id of this Image. | ||||
| internal Image(DocX document, PackageRelationship pr) | internal Image(DocX document, PackageRelationship pr) | ||||
| { | { | ||||
| id = pr.Id; | |||||
| this.document = document; | |||||
| this.id = pr.Id; | |||||
| } | |||||
| /// <summary> | |||||
| /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append. | |||||
| /// </summary> | |||||
| /// <returns></returns> | |||||
| /// <example> | |||||
| /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append. | |||||
| /// <code> | |||||
| /// using (DocX document = DocX.Create("Test.docx")) | |||||
| /// { | |||||
| /// // Add an image to the document. | |||||
| /// Image i = document.AddImage(@"Image.jpg"); | |||||
| /// | |||||
| /// // Create a picture i.e. (A custom view of an image) | |||||
| /// Picture p = i.CreatePicture(); | |||||
| /// p.FlipHorizontal = true; | |||||
| /// p.Rotation = 10; | |||||
| /// | |||||
| /// // Create a new Paragraph. | |||||
| /// Paragraph par = document.InsertParagraph(); | |||||
| /// | |||||
| /// // Append content to the Paragraph. | |||||
| /// par.Append("Here is a cool picture") | |||||
| /// .AppendPicture(p) | |||||
| /// .Append(" don't you think so?"); | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Picture CreatePicture() | |||||
| { | |||||
| return Paragraph.CreatePicture(document, id, string.Empty, string.Empty); | |||||
| } | } | ||||
| } | } | ||||
| } | } |
| return base.InsertTableBeforeSelf(t); | return base.InsertTableBeforeSelf(t); | ||||
| } | } | ||||
| private Direction direction; | |||||
| /// <summary> | |||||
| /// Gets or Sets the Direction of content in this Paragraph. | |||||
| /// <example> | |||||
| /// Create a Paragraph with content that flows right to left. Default is left to right. | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create("Test.docx")) | |||||
| /// { | |||||
| /// // Create a new Paragraph with the text "Hello World". | |||||
| /// Paragraph p = document.InsertParagraph("Hello World."); | |||||
| /// | |||||
| /// // Make this Paragraph flow right to left. Default is left to right. | |||||
| /// p.Direction = Direction.RightToLeft; | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| /// </summary> | |||||
| public Direction Direction | |||||
| { | |||||
| get { return Direction; } | |||||
| set | |||||
| { | |||||
| direction = value; | |||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| XElement bidi = pPr.Element(XName.Get("bidi", DocX.w.NamespaceName)); | |||||
| if (direction == Direction.RightToLeft) | |||||
| { | |||||
| if(bidi == null) | |||||
| pPr.Add(new XElement(XName.Get("bidi", DocX.w.NamespaceName))); | |||||
| } | |||||
| else | |||||
| { | |||||
| if (bidi != null) | |||||
| bidi.Remove(); | |||||
| } | |||||
| } | |||||
| } | |||||
| /// <summary> | |||||
| /// If the pPr element doesent exist it is created, either way it is returned by this function. | |||||
| /// </summary> | |||||
| /// <returns>The pPr element for this Paragraph.</returns> | |||||
| internal XElement GetOrCreate_pPr() | |||||
| { | |||||
| // Get the element. | |||||
| XElement pPr = Xml.Element(XName.Get("pPr", DocX.w.NamespaceName)); | |||||
| // If it dosen't exist, create it. | |||||
| if (pPr == null) | |||||
| { | |||||
| Xml.AddFirst(new XElement(XName.Get("pPr", DocX.w.NamespaceName))); | |||||
| pPr = Xml.Element(XName.Get("pPr", DocX.w.NamespaceName)); | |||||
| } | |||||
| // Return the pPr element for this Paragraph. | |||||
| return pPr; | |||||
| } | |||||
| /// <summary> | |||||
| /// If the ind element doesent exist it is created, either way it is returned by this function. | |||||
| /// </summary> | |||||
| /// <returns>The ind element for this Paragraphs pPr.</returns> | |||||
| internal XElement GetOrCreate_pPr_ind() | |||||
| { | |||||
| // Get the element. | |||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| XElement ind = pPr.Element(XName.Get("ind", DocX.w.NamespaceName)); | |||||
| // If it dosen't exist, create it. | |||||
| if (ind == null) | |||||
| { | |||||
| pPr.Add(new XElement(XName.Get("ind", DocX.w.NamespaceName))); | |||||
| ind = pPr.Element(XName.Get("ind", DocX.w.NamespaceName)); | |||||
| } | |||||
| // Return the pPr element for this Paragraph. | |||||
| return ind; | |||||
| } | |||||
| private float indentationFirstLine; | |||||
| /// <summary> | |||||
| /// Get or set the indentation of the first line of this Paragraph. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Indent only the first line of a Paragraph. | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create("Test.docx")) | |||||
| /// { | |||||
| /// // Create a new Paragraph. | |||||
| /// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3"); | |||||
| /// | |||||
| /// // Indent only the first line of the Paragraph. | |||||
| /// p.IndentationFirstLine = 2.0f; | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public float IndentationFirstLine | |||||
| { | |||||
| get | |||||
| { | |||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| XElement ind = GetOrCreate_pPr_ind(); | |||||
| XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName)); | |||||
| if (firstLine != null) | |||||
| return float.Parse(firstLine.Value); | |||||
| return 0.0f; | |||||
| } | |||||
| set | |||||
| { | |||||
| if (IndentationFirstLine != value) | |||||
| { | |||||
| indentationFirstLine = value; | |||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| XElement ind = GetOrCreate_pPr_ind(); | |||||
| // Paragraph can either be firstLine or hanging (Remove hanging). | |||||
| XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName)); | |||||
| if (hanging != null) | |||||
| hanging.Remove(); | |||||
| string indentation = ((indentationFirstLine / 0.1) * 57).ToString(); | |||||
| XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName)); | |||||
| if (firstLine != null) | |||||
| firstLine.Value = indentation; | |||||
| else | |||||
| ind.Add(new XAttribute(XName.Get("firstLine", DocX.w.NamespaceName), indentation)); | |||||
| } | |||||
| } | |||||
| } | |||||
| private float indentationHanging; | |||||
| /// <summary> | |||||
| /// Get or set the indentation of all but the first line of this Paragraph. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Indent all but the first line of a Paragraph. | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create("Test.docx")) | |||||
| /// { | |||||
| /// // Create a new Paragraph. | |||||
| /// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3"); | |||||
| /// | |||||
| /// // Indent all but the first line of the Paragraph. | |||||
| /// p.IndentationHanging = 1.0f; | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public float IndentationHanging | |||||
| { | |||||
| get | |||||
| { | |||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| XElement ind = GetOrCreate_pPr_ind(); | |||||
| XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName)); | |||||
| if (hanging != null) | |||||
| return float.Parse(hanging.Value); | |||||
| return 0.0f; | |||||
| } | |||||
| set | |||||
| { | |||||
| if (IndentationHanging != value) | |||||
| { | |||||
| indentationHanging = value; | |||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| XElement ind = GetOrCreate_pPr_ind(); | |||||
| // Paragraph can either be firstLine or hanging (Remove firstLine). | |||||
| XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName)); | |||||
| if (firstLine != null) | |||||
| firstLine.Remove(); | |||||
| string indentation = ((indentationHanging / 0.1) * 57).ToString(); | |||||
| XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName)); | |||||
| if (hanging != null) | |||||
| hanging.Value = indentation; | |||||
| else | |||||
| ind.Add(new XAttribute(XName.Get("hanging", DocX.w.NamespaceName), indentation)); | |||||
| } | |||||
| } | |||||
| } | |||||
| private float indentationBefore; | |||||
| /// <summary> | |||||
| /// Set the before indentation in cm for this Paragraph. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// // Indent an entire Paragraph from the left. | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create("Test.docx")) | |||||
| /// { | |||||
| /// // Create a new Paragraph. | |||||
| /// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3"); | |||||
| /// | |||||
| /// // Indent this entire Paragraph from the left. | |||||
| /// p.IndentationBefore = 2.0f; | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| ///} | |||||
| /// </code> | |||||
| /// </example> | |||||
| public float IndentationBefore | |||||
| { | |||||
| get | |||||
| { | |||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| XElement ind = GetOrCreate_pPr_ind(); | |||||
| XAttribute left = ind.Attribute(XName.Get("left", DocX.w.NamespaceName)); | |||||
| if (left != null) | |||||
| return float.Parse(left.Value); | |||||
| return 0.0f; | |||||
| } | |||||
| set | |||||
| { | |||||
| if (IndentationBefore != value) | |||||
| { | |||||
| indentationBefore = value; | |||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| XElement ind = GetOrCreate_pPr_ind(); | |||||
| string indentation = ((indentationBefore / 0.1) * 57).ToString(); | |||||
| XAttribute left = ind.Attribute(XName.Get("left", DocX.w.NamespaceName)); | |||||
| if(left != null) | |||||
| left.Value = indentation; | |||||
| else | |||||
| ind.Add(new XAttribute(XName.Get("left", DocX.w.NamespaceName), indentation)); | |||||
| } | |||||
| } | |||||
| } | |||||
| private float indentationAfter = 0.0f; | |||||
| /// <summary> | |||||
| /// Set the after indentation in cm for this Paragraph. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// // Indent an entire Paragraph from the right. | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create("Test.docx")) | |||||
| /// { | |||||
| /// // Create a new Paragraph. | |||||
| /// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3"); | |||||
| /// | |||||
| /// // Make the content of this Paragraph flow right to left. | |||||
| /// p.Direction = Direction.RightToLeft; | |||||
| /// | |||||
| /// // Indent this entire Paragraph from the right. | |||||
| /// p.IndentationAfter = 2.0f; | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public float IndentationAfter | |||||
| { | |||||
| get | |||||
| { | |||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| XElement ind = GetOrCreate_pPr_ind(); | |||||
| XAttribute right = ind.Attribute(XName.Get("right", DocX.w.NamespaceName)); | |||||
| if (right != null) | |||||
| return float.Parse(right.Value); | |||||
| return 0.0f; | |||||
| } | |||||
| set | |||||
| { | |||||
| if (IndentationAfter != value) | |||||
| { | |||||
| indentationAfter = value; | |||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| XElement ind = GetOrCreate_pPr_ind(); | |||||
| string indentation = ((indentationAfter / 0.1) * 57).ToString(); | |||||
| XAttribute right = ind.Attribute(XName.Get("right", DocX.w.NamespaceName)); | |||||
| if (right != null) | |||||
| right.Value = indentation; | |||||
| else | |||||
| ind.Add(new XAttribute(XName.Get("right", DocX.w.NamespaceName), indentation)); | |||||
| } | |||||
| } | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Insert a new Table into this document before this Paragraph. | /// Insert a new Table into this document before this Paragraph. | ||||
| /// </summary> | /// </summary> | ||||
| { | { | ||||
| alignment = value; | alignment = value; | ||||
| XElement pPr = Xml.Element(XName.Get("pPr", DocX.w.NamespaceName)); | |||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| XElement jc = pPr.Element(XName.Get("jc", DocX.w.NamespaceName)); | |||||
| if (alignment != Novacode.Alignment.left) | if (alignment != Novacode.Alignment.left) | ||||
| { | { | ||||
| if (pPr == null) | |||||
| Xml.Add(new XElement(XName.Get("pPr", DocX.w.NamespaceName))); | |||||
| pPr = Xml.Element(XName.Get("pPr", DocX.w.NamespaceName)); | |||||
| XElement jc = pPr.Element(XName.Get("jc", DocX.w.NamespaceName)); | |||||
| if(jc == null) | if(jc == null) | ||||
| pPr.Add(new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignment.ToString()))); | pPr.Add(new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignment.ToString()))); | ||||
| else | else | ||||
| else | else | ||||
| { | { | ||||
| if (pPr != null) | |||||
| { | |||||
| XElement jc = pPr.Element(XName.Get("jc", DocX.w.NamespaceName)); | |||||
| if (jc != null) | if (jc != null) | ||||
| jc.Remove(); | jc.Remove(); | ||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /// <param name="id">A unique id that identifies an Image embedded in this document.</param> | /// <param name="id">A unique id that identifies an Image embedded in this document.</param> | ||||
| /// <param name="name">The name of this Picture.</param> | /// <param name="name">The name of this Picture.</param> | ||||
| /// <param name="descr">The description of this Picture.</param> | /// <param name="descr">The description of this Picture.</param> | ||||
| internal Picture CreatePicture(DocX document, string id, string name, string descr) | |||||
| static internal Picture CreatePicture(DocX document, string id, string name, string descr) | |||||
| { | { | ||||
| PackagePart word_document = document.package.GetPart(new Uri("/word/document.xml", UriKind.Relative)); | PackagePart word_document = document.package.GetPart(new Uri("/word/document.xml", UriKind.Relative)); | ||||
| PackagePart part = document.package.GetPart(word_document.GetRelationship(id).TargetUri); | PackagePart part = document.package.GetPart(word_document.GetRelationship(id).TargetUri); | ||||
| </drawing> | </drawing> | ||||
| ", cx, cy, id, name, descr)); | ", cx, cy, id, name, descr)); | ||||
| return new Picture(Document, xml); | |||||
| return new Picture(document, xml); | |||||
| } | } | ||||
| public Picture InsertPicture(int index, string imageID) | public Picture InsertPicture(int index, string imageID) | ||||
| return this; | return this; | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Append a hyperlink to a Paragraph. | |||||
| /// </summary> | |||||
| /// <param name="h">The hyperlink to append.</param> | |||||
| /// <returns>The Paragraph with the hyperlink appended.</returns> | |||||
| /// <example> | |||||
| /// Creates a Paragraph with some text and a hyperlink. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// // Add a hyperlink to this document. | |||||
| /// Hyperlink h = document.AddHyperlink("Google", new Uri("http://www.google.com")); | |||||
| /// | |||||
| /// // Add a new Paragraph to this document. | |||||
| /// Paragraph p = document.InsertParagraph(); | |||||
| /// p.Append("My favourite search engine is "); | |||||
| /// p.AppendHyperlink(h); | |||||
| /// p.Append(", I think it's great."); | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph AppendHyperlink(Hyperlink h) | |||||
| { | |||||
| Xml.Add(h.Xml); | |||||
| this.runs = Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).Reverse().Take(h.Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).Count()).ToList(); | |||||
| BuildRunLookup(Xml); | |||||
| return this; | |||||
| } | |||||
| /// <summary> | |||||
| /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append. | |||||
| /// </summary> | |||||
| /// <param name="p">The Picture to append.</param> | |||||
| /// <returns>The Paragraph with the Picture now appended.</returns> | |||||
| /// <example> | |||||
| /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append. | |||||
| /// <code> | |||||
| /// using (DocX document = DocX.Create("Test.docx")) | |||||
| /// { | |||||
| /// // Add an image to the document. | |||||
| /// Image i = document.AddImage(@"Image.jpg"); | |||||
| /// | |||||
| /// // Create a picture i.e. (A custom view of an image) | |||||
| /// Picture p = i.CreatePicture(); | |||||
| /// p.FlipHorizontal = true; | |||||
| /// p.Rotation = 10; | |||||
| /// | |||||
| /// // Create a new Paragraph. | |||||
| /// Paragraph par = document.InsertParagraph(); | |||||
| /// | |||||
| /// // Append content to the Paragraph. | |||||
| /// par.Append("Here is a cool picture") | |||||
| /// .AppendPicture(p) | |||||
| /// .Append(" don't you think so?"); | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph AppendPicture(Picture p) | |||||
| { | |||||
| Xml.Add(p.Xml); | |||||
| this.runs = Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).Reverse().Take(p.Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).Count()).ToList(); | |||||
| BuildRunLookup(Xml); | |||||
| return this; | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Append text on a new line to this Paragraph. | /// Append text on a new line to this Paragraph. | ||||
| /// </summary> | /// </summary> |
| select a.Value | select a.Value | ||||
| ).First(); | ).First(); | ||||
| this.descr = | this.descr = | ||||
| ( | ( | ||||
| from e in Xml.Descendants() | from e in Xml.Descendants() |
| // You can specify all the values or you can default the Build and Revision Numbers | // You can specify all the values or you can default the Build and Revision Numbers | ||||
| // by using the '*' as shown below: | // by using the '*' as shown below: | ||||
| // [assembly: AssemblyVersion("1.0.*")] | // [assembly: AssemblyVersion("1.0.*")] | ||||
| [assembly: AssemblyVersion("1.0.0.8")] | |||||
| [assembly: AssemblyFileVersion("1.0.0.8")] | |||||
| [assembly: AssemblyVersion("1.0.0.9")] | |||||
| [assembly: AssemblyFileVersion("1.0.0.9")] |
| private List<Row> rows; | private List<Row> rows; | ||||
| private int rowCount, columnCount; | private int rowCount, columnCount; | ||||
| /// <summary> | |||||
| /// Set the direction of all content in this Table. | |||||
| /// </summary> | |||||
| /// <param name="direction">(Left to Right) or (Right to Left)</param> | |||||
| /// <example> | |||||
| /// Set the content direction for all content in a table to RightToLeft. | |||||
| /// <code> | |||||
| /// // Load a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first table in a document. | |||||
| /// Table table = document.Tables[0]; | |||||
| /// | |||||
| /// // Set the content direction for all content in this table to RightToLeft. | |||||
| /// table.SetDirection(Direction.RightToLeft); | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public void SetDirection(Direction direction) | |||||
| { | |||||
| foreach (Row r in Rows) | |||||
| r.SetDirection(direction); | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Returns the number of rows in this table. | /// Returns the number of rows in this table. | ||||
| /// </summary> | /// </summary> | ||||
| select new Cell(document, c)).ToList(); | select new Cell(document, c)).ToList(); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Set the content direction of a single Row in a Table. | |||||
| /// </summary> | |||||
| /// <param name="direction">The direction either (LeftToRight or RightToLeft).</param> | |||||
| /// <example> | |||||
| /// Set the content direction of a single Row in a Table. | |||||
| /// <code> | |||||
| /// // Load a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first Table from a document. | |||||
| /// Table t = document.Tables[0]; | |||||
| /// | |||||
| /// // Get the first row from this Table. | |||||
| /// Row r = t.Rows[0]; | |||||
| /// | |||||
| /// // Set the content direction of this Row to RightToLeft. | |||||
| /// r.SetDirection(Direction.RightToLeft); | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| ///} | |||||
| /// </code> | |||||
| /// </example> | |||||
| public void SetDirection(Direction direction) | |||||
| { | |||||
| foreach (Cell c in Cells) | |||||
| c.SetDirection(direction); | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Height in pixels. // Added by Joel, refactored by Cathal. | /// Height in pixels. // Added by Joel, refactored by Cathal. | ||||
| /// </summary> | /// </summary> | ||||
| paragraphs = xml.Elements(XName.Get("p", DocX.w.NamespaceName)).Select(p => new Paragraph(document, p, 0)).ToList(); | paragraphs = xml.Elements(XName.Get("p", DocX.w.NamespaceName)).Select(p => new Paragraph(document, p, 0)).ToList(); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Set the content direction of a single Cell in a Table. | |||||
| /// </summary> | |||||
| /// <param name="direction">The direction either (LeftToRight or RightToLeft).</param> | |||||
| /// <example> | |||||
| /// Set the content direction of a single Cell in a Table. | |||||
| /// <code> | |||||
| /// // Load a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first Table from a document. | |||||
| /// Table t = document.Tables[0]; | |||||
| /// | |||||
| /// // Get the first row from this Table. | |||||
| /// Row r = t.Rows[0]; | |||||
| /// | |||||
| /// // Get the first cell from this Row. | |||||
| /// Cell c = r.Cells[1]; | |||||
| /// | |||||
| /// // Set the content direction of this Cell to RightToLeft. | |||||
| /// c.SetDirection(Direction.RightToLeft); | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| ///} | |||||
| /// </code> | |||||
| /// </example> | |||||
| public void SetDirection(Direction direction) | |||||
| { | |||||
| foreach (Paragraph p in Paragraphs) | |||||
| p.Direction = direction; | |||||
| } | |||||
| public Color Shading | public Color Shading | ||||
| { | { | ||||
| get | get |
| public enum Alignment | public enum Alignment | ||||
| { | { | ||||
| /// <summary> | /// <summary> | ||||
| /// Align text to the left. | |||||
| /// Align Paragraph to the left. | |||||
| /// </summary> | /// </summary> | ||||
| left, | left, | ||||
| /// <summary> | /// <summary> | ||||
| /// Center text. | |||||
| /// Align Paragraph as Centered. | |||||
| /// </summary> | /// </summary> | ||||
| center, | center, | ||||
| /// <summary> | /// <summary> | ||||
| /// Align text to the right. | |||||
| /// Align Paragraph to the right. | |||||
| /// </summary> | /// </summary> | ||||
| right, | right, | ||||
| /// <summary> | /// <summary> | ||||
| /// Align text to both the left and right margins, adding extra space between words as necessary. | |||||
| /// (Justified) Align Paragraph to both the left and right margins, adding extra space between content as necessary. | |||||
| /// </summary> | /// </summary> | ||||
| both | both | ||||
| }; | }; | ||||
| public enum Direction | |||||
| { | |||||
| LeftToRight, | |||||
| RightToLeft | |||||
| }; | |||||
| /// <summary> | /// <summary> | ||||
| /// Paragraph edit types | /// Paragraph edit types | ||||
| /// </summary> | /// </summary> |