当前位置: 首页>>代码示例>>C#>>正文


C# MainDocumentPart.AddPart方法代码示例

本文整理汇总了C#中DocumentFormat.OpenXml.Packaging.MainDocumentPart.AddPart方法的典型用法代码示例。如果您正苦于以下问题:C# MainDocumentPart.AddPart方法的具体用法?C# MainDocumentPart.AddPart怎么用?C# MainDocumentPart.AddPart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DocumentFormat.OpenXml.Packaging.MainDocumentPart的用法示例。


在下文中一共展示了MainDocumentPart.AddPart方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ImportSmartArtFromPowerPoint

        void ImportSmartArtFromPowerPoint(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile filename)
        {
            string docLayoutPartId = "";
            string docDataPartId = "";
            string docColorsPartId = "";
            string docStylePartId = "";

            byte[] byteArray = filename.OpenBinary();

            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray, 0, (int)byteArray.Length);

                using (PresentationDocument myPres = PresentationDocument.Open(mem, true))
                {
                    PresentationPart presPart = myPres.PresentationPart;
                    // Get the slide that contains the SmartArt graphic.
                    SlidePart slide = (SlidePart)presPart.GetPartById("rId3");
                    // Get all the appropriate parts associated with the SmartArt.
                    DiagramLayoutDefinitionPart layoutPart =
                       slide.DiagramLayoutDefinitionParts.First();
                    DiagramDataPart dataPart = slide.DiagramDataParts.First();
                    DiagramColorsPart colorsPart = slide.DiagramColorsParts.First();
                    DiagramStylePart stylePart = slide.DiagramStyleParts.First();

                    // Get some of the appropriate properties off the SmartArt graphic.
                    PPT.GraphicFrame graphicFrame =
                       slide.Slide.Descendants<PPT.GraphicFrame>().First();
                    PPT.NonVisualDrawingProperties drawingPr = graphicFrame
                       .Descendants<PPT.NonVisualDrawingProperties>().First();
                    Draw.Extents extents =
                       graphicFrame.Descendants<Draw.Extents>().First();

                    // Import SmartArt into the Word document.
                    // Add the SmartArt parts to the Word document.
                    DiagramLayoutDefinitionPart docLayoutPart =
                       mainPart.AddPart<DiagramLayoutDefinitionPart>(layoutPart);
                    DiagramDataPart docDataPart =
                       mainPart.AddPart<DiagramDataPart>(dataPart);
                    DiagramColorsPart docColorsPart =
                       mainPart.AddPart<DiagramColorsPart>(colorsPart);
                    DiagramStylePart docStylePart =
                       mainPart.AddPart<DiagramStylePart>(stylePart);
                    // Get all the relationship ids of the added parts.
                    docLayoutPartId = mainPart.GetIdOfPart(docLayoutPart);
                    docDataPartId = mainPart.GetIdOfPart(docDataPart);
                    docColorsPartId = mainPart.GetIdOfPart(docColorsPart);
                    docStylePartId = mainPart.GetIdOfPart(docStylePart);

                    // Use the document reflector to figure out how to add a SmartArt
                    // graphic to Word.
                    // Change attribute values based on specifics related to the SmartArt.
                    Word.Paragraph p = new Word.Paragraph(
                       new Word.Run(
                       new Word.Drawing(
                       new WP.Inline(
                       new WP.Extent() { Cx = extents.Cx, Cy = extents.Cy },
                       new WP.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                       new WP.DocProperties() { Id = drawingPr.Id, Name = drawingPr.Name },
                       new WP.NonVisualGraphicFrameDrawingProperties(),
                       new Draw.Graphic(
                       new Draw.GraphicData(
                       new Dgm.RelationshipIds()
                       {
                           DataPart = docDataPartId,
                           LayoutPart = docLayoutPartId,
                           StylePart = docStylePartId,
                           ColorPart = docColorsPartId
                       }) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/diagram" }))
                       {
                           DistanceFromTop = (UInt32Value)0U,
                           DistanceFromBottom = (UInt32Value)0U,
                           DistanceFromLeft = (UInt32Value)0U,
                           DistanceFromRight = (UInt32Value)0U
                       })));

                    // Swap out the content control for the SmartArt.
                    OpenXmlElement parent = sdt.Parent;
                    parent.InsertAfter(p, sdt);
                    sdt.Remove();
                }
            }
        }
开发者ID:kawalgrover,项目名称:MARTA-Projects,代码行数:83,代码来源:AssembleDocuments.cs

