static private DecompressXMLResource ( string manifest_resource_name ) : System.Xml.Linq.XDocument | ||
manifest_resource_name | string | |
return | System.Xml.Linq.XDocument |
/// <summary> /// If this document does not contain a /word/styles.xml add the default one generated by Microsoft Word. /// </summary> /// <param name="package"></param> /// <param name="mainDocumentPart"></param> /// <returns></returns> internal static XDocument AddDefaultStylesXml(Package package) { XDocument stylesDoc; // Create the main document part for this package PackagePart word_styles = package.CreatePart(new Uri("/word/styles.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", CompressionOption.Maximum); stylesDoc = HelperFunctions.DecompressXMLResource("Novacode.Resources.default_styles.xml.gz"); XElement lang = stylesDoc.Root.Element(XName.Get("docDefaults", DocX.w.NamespaceName)).Element(XName.Get("rPrDefault", DocX.w.NamespaceName)).Element(XName.Get("rPr", DocX.w.NamespaceName)).Element(XName.Get("lang", DocX.w.NamespaceName)); lang.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), CultureInfo.CurrentCulture); // Save /word/styles.xml using (TextWriter tw = new StreamWriter(word_styles.GetStream(FileMode.Create, FileAccess.Write))) stylesDoc.Save(tw, SaveOptions.None); PackagePart mainDocumentPart = package.GetParts().Where ( p => p.ContentType.Equals ( "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", StringComparison.CurrentCultureIgnoreCase ) ).Single(); mainDocumentPart.CreateRelationship(word_styles.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"); return(stylesDoc); }
internal void CreateNewNumberingNumId(int level = 0, ListItemType listType = ListItemType.Numbered) { ValidateDocXNumberingPartExists(); if (Document.numbering.Root == null) { throw new InvalidOperationException("Numbering section did not instantiate properly."); } ListType = listType; var numId = GetMaxNumId() + 1; var abstractNumId = GetMaxAbstractNumId() + 1; XDocument listTemplate; switch (listType) { case ListItemType.Bulleted: listTemplate = HelperFunctions.DecompressXMLResource("Novacode.Resources.numbering.default_bullet_abstract.xml.gz"); break; case ListItemType.Numbered: listTemplate = HelperFunctions.DecompressXMLResource("Novacode.Resources.numbering.default_decimal_abstract.xml.gz"); break; default: throw new InvalidOperationException(string.Format("Unable to deal with ListItemType: {0}.", listType.ToString())); } var abstractNumTemplate = listTemplate.Descendants().Single(d => d.Name.LocalName == "abstractNum"); abstractNumTemplate.SetAttributeValue(DocX.w + "abstractNumId", abstractNumId); var abstractNumXml = new XElement(XName.Get("num", DocX.w.NamespaceName), new XAttribute(DocX.w + "numId", numId), new XElement(XName.Get("abstractNumId", DocX.w.NamespaceName), new XAttribute(DocX.w + "val", abstractNumId))); var abstractNumNode = Document.numbering.Root.Descendants().LastOrDefault(xElement => xElement.Name.LocalName == "abstractNum"); var numXml = Document.numbering.Root.Descendants().LastOrDefault(xElement => xElement.Name.LocalName == "num"); if (abstractNumNode == null || numXml == null) { Document.numbering.Root.Add(abstractNumTemplate); Document.numbering.Root.Add(abstractNumXml); } else { abstractNumNode.AddAfterSelf(abstractNumTemplate); numXml.AddAfterSelf( abstractNumXml ); } if (!Document.mainPart.GetRelationshipsByType(NumberingRelationshipType).Any(r => r.TargetUri == NumberingUri)) { Document.mainPart.CreateRelationship(NumberingUri, TargetMode.Internal, NumberingRelationshipType); } NumId = numId; }
internal void CreateNewNumberingNumId(int level = 0, ListItemType listType = ListItemType.Numbered, int?startNumber = null, bool continueNumbering = false) { ValidateDocXNumberingPartExists(); if (Document.numbering.Root == null) { throw new InvalidOperationException("Numbering section did not instantiate properly."); } ListType = listType; var numId = GetMaxNumId() + 1; var abstractNumId = GetMaxAbstractNumId() + 1; XDocument listTemplate; switch (listType) { case ListItemType.Bulleted: listTemplate = HelperFunctions.DecompressXMLResource("Novacode.Resources.numbering.default_bullet_abstract.xml.gz"); break; case ListItemType.Numbered: listTemplate = HelperFunctions.DecompressXMLResource("Novacode.Resources.numbering.default_decimal_abstract.xml.gz"); break; default: throw new InvalidOperationException(string.Format("Unable to deal with ListItemType: {0}.", listType.ToString())); } var abstractNumTemplate = listTemplate.Descendants().Single(d => d.Name.LocalName == "abstractNum"); abstractNumTemplate.SetAttributeValue(DocX.w + "abstractNumId", abstractNumId); //Fixing an issue where numbering would continue from previous numbered lists. Setting startOverride assures that a numbered list starts on the provided number. //The override needs only be on level 0 as this will cascade to the rest of the list. var abstractNumXml = GetAbstractNumXml(abstractNumId, numId, startNumber, continueNumbering); var abstractNumNode = Document.numbering.Root.Descendants().LastOrDefault(xElement => xElement.Name.LocalName == "abstractNum"); var numXml = Document.numbering.Root.Descendants().LastOrDefault(xElement => xElement.Name.LocalName == "num"); if (abstractNumNode == null || numXml == null) { Document.numbering.Root.Add(abstractNumTemplate); Document.numbering.Root.Add(abstractNumXml); } else { abstractNumNode.AddAfterSelf(abstractNumTemplate); numXml.AddAfterSelf( abstractNumXml ); } NumId = numId; }
internal void CreateNewNumberingNumId(int level = 0, ListItemType listType = ListItemType.Numbered, int?startNumber = default(int?), bool continueNumbering = false) { ValidateDocXNumberingPartExists(); if (base.Document.numbering.Root == null) { throw new InvalidOperationException("Numbering section did not instantiate properly."); } ListType = listType; int numId = GetMaxNumId() + 1; int num = GetMaxAbstractNumId() + 1; XDocument xDocument; switch (listType) { case ListItemType.Bulleted: xDocument = HelperFunctions.DecompressXMLResource("Novacode.Resources.numbering.default_bullet_abstract.xml.gz"); break; case ListItemType.Numbered: xDocument = HelperFunctions.DecompressXMLResource("Novacode.Resources.numbering.default_decimal_abstract.xml.gz"); break; default: throw new InvalidOperationException($"Unable to deal with ListItemType: {listType.ToString()}."); } XElement xElement2 = xDocument.Descendants().Single((XElement d) => d.Name.LocalName == "abstractNum"); xElement2.SetAttributeValue(DocX.w + "abstractNumId", num); XElement abstractNumXml = GetAbstractNumXml(num, numId, startNumber, continueNumbering); XElement xElement3 = base.Document.numbering.Root.Descendants().LastOrDefault((XElement xElement) => xElement.Name.LocalName == "abstractNum"); XElement xElement4 = base.Document.numbering.Root.Descendants().LastOrDefault((XElement xElement) => xElement.Name.LocalName == "num"); if (xElement3 == null || xElement4 == null) { base.Document.numbering.Root.Add(xElement2); base.Document.numbering.Root.Add(abstractNumXml); } else { xElement3.AddAfterSelf(xElement2); xElement4.AddAfterSelf(abstractNumXml); } NumId = numId; }