Пример #1
0
        internal override void Process(DocxNode node, ref Paragraph paragraph)
        {
            if (IsHidden(node))
            {
                return;
            }

            string src = node.ExtractAttributeValue("src");

            if (!string.IsNullOrEmpty(src))
            {
                try
                {
                    Drawing drawing = PrepareImage(src);

                    if (drawing != null)
                    {
                        if (paragraph == null)
                        {
                            paragraph = node.Parent.AppendChild(new Paragraph());
                            OnParagraphCreated(node, paragraph);
                        }

                        Run run = paragraph.AppendChild(new Run(drawing));
                        RunCreated(node, run);
                    }
                }
                catch
                {
                    return;//fails silently?
                }
            }
        }
        /// <summary>
        /// Embeds an object with an extension included in the validImageExtensions, officeXmlExtensions, officeBasicExtensions, or validObjectExtensions Lists.
        /// </summary>
        /// <param name="realFileName">Full path to the file.</param>
        /// <param name="displayFileName">Name displayed under the file icon in the document.</param>
        public void AddObject(String realFileName, String displayFileName)
        {
            String filePath = realFileName;
            Paragraph para = new Paragraph();
            Run run = null;
            if (File.Exists(filePath))
            {
                MainDocumentPart mainPart = _package.MainDocumentPart;

                ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);
                GenerateImage(imagePart, displayFileName);

                String extension = Path.GetExtension(filePath);
                String contentType = GetContentType(extension);
                if (officeXmlExtensions.Contains(extension))
                {
                    EmbeddedPackagePart embeddedPackagePart = _package.MainDocumentPart.AddEmbeddedPackagePart(contentType);
                    using (FileStream stream = new FileStream(filePath, FileMode.Open))
                    {
                        embeddedPackagePart.FeedData(stream);
                    }
                    run = GetObjectRun(mainPart.GetIdOfPart(imagePart), mainPart.GetIdOfPart(embeddedPackagePart), extension);
                }
                else if (officeBasicExtensions.Contains(extension))
                {
                    EmbeddedObjectPart embeddedObjectPart = _package.MainDocumentPart.AddEmbeddedObjectPart(contentType);
                    using (FileStream stream = new FileStream(filePath, FileMode.Open))
                    {
                        embeddedObjectPart.FeedData(stream);
                    }
                    run = GetObjectRun(mainPart.GetIdOfPart(imagePart), mainPart.GetIdOfPart(embeddedObjectPart), extension);
                }
                else if (validObjectExtensions.Contains(extension))
                {
                    StringBuilder errorMessages;
                    EmbeddedObjectPart embeddedObjectPart = _package.MainDocumentPart.AddEmbeddedObjectPart(contentType);
                    String outputBinaryName = GetBinaryName(filePath);
                    bool success = GenerateOleObject(filePath, outputBinaryName, out errorMessages);
                    if (success)
                    {
                        using (FileStream stream = new FileStream(outputBinaryName, FileMode.Open))
                        {
                            embeddedObjectPart.FeedData(stream);
                        }
                        try
                        {
                            File.Delete(outputBinaryName);
                            run = GetObjectRun(mainPart.GetIdOfPart(imagePart), mainPart.GetIdOfPart(embeddedObjectPart), extension);

                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Failed to remove file: {0}", outputBinaryName);
                        }
                    }
                    else
                    {
                        Debug.WriteLine("Failed to generate OLE Object: {0}", filePath);
                    }
                }
                else
                {
                    Debug.WriteLine("Invalid file format: {0}", filePath);
                }
                para.AppendChild(run);
                _document.Append(para);
            }
            else
            {
                Debug.WriteLine("File not found: {0}", filePath);
            }
        }
