public static Styles CreateStyles(MainDocumentPart mainPart) { StyleDefinitionsPart styleDefinitionsPart = mainPart.AddNewPart<StyleDefinitionsPart>(); Styles styles = new Styles(); styleDefinitionsPart.Styles = styles; //styles.Save(); return styles; }
internal static void ClearHeaderFooter(MainDocumentPart mdp) { mdp.DeleteParts(mdp.HeaderParts); mdp.DeleteParts(mdp.FooterParts); var hp = mdp.AddNewPart<HeaderPart>(); var fp = mdp.AddNewPart<FooterPart>(); hp.Header = new Header(); fp.Footer = new Footer(); foreach (var sps in mdp.Document.Body.Elements<SectionProperties>()) { sps.RemoveAllChildren<HeaderReference>(); sps.RemoveAllChildren<FooterReference>(); sps.PrependChild(new HeaderReference { Id = mdp.GetIdOfPart(hp) }); sps.PrependChild(new FooterReference { Id = mdp.GetIdOfPart(fp) }); } mdp.Document.Save(); }
private NumberingDefinitionsPart FindOrCreateNumberingDefinitionPart(MainDocumentPart mainDocumentPart) { NumberingDefinitionsPart result = null; var existingDefinitions = mainDocumentPart.GetPartsOfType<NumberingDefinitionsPart>(); foreach (NumberingDefinitionsPart existingDefinition in existingDefinitions) { result = existingDefinition; } if (result == null) result = mainDocumentPart.AddNewPart<NumberingDefinitionsPart>(); return result; }
public static Run AddImageParagraph(MainDocumentPart mainpart, string filename, string SigId) { SigId = SigId.Replace("/", ""); byte[] imageFileBytes; Bitmap image = null; // Open a stream on the image file and read it's contents. using (FileStream fsImageFile = System.IO.File.OpenRead(filename)) { imageFileBytes = new byte[fsImageFile.Length]; fsImageFile.Read(imageFileBytes, 0, imageFileBytes.Length); image = new Bitmap(fsImageFile); } string extension = Path.GetExtension(filename); string ExtensionText = "image/jpeg"; if (extension == "png") ExtensionText = "image/png"; double MaxImageWidth = 0.2; double imgWidth = image.Width; double imgHeight = image.Height; double itmp = imgWidth / MaxImageWidth; double HorResolution = 1;//image.HorizontalResolution; double verResolution = 1;//image.VerticalResolution; double emuWidth = imgWidth / HorResolution / itmp; double emuHeight = imgHeight / verResolution / itmp; long imageWidthEMU = (long)((emuWidth) * 914400L); long imageHeightEMU = (long)((emuHeight) * 914400L); // add this is not already there try { if (mainpart.GetPartById(SigId) == null) { var imagePart = mainpart.AddNewPart<ImagePart>(ExtensionText, SigId); using (BinaryWriter writer = new BinaryWriter(imagePart.GetStream())) { writer.Write(imageFileBytes); writer.Flush(); } } } catch // thrown if not found { var imagePart = mainpart.AddNewPart<ImagePart>(ExtensionText, SigId); using (BinaryWriter writer = new BinaryWriter(imagePart.GetStream())) { writer.Write(imageFileBytes); writer.Flush(); } } WP.Run para = new WP.Run( new WP.Drawing( new wp.Inline( new wp.Extent() { Cx = imageWidthEMU, Cy = imageHeightEMU }, new wp.EffectExtent() { LeftEdge = 19050L, TopEdge = 0L, RightEdge = 9525L, BottomEdge = 0L }, new wp.DocProperties() { Id = (UInt32Value)1U, Name = "Inline Text Wrapping Picture", Description = GraphicDataUri }, new wp.NonVisualGraphicFrameDrawingProperties( new a.GraphicFrameLocks() { NoChangeAspect = true }), new a.Graphic( new a.GraphicData( new pic.Picture( new pic.NonVisualPictureProperties( new pic.NonVisualDrawingProperties() { Id = (UInt32Value)0U, Name = filename }, new pic.NonVisualPictureDrawingProperties()), new pic.BlipFill( new a.Blip() { Embed = SigId }, new a.Stretch( new a.FillRectangle())), new pic.ShapeProperties( new a.Transform2D( new a.Offset() { X = 0L, Y = 0L }, new a.Extents() { Cx = imageWidthEMU, Cy = imageHeightEMU }), new a.PresetGeometry( new a.AdjustValueList() ) { Preset = a.ShapeTypeValues.Rectangle })) ) { Uri = GraphicDataUri }) ) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U })); return para; }
private static void InitEmptyHeaderOrFooter(MainDocumentPart mainDocPart, XElement sect, XName referenceXName, string toType) { XDocument xDoc = null; if (referenceXName == W.headerReference) { xDoc = XDocument.Parse( @"<?xml version='1.0' encoding='utf-8' standalone='yes'?> <w:hdr xmlns:wpc='http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas' xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:r='http://schemas.openxmlformats.org/officeDocument/2006/relationships' xmlns:m='http://schemas.openxmlformats.org/officeDocument/2006/math' xmlns:v='urn:schemas-microsoft-com:vml' xmlns:wp14='http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing' xmlns:wp='http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing' xmlns:w10='urn:schemas-microsoft-com:office:word' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' xmlns:w14='http://schemas.microsoft.com/office/word/2010/wordml' xmlns:w15='http://schemas.microsoft.com/office/word/2012/wordml' xmlns:wpg='http://schemas.microsoft.com/office/word/2010/wordprocessingGroup' xmlns:wpi='http://schemas.microsoft.com/office/word/2010/wordprocessingInk' xmlns:wne='http://schemas.microsoft.com/office/word/2006/wordml' xmlns:wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape' mc:Ignorable='w14 w15 wp14'> <w:p> <w:pPr> <w:pStyle w:val='Header' /> </w:pPr> <w:r> <w:t></w:t> </w:r> </w:p> </w:hdr>"); var newHeaderPart = mainDocPart.AddNewPart<HeaderPart>(); newHeaderPart.PutXDocument(xDoc); var referenceToAdd = new XElement(W.headerReference, new XAttribute(W.type, toType), new XAttribute(R.id, mainDocPart.GetIdOfPart(newHeaderPart))); sect.AddFirst(referenceToAdd); } else { xDoc = XDocument.Parse(@"<?xml version='1.0' encoding='utf-8' standalone='yes'?> <w:ftr xmlns:wpc='http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas' xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:r='http://schemas.openxmlformats.org/officeDocument/2006/relationships' xmlns:m='http://schemas.openxmlformats.org/officeDocument/2006/math' xmlns:v='urn:schemas-microsoft-com:vml' xmlns:wp14='http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing' xmlns:wp='http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing' xmlns:w10='urn:schemas-microsoft-com:office:word' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' xmlns:w14='http://schemas.microsoft.com/office/word/2010/wordml' xmlns:w15='http://schemas.microsoft.com/office/word/2012/wordml' xmlns:wpg='http://schemas.microsoft.com/office/word/2010/wordprocessingGroup' xmlns:wpi='http://schemas.microsoft.com/office/word/2010/wordprocessingInk' xmlns:wne='http://schemas.microsoft.com/office/word/2006/wordml' xmlns:wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape' mc:Ignorable='w14 w15 wp14'> <w:p> <w:pPr> <w:pStyle w:val='Footer' /> </w:pPr> <w:r> <w:t></w:t> </w:r> </w:p> </w:ftr>"); var newFooterPart = mainDocPart.AddNewPart<FooterPart>(); newFooterPart.PutXDocument(xDoc); var referenceToAdd = new XElement(W.footerReference, new XAttribute(W.type, toType), new XAttribute(R.id, mainDocPart.GetIdOfPart(newFooterPart))); sect.AddFirst(referenceToAdd); } }
// Adds child parts and generates content of the specified part. public static void CreateMemo(MainDocumentPart part) { FontTablePart fontTablePart1 = part.AddNewPart<FontTablePart>("rId8"); GenerateFontTablePart1Content(fontTablePart1); DocumentSettingsPart documentSettingsPart1 = part.AddNewPart<DocumentSettingsPart>("rId3"); GenerateDocumentSettingsPart1Content(documentSettingsPart1); documentSettingsPart1.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", new System.Uri("file:///C:\\Users\\Jia\\OneDrive\\Honor%20Council\\Memo%20template.dotx", System.UriKind.Absolute), "rId1"); StyleDefinitionsPart styleDefinitionsPart1 = part.AddNewPart<StyleDefinitionsPart>("rId2"); GenerateStyleDefinitionsPart1Content(styleDefinitionsPart1); CustomXmlPart customXmlPart1 = part.AddNewPart<CustomXmlPart>("application/xml", "rId1"); GenerateCustomXmlPart1Content(customXmlPart1); CustomXmlPropertiesPart customXmlPropertiesPart1 = customXmlPart1.AddNewPart<CustomXmlPropertiesPart>("rId1"); GenerateCustomXmlPropertiesPart1Content(customXmlPropertiesPart1); EndnotesPart endnotesPart1 = part.AddNewPart<EndnotesPart>("rId6"); GenerateEndnotesPart1Content(endnotesPart1); FootnotesPart footnotesPart1 = part.AddNewPart<FootnotesPart>("rId5"); GenerateFootnotesPart1Content(footnotesPart1); WebSettingsPart webSettingsPart1 = part.AddNewPart<WebSettingsPart>("rId4"); GenerateWebSettingsPart1Content(webSettingsPart1); ThemePart themePart1 = part.AddNewPart<ThemePart>("rId9"); GenerateThemePart1Content(themePart1); part.AddHyperlinkRelationship(new System.Uri("mailto:[email protected]", System.UriKind.Absolute), true, "rId7"); GeneratePartContent(part); }
private void InitilizeDocument() { //создаем класс настроек документа _docSettings = new DocSettings(); _mainPart = _wordDocument.AddMainDocumentPart(); //устанавливаем стили var styleDefinitionsPart = _mainPart.AddNewPart<StyleDefinitionsPart>(); styleDefinitionsPart.Styles = new Styles(); foreach (var style in _docSettings.Styles) { styleDefinitionsPart.Styles.Append(style); } //устанавливаем разметку списков _numbering = new Numbering(); _docSettings.GenerateListNumbering(ref _numbering); //numberingDefinitionsPart.Numbering = _docSettings.ListNumbering; //_numberingDefinitionsPart.Numbering = _docSettings.Numbering; _mainPart.Document = new Document(); _body = _mainPart.Document.AppendChild(new Body()); }
public static string CreateFooter(MainDocumentPart mainPart, OpenXmlCompositeElement element) { FooterPart footerPart = mainPart.AddNewPart<FooterPart>(); Footer footer = new Footer(); AddHeaderFooterNamespaceDeclaration(footer); if (element != null) footer.AppendChild(element); footerPart.Footer = footer; return mainPart.GetIdOfPart(footerPart); }
public static string CreateHeader(MainDocumentPart mainPart, OpenXmlCompositeElement element) { HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>(); Header header = new Header(); AddHeaderFooterNamespaceDeclaration(header); if (element != null) header.AppendChild(element); headerPart.Header = header; return mainPart.GetIdOfPart(headerPart); }
//private void AddChartToDoc(MainDocumentPart mainPart, ChartItem chartItem, string controlName) { } private void AddChartToDoc(MainDocumentPart mainPart, ChartItem chartItem, string controlName) { // open Word documant and remove existing content from control Paragraph para = findAndRemoveContent(mainPart, controlName); // generate new ChartPart and ChartSpace ChartPart chartPart = mainPart.AddNewPart<ChartPart>(); string relId = mainPart.GetIdOfPart(chartPart); GraphData gd = chartItem.GraphData; gd.AddEmbeddedToChartPart(chartPart); C.ChartSpace chartSpace = GraphSpace.GenerateChartSpaceWithData(chartItem.Chart, gd.ExternalDataId); chartPart.ChartSpace = chartSpace; // generate a new Wordprocessing Drawing, add to a new Run, // and relate to new ChartPart Run run = new Run(); Drawing drawing = GraphDrawing.GenerateDrawing(relId, controlName, docPrId, Graph.Cx, Graph.Cy); docPrId++; para.Append(run); run.Append(drawing); }
public void InsertBarChartIntoWord(MainDocumentPart mainPart, string title) { // open Word documant and remove existing content from control Paragraph para = findAndRemoveContent(mainPart, "Chart1"); // generate new ChartPart and ChartSpace ChartPart chartPart = mainPart.AddNewPart<ChartPart>(); string relId = mainPart.GetIdOfPart(chartPart); AllocationComparisonBarChart bc = new AllocationComparisonBarChart(); C.Chart chart = bc.GenerateChart(title, null); C.ChartSpace chartSpace = GraphSpace.GenerateChartSpace(chart); // set ChartPart ChartSpace chartPart.ChartSpace = chartSpace; // generate Pie Chart and add to ChartSpace //StressBarChart bc = new StressBarChart(); chartSpace.Append(chart); // set ChartPart ChartSpace chartPart.ChartSpace = chartSpace; // generate a new Wordprocessing Drawing, add to a new Run, // and relate to new ChartPart Run run = new Run(); Drawing drawing = GraphDrawing.GenerateDrawing(relId, "Chart 1", 2U, Graph.Cx, Graph.Cy); para.Append(run); run.Append(drawing); // save and close document mainPart.Document.Save(); }
private static void BuildStyles(MainDocumentPart main) { var stylesPart = main.AddNewPart<StyleDefinitionsPart>(); stylesPart.Styles = new Styles(); stylesPart.Styles.DocDefaults = new DocDefaults(); stylesPart.Styles.DocDefaults.Append( new ParagraphPropertiesDefault( new SpacingBetweenLines { After = "0" }) ); stylesPart.Styles.DocDefaults.Append( new RunPropertiesDefault( new RunFonts { Ascii = "Verdana", HighAnsi = "Verdana", ComplexScript = "Verdana" }, new FontSize { Val = "22" }) ); stylesPart.Styles.Append(new Style { StyleId = h1TextStyleId, StyleRunProperties = new StyleRunProperties( new FontSize { Val = "48" }, new Bold { Val = true }) }); stylesPart.Styles.Append(new Style { StyleId = h2TextStyleId, StyleRunProperties = new StyleRunProperties( new FontSize { Val = "33" }, new Bold { Val = true }) }); stylesPart.Styles.Append(new Style { StyleId = h3TextStyleId, StyleRunProperties = new StyleRunProperties( new Bold { Val = true }) }); }