Example #1
0
 public void AddCommentDrawingsNullWorksheetThrowsException()
 {
     using (var package = new ExcelPackage())
     {
         var sheet             = package.Workbook.Worksheets.Add("Sheet1");
         var vmlDrawingsUri    = XmlHelper.GetNewUri(sheet.Package.Package, @"/xl/drawings/vmlDrawing{0}.vml");
         var commentCollection = new ExcelCommentCollection(package, sheet, sheet.NameSpaceManager);
         ExcelVmlDrawingCommentHelper.AddCommentDrawings(null, commentCollection);
     }
 }
Example #2
0
        public void AddCommentDrawingsToPackageWithExistingVml()
        {
            var tempFile = new FileInfo(Path.GetTempFileName());

            if (tempFile.Exists)
            {
                tempFile.Delete();
            }
            try
            {
                using (var package = new ExcelPackage())
                {
                    var sheet             = package.Workbook.Worksheets.Add("Sheet1");
                    var vmlDrawingsUri    = XmlHelper.GetNewUri(sheet.Package.Package, @"/xl/drawings/vmlDrawing{0}.vml");
                    var commentCollection = new ExcelCommentCollection(package, sheet, sheet.NameSpaceManager)
                    {
                        { sheet.Cells[2, 2], "commenttext1", "author" },
                        { sheet.Cells[3, 2], "commenttext2", "author" },
                        { sheet.Cells[4, 2], "commenttext3", "author" }
                    };
                    ExcelVmlDrawingCommentHelper.AddCommentDrawings(sheet, commentCollection);
                    Assert.IsTrue(sheet.Package.Package.TryGetPart(vmlDrawingsUri, out var vmlDrawingsPart));
                    package.SaveAs(tempFile);
                }
                using (var package = new ExcelPackage(tempFile))
                {
                    var sheet             = package.Workbook.Worksheets["Sheet1"];
                    var vmlDrawingsUri    = XmlHelper.GetNewUri(sheet.Package.Package, @"/xl/drawings/vmlDrawing{0}.vml");
                    var commentCollection = new ExcelCommentCollection(package, sheet, sheet.NameSpaceManager)
                    {
                        { sheet.Cells[2, 2], "newcommenttext1", "author" },
                        { sheet.Cells[3, 2], "newcommenttext2", "author" }
                    };
                    ExcelVmlDrawingCommentHelper.AddCommentDrawings(sheet, commentCollection);
                    Assert.IsTrue(sheet.Package.Package.TryGetPart(vmlDrawingsUri, out var vmlDrawingsPart));
                    var xmlDoc = new XmlDocument();
                    xmlDoc.Load(vmlDrawingsPart.GetStream());
                    var nsmgr = new XmlNamespaceManager(new NameTable());
                    nsmgr.AddNamespace("v", "urn:schemas-microsoft-com:vml");
                    var nodes = xmlDoc.SelectNodes("/xml/v:shape", nsmgr);
                    Assert.AreEqual(2, nodes.Count);
                }
            }
            finally
            {
                if (tempFile.Exists)
                {
                    tempFile.Delete();
                }
            }
        }
        /// <summary>
        /// Create and/or update vmlDrawings[n].vml for the specified <paramref name="commentCollection"/> on
        /// the specified <paramref name="sheet"/>.
        /// </summary>
        /// <param name="sheet">The worksheet that the comments belong to.</param>
        /// <param name="commentCollection">The comments to add drawings for.</param>
        public static void AddCommentDrawings(ExcelWorksheet sheet, ExcelCommentCollection commentCollection)
        {
            if (sheet == null)
            {
                throw new ArgumentNullException(nameof(sheet));
            }
            if (commentCollection == null)
            {
                throw new ArgumentNullException(nameof(commentCollection));
            }
            var         vmlDrawingsUri = XmlHelper.GetNewUri(sheet.Package.Package, @"/xl/drawings/vmlDrawing{0}.vml");
            XmlDocument vmlDocumentXml = new XmlDocument();

            if (sheet.Package.Package.TryGetPart(vmlDrawingsUri, out var vmlDrawingsPart))
            {
                vmlDocumentXml.Load(vmlDrawingsPart.GetStream());
                var nodesToDelete = new List <XmlNode>();
                var shapeNodes    = vmlDocumentXml.SelectNodes("v:shape");
                foreach (XmlNode node in shapeNodes)
                {
                    if (node.Attributes?["type"].Value == "#_x0000_t202")
                    {
                        nodesToDelete.Add(node);
                    }
                }
                foreach (var node in nodesToDelete)
                {
                    vmlDocumentXml.RemoveChild(node);
                }
                if (commentCollection.Count > 0)
                {
                    ExcelVmlDrawingCommentHelper.RemoveLegacyDrawingRel(sheet);
                }
            }
            else
            {
                vmlDrawingsPart = sheet.Package.Package.CreatePart(vmlDrawingsUri, "application/vnd.openxmlformats-officedocument.vmlDrawing", sheet.Package.Compression);
                var rel = sheet.Part.CreateRelationship(UriHelper.GetRelativeUri(sheet.WorksheetUri, vmlDrawingsUri), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/vmlDrawing");
                sheet.SetXmlNodeString("d:legacyDrawing/@r:id", rel.Id);
                vmlDocumentXml.LoadXml(ExcelVmlDrawingCommentHelper.CreateVmlDrawings());
            }
            int id = 1024;

            foreach (ExcelComment comment in commentCollection)
            {
                ExcelVmlDrawingCommentHelper.CreateDrawing(vmlDocumentXml, comment.Range, id++);
            }
            vmlDocumentXml.Save(vmlDrawingsPart.GetStream(FileMode.Create));
        }
Example #4
0
 public void AddCommentDrawingsToEmptyPackage()
 {
     using (var package = new ExcelPackage())
     {
         var sheet          = package.Workbook.Worksheets.Add("Sheet1");
         var vmlDrawingsUri = XmlHelper.GetNewUri(sheet.Package.Package, @"/xl/drawings/vmlDrawing{0}.vml");
         Assert.IsFalse(sheet.Package.Package.TryGetPart(vmlDrawingsUri, out var vmlDrawingsPart));
         var commentCollection = new ExcelCommentCollection(package, sheet, sheet.NameSpaceManager)
         {
             { sheet.Cells[2, 2], "commenttext1", "author" },
             { sheet.Cells[3, 2], "commenttext2", "author" },
             { sheet.Cells[4, 2], "commenttext3", "author" }
         };
         ExcelVmlDrawingCommentHelper.AddCommentDrawings(sheet, commentCollection);
         Assert.IsTrue(sheet.Package.Package.TryGetPart(vmlDrawingsUri, out vmlDrawingsPart));
     }
 }