Пример #3
0
        public static void FormatFLow(SlidePart slidePart)
        {
            if (slidePart != default(SlidePart))
            {
                IEnumerable <Shape> flowChartShapes = slidePart.Slide.Descendants <Shape>().Where(d => d.Descendants <NonVisualDrawingProperties>().Where(e => e.Name.InnerText.Contains(Constants._ARROW_)).Count() > 0);
                int           i       = 0;
                long          firstId = 0;
                D.Transform2D prevBox = new D.Transform2D();
                foreach (Shape shape in flowChartShapes)
                {
                    String title = String.Empty;
                    NonVisualDrawingProperties meta = shape.Descendants <NonVisualDrawingProperties>().FirstOrDefault();


                    //resize shape
                    D.Transform2D currentShapePos = shape.Descendants <D.Transform2D>().FirstOrDefault();

                    currentShapePos.Extents.Cx = 2775204;
                    currentShapePos.Extents.Cy = 1446936;
                    if (i == 0)
                    {
                        firstId = meta.Id;
                        currentShapePos.Offset.X = 458569;
                        currentShapePos.Offset.Y = 1712339;
                    }
                    else
                    {
                        currentShapePos.Offset.X = Convert.ToInt64((prevBox.Extents.Cx.Value - (prevBox.Extents.Cx.Value * 0.20)) + prevBox.Offset.X);
                        currentShapePos.Offset.Y = prevBox.Offset.Y;
                    }



                    prevBox = currentShapePos;


                    if (meta != default(NonVisualDrawingProperties))
                    {
                        long idToFind = (firstId + i) - (flowChartShapes.Count() + 1);//check

                        Shape titleShape = slidePart.Slide.Descendants <Shape>().Where(d => d.Descendants <NonVisualDrawingProperties>().First() != default(NonVisualDrawingProperties) &&
                                                                                       d.Descendants <NonVisualDrawingProperties>().First().Id == idToFind &&
                                                                                       d.Descendants <NonVisualDrawingProperties>().First().Name.InnerText.Contains(Constants._TEXTBOX_))
                                           .FirstOrDefault();
                        if (titleShape != default(Shape))
                        {
                            D.Paragraph data = titleShape.Descendants <D.Paragraph>().FirstOrDefault();

                            title = data != default(D.Paragraph) ? data.InnerText : title;
                        }

                        //slidePart.Slide.RemoveChild(titleShape);
                        titleShape.Remove();
                        slidePart.Slide.Save();

                        D.Paragraph paragraph = shape.Descendants <D.Paragraph>().FirstOrDefault();
                        if (paragraph != default(D.Paragraph))
                        {
                            paragraph.RemoveAllChildren();
                            slidePart.Slide.Save();
                            D.Run run1 = new D.Run();
                            paragraph.AppendChild(new D.ParagraphProperties()
                            {
                                Alignment = D.TextAlignmentTypeValues.Center
                            });
                            run1.AppendChild(new D.RunProperties()
                            {
                                Language = "en-US", Dirty = true
                            });
                            run1.Text = new D.Text(title);
                            paragraph.AppendChild(run1);
                        }
                        slidePart.Slide.Save();
                    }
                    i++;
                }
            }
        }
 /// <summary>
 /// Embeds an image within the document.
 /// </summary>
 /// <param name="filePath">File path to the image to embed.</param>
 public void AddImage(String filePath)
 {
     Paragraph para = new Paragraph();
     Run run = null;
     if (File.Exists(filePath))
     {
         String extension = Path.GetExtension(filePath);
         if (validImageExtensions.Contains(extension))
         {
             ImagePartType imagePartType = GetImagePartType(extension);
             ImagePart imagePart = _package.MainDocumentPart.AddImagePart(imagePartType);
             using (FileStream stream = new FileStream(filePath, FileMode.Open))
             {
                 imagePart.FeedData(stream);
             }
             using (FileStream stream = new FileStream(filePath, FileMode.Open))
             {
                 Bitmap image = new Bitmap(stream);
                 run = GetImageRun(_package.MainDocumentPart.GetIdOfPart(imagePart), image);
                 para.AppendChild(run);
                 _document.Append(para);
             }
         }
         else
         {
             Debug.WriteLine("Invalid file format: {0}", filePath);
         }
     }
     else
     {
         Debug.WriteLine("File not found: {0}", filePath);
     }
 }