示例2: insertChart

        private String insertChart(MainDocumentPart mainPart, wp.Paragraph par, String templateName, int dimx, int dimy)
        {
            String relId;
            // vai buscar o gráfico e coloca-o no local com a dimensão definida HARCODED
            wp.Paragraph chartP = new wp.Paragraph();
            wp.Run chartR = new wp.Run();
            chartP.Append(chartR);
            wp.Drawing drawing = new wp.Drawing();
            chartR.Append(drawing);

            clearParagraphText(par);

            //Open Excel spreadsheet
            using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(templateName, true))
            {
                //Get all the appropriate parts

                // assume que há apenas um gráfico por ficheiro template
                // isto deveria evoluir para um ficheiro apenas

                WorkbookPart workbookPart = mySpreadsheet.WorkbookPart;
                WorksheetPart worksheetPart = (WorksheetPart)workbookPart.GetPartById("rId1");
                DrawingsPart drawingPart = worksheetPart.DrawingsPart;
                ChartPart chartPart = (ChartPart)drawingPart.GetPartById("rId1");

                //Clone the chart part and add it to my Word document
                ChartPart importedChartPart = mainPart.AddPart<ChartPart>(chartPart);
                relId = mainPart.GetIdOfPart(importedChartPart);

                //The frame element contains information for the chart
                ssd.GraphicFrame frame = drawingPart.WorksheetDrawing.Descendants<ssd.GraphicFrame>().First();
                string chartName = frame.NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name;

                //Clone this node so we can add it to my Word document
                Graphic clonedGraphic = (Graphic)frame.Graphic.CloneNode(true);

                // clonedGraphic.GraphicData

                ChartReference c = clonedGraphic.GraphicData.GetFirstChild<ChartReference>();
                c.Id = relId;

                //Give the chart a unique id and name
                wpd.DocProperties docPr = new wpd.DocProperties();
                docPr.Name = chartName;
                docPr.Id = GetMaxDocPrId(mainPart) + 1;

                //add the chart data to the inline drawing object
                // wpd.Inline inline = new wpd.Inline(new wpd.Extent() { Cx = 5372100, Cy = 1914525 });
                wpd.Inline inline;
                if (dimx == 0 && dimy == 0)
                    inline = new wpd.Inline();
                else
                    inline = new wpd.Inline(new wpd.Extent() { Cx = dimx, Cy = dimy });
                inline.Append(docPr, clonedGraphic);
                drawing.Append(inline);
            }

            // JUNTA O GRÁFICO
            par.Append(chartP);

            // retorna o ID do gráfico que necessita update
            return relId;
        }
开发者ID:JCanhoto,项目名称:ToolsQtools,代码行数:63,代码来源:RamkoersEngineReport.cs

示例3: ImportChartFromSpreadsheet

        void ImportChartFromSpreadsheet(MainDocumentPart mainPart, Word.SdtElement sdt,
   SPFile spreadsheetFileName)
        {
            // Create a paragraph that has an inline drawing object.
            Word.Paragraph p = new Word.Paragraph();
            Word.Run r = new Word.Run();
            p.Append(r);
            Word.Drawing drawing = new Word.Drawing();
            r.Append(drawing);
            // These dimensions work perfectly for the template document.
            WP.Inline inline = new WP.Inline(new WP.Extent() { Cx = 5486400, Cy = 3200400 });
            byte[] byteArray = spreadsheetFileName.OpenBinary();

            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray, 0, (int)byteArray.Length);

                // Open the Excel spreadsheet.
                using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(mem, true))
                {
                    // Get all of the appropriate parts.
                    WorkbookPart workbookPart = mySpreadsheet.WorkbookPart;
                    WorksheetPart worksheetPart = XLGetWorksheetPartByName(mySpreadsheet,
                       "Sheet2");
                    DrawingsPart drawingPart = worksheetPart.DrawingsPart;
                    ChartPart chartPart = (ChartPart)drawingPart.GetPartById("rId1");

                    // Clone the chart part and add it to the Word document.
                    ChartPart importedChartPart = mainPart.AddPart<ChartPart>(chartPart);
                    string relId = mainPart.GetIdOfPart(importedChartPart);

                    // The frame element contains information for the chart.
                    GraphicFrame frame =
                       drawingPart.WorksheetDrawing.Descendants<GraphicFrame>().First();
                    string chartName = frame.NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name;
                    // Clone this node so that you can add it to the Word document.
                    Draw.Graphic clonedGraphic = (Draw.Graphic)frame.Graphic.CloneNode(true);
                    ChartReference c = clonedGraphic.GraphicData.GetFirstChild<ChartReference>();
                    c.Id = relId;

                    // Give the chart a unique ID and name.
                    WP.DocProperties docPr = new WP.DocProperties();
                    docPr.Name = chartName;
                    docPr.Id = GetMaxDocPrId(mainPart) + 1;

                    // Add the chart data to the inline drawing object.
                    inline.Append(docPr, clonedGraphic);
                    drawing.Append(inline);
                }
            }
            OpenXmlElement parent = sdt.Parent;
            parent.InsertAfter(p, sdt);
            sdt.Remove();
        }
开发者ID:kawalgrover,项目名称:MARTA-Projects,代码行数:54,代码来源:AssembleDocuments.cs


注:本文中的DocumentFormat.OpenXml.Packaging.MainDocumentPart.AddPart方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。