Esempio n. 1
0
 private static void CopyChartObjects(ChartPart oldChart, ChartPart newChart)
 {
     foreach (XElement dataReference in newChart.GetXDocument().Descendants(ns_c + "externalData"))
     {
         string relId = dataReference.Attribute(ns_r + "id").Value;
         EmbeddedPackagePart oldPart = (EmbeddedPackagePart)oldChart.GetPartById(relId);
         EmbeddedPackagePart newPart = newChart.AddEmbeddedPackagePart(oldPart.ContentType);
         using (Stream oldObject = oldPart.GetStream(FileMode.Open, FileAccess.Read))
         using (Stream newObject = newPart.GetStream(FileMode.Create, FileAccess.ReadWrite))
         {
             int byteCount;
             byte[] buffer = new byte[65536];
             while ((byteCount = oldObject.Read(buffer, 0, 65536)) != 0)
                 newObject.Write(buffer, 0, byteCount);
         }
         dataReference.Attribute(ns_r + "id").Value = newChart.GetIdOfPart(newPart);
     }
 }
 private static void CopyChartObjects(ChartPart oldChart, ChartPart newChart)
 {
     foreach (XElement dataReference in newChart.GetXDocument().Descendants(C.externalData))
     {
         string relId = dataReference.Attribute(R.id).Value;
         try
         {
             EmbeddedPackagePart oldPart = (EmbeddedPackagePart)oldChart.GetPartById(relId);
             EmbeddedPackagePart newPart = newChart.AddEmbeddedPackagePart(oldPart.ContentType);
             using (Stream oldObject = oldPart.GetStream(FileMode.Open, FileAccess.Read))
             using (Stream newObject = newPart.GetStream(FileMode.Create, FileAccess.ReadWrite))
             {
                 int byteCount;
                 byte[] buffer = new byte[65536];
                 while ((byteCount = oldObject.Read(buffer, 0, 65536)) != 0)
                     newObject.Write(buffer, 0, byteCount);
             }
             dataReference.Attribute(R.id).Value = newChart.GetIdOfPart(newPart);
         }
         catch (ArgumentOutOfRangeException)
         {
             ExternalRelationship oldRelationship = oldChart.GetExternalRelationship(relId);
             Guid g = Guid.NewGuid();
             string newRid = "R" + g.ToString().Replace("-", "");
             var oldRel = oldChart.ExternalRelationships.FirstOrDefault(h => h.Id == relId);
             if (oldRel == null)
                 throw new DocumentBuilderInternalException("Internal Error 0007");
             newChart.AddExternalRelationship(oldRel.RelationshipType, oldRel.Uri, newRid);
             dataReference.Attribute(R.id).Value = newRid;
         }
     }
 }
        internal void FeedDataChartPart(ChartPart NewPart, ChartPart ExistingPart)
        {
            using (StreamReader sr = new StreamReader(ExistingPart.GetStream()))
            {
                using (StreamWriter sw = new StreamWriter(NewPart.GetStream(FileMode.Create)))
                {
                    sw.Write(sr.ReadToEnd());
                }
            }

            ImagePart imgpNew;

            if (ExistingPart.ChartDrawingPart != null)
            {
                NewPart.AddNewPart<ChartDrawingPart>(ExistingPart.GetIdOfPart(ExistingPart.ChartDrawingPart));
                using (StreamReader sr = new StreamReader(ExistingPart.ChartDrawingPart.GetStream()))
                {
                    using (StreamWriter sw = new StreamWriter(NewPart.ChartDrawingPart.GetStream(FileMode.Create)))
                    {
                        sw.Write(sr.ReadToEnd());
                    }
                }

                // why does a ChartPart contain a ChartDrawingPart that contains a ChartPart??
                // Does it never end??
                if (ExistingPart.ChartDrawingPart.ChartPart != null)
                {
                    NewPart.ChartDrawingPart.AddNewPart<ChartPart>(ExistingPart.ChartDrawingPart.GetIdOfPart(ExistingPart.ChartDrawingPart.ChartPart));
                    this.FeedDataChartPart(NewPart.ChartDrawingPart.ChartPart, ExistingPart.ChartDrawingPart.ChartPart);
                }

                foreach (ImagePart imgp in ExistingPart.ChartDrawingPart.ImageParts)
                {
                    imgpNew = NewPart.ChartDrawingPart.AddImagePart(imgp.ContentType, ExistingPart.ChartDrawingPart.GetIdOfPart(imgp));
                    this.FeedDataImagePart(imgpNew, imgp);
                }
            }

            if (ExistingPart.EmbeddedPackagePart != null)
            {
                NewPart.AddEmbeddedPackagePart(ExistingPart.EmbeddedPackagePart.ContentType);
                this.FeedDataEmbeddedPackagePart(NewPart.EmbeddedPackagePart, ExistingPart.EmbeddedPackagePart);
            }

            foreach (ImagePart imgp in ExistingPart.ImageParts)
            {
                imgpNew = NewPart.AddImagePart(imgp.ContentType, ExistingPart.GetIdOfPart(imgp));
                this.FeedDataImagePart(imgpNew, imgp);
            }

            if (ExistingPart.ThemeOverridePart != null)
            {
                NewPart.AddNewPart<ThemeOverridePart>(ExistingPart.GetIdOfPart(ExistingPart.ThemeOverridePart));
                this.FeedDataThemeOverridePart(NewPart.ThemeOverridePart, ExistingPart.ThemeOverridePart);
            }
        }