Exemplo n.º 1
0
        /// <summary>
        /// Converts the paragraph.
        /// </summary>
        /// <param name="worksheetPart">The worksheet part.</param>
        /// <param name="RTFSection">The RTF section.</param>
        /// <param name="matchShape">The match shape.</param>
        /// <returns></returns>
        public static DrawingSpreadsheet.Shape ConvertParagraph(WorksheetPart worksheetPart, Section RTFSection, DrawingSpreadsheet.Shape matchShape)
        {
            DrawingSpreadsheet.Shape           NewShape = new DrawingSpreadsheet.Shape();
            DocumentFormat.OpenXml.Drawing.Run ShapeRun = null;

            // get the graphic frame from the source anchor
            var sourceAnchor = worksheetPart.DrawingsPart.WorksheetDrawing.GetFirstChild <DrawingSpreadsheet.TwoCellAnchor>();
            var sourceShape  = sourceAnchor.Descendants <DrawingSpreadsheet.Shape>().FirstOrDefault();

            // add it to the target anchor (ie. the one with the shape removed)
            DocumentFormat.OpenXml.Drawing.Spreadsheet.Shape targetShape = (DocumentFormat.OpenXml.Drawing.Spreadsheet.Shape)NewShape.CloneNode(true);

            targetShape.Append(matchShape.NonVisualShapeProperties.CloneNode(true));
            targetShape.Append(matchShape.ShapeProperties.CloneNode(true));
            targetShape.Append(matchShape.ShapeStyle.CloneNode(true));

            targetShape.Append(matchShape.TextBody.CloneNode(true));

            //Remove the text associated with the shape

            foreach (DocumentFormat.OpenXml.Drawing.Paragraph Paragraph in targetShape.TextBody.Descendants <DocumentFormat.OpenXml.Drawing.Paragraph>())
            {
                foreach (DocumentFormat.OpenXml.Drawing.Run RunToRemove in Paragraph.Descendants <DocumentFormat.OpenXml.Drawing.Run>())
                {
                    RunToRemove.Remove();
                }
            }

            try
            {
                foreach (System.Windows.Documents.Paragraph p in RTFSection.Blocks)
                {
                    InlineCollection ParagraphInLines = p.Inlines;


                    DocumentFormat.OpenXml.Drawing.Paragraph ShapeParagraph = new DocumentFormat.OpenXml.Drawing.Paragraph();

                    foreach (var InLine in ParagraphInLines)
                    {
                        if (InLine.GetType() == typeof(System.Windows.Documents.Span))
                        {
                            Span s = (Span)InLine;

                            Brush SourceTextColour = s.Foreground;

                            foreach (System.Windows.Documents.Run r in s.Inlines)
                            {
                                if (r.Text != "\n")
                                {
                                    ShapeRun = new DocumentFormat.OpenXml.Drawing.Run();

                                    DocumentFormat.OpenXml.Drawing.RunProperties ShapeRunProperties = new DocumentFormat.OpenXml.Drawing.RunProperties();
                                    DocumentFormat.OpenXml.Drawing.Text          ShapeText          = new DocumentFormat.OpenXml.Drawing.Text(r.Text);

                                    //the font family will be inherited from the sheet of the target shape
                                    ShapeRunProperties.FontSize = new Int32Value(System.Convert.ToInt32(s.FontSize * 100));

                                    SolidFill textFill = new SolidFill();

                                    SystemColor textColour = new SystemColor();

                                    textColour.Val = new EnumValue <SystemColorValues>(SystemColorValues.WindowText);

                                    Int64Value ColourMask   = 0xFF000000;
                                    Int64Value SourceColour = System.Convert.ToInt64(SourceTextColour.ToString().Replace("#", "0x"), 16);

                                    SourceColour = SourceColour % ColourMask;

                                    textColour.LastColor = new HexBinaryValue(string.Format("{0,10:X}", SourceColour));

                                    textFill.SystemColor = textColour;

                                    ShapeRunProperties.Append(textFill);

                                    if (r.FontWeight == System.Windows.FontWeights.Bold)
                                    {
                                        ShapeRunProperties.Bold = true;
                                    }

                                    if (r.FontStyle == System.Windows.FontStyles.Italic)
                                    {
                                        ShapeRunProperties.Italic = true;
                                    }

                                    if (r.TextDecorations == System.Windows.TextDecorations.Underline)
                                    {
                                        ShapeRunProperties.Underline = new EnumValue <TextUnderlineValues>(TextUnderlineValues.Single);
                                    }

                                    ShapeRun.Text          = ShapeText;
                                    ShapeRun.RunProperties = ShapeRunProperties;

                                    ShapeParagraph.Append(ShapeRun);
                                }
                            }
                        }
                        else
                        {
                            //do something else
                        }
                    }

                    targetShape.TextBody.Append(ShapeParagraph);
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }

            return(targetShape);
        }