private void CopyHeaderFooterPictures(ExcelWorksheet Copy, ExcelWorksheet added) { if (Copy._headerFooter == null) { return; } //Copy the texts CopyText(Copy.HeaderFooter._oddHeader, added.HeaderFooter.OddHeader); CopyText(Copy.HeaderFooter._oddFooter, added.HeaderFooter.OddFooter); CopyText(Copy.HeaderFooter._evenHeader, added.HeaderFooter.EvenHeader); CopyText(Copy.HeaderFooter._evenFooter, added.HeaderFooter.EvenFooter); CopyText(Copy.HeaderFooter._firstHeader, added.HeaderFooter.FirstHeader); CopyText(Copy.HeaderFooter._firstFooter, added.HeaderFooter.FirstFooter); //Copy any images; if (Copy.HeaderFooter.Pictures.Count > 0) { Uri source = Copy.HeaderFooter.Pictures.Uri; Uri dest = XmlHelper.GetNewUri(_pck.Package, @"/xl/drawings/vmlDrawing{0}.vml"); var part = _pck.Package.CreatePart(dest, "application/vnd.openxmlformats-officedocument.vmlDrawing", _pck.Compression); foreach (ExcelVmlDrawingPicture pic in Copy.HeaderFooter.Pictures) { var item = added.HeaderFooter.Pictures.Add(pic.Id, pic.ImageUri, pic.Title, pic.Width, pic.Height); foreach (XmlAttribute att in pic.TopNode.Attributes) { (item.TopNode as XmlElement).SetAttribute(att.Name, att.Value); } item.TopNode.InnerXml = pic.TopNode.InnerXml; } } }
internal void SaveHeaderFooterImages() { if (_vmlDrawingsHF != null) { if (_vmlDrawingsHF.Count == 0) { if (_vmlDrawingsHF.Uri != null) { _ws.Part.DeleteRelationship(_vmlDrawingsHF.RelId); _ws._package.Package.DeletePart(_vmlDrawingsHF.Uri); } } else { if (_vmlDrawingsHF.Uri == null) { _vmlDrawingsHF.Uri = XmlHelper.GetNewUri(_ws._package.Package, @"/xl/drawings/vmlDrawing{0}.vml"); } if (_vmlDrawingsHF.Part == null) { _vmlDrawingsHF.Part = _ws._package.Package.CreatePart(_vmlDrawingsHF.Uri, "application/vnd.openxmlformats-officedocument.vmlDrawing", _ws._package.Compression); var rel = _ws.Part.CreateRelationship(PackUriHelper.GetRelativeUri(_ws.WorksheetUri, _vmlDrawingsHF.Uri), TargetMode.Internal, ExcelPackage.schemaRelationships + "/vmlDrawing"); _ws.SetHFLegacyDrawingRel(rel.Id); _vmlDrawingsHF.RelId = rel.Id; foreach (ExcelVmlDrawingPicture draw in _vmlDrawingsHF) { rel = _vmlDrawingsHF.Part.CreateRelationship(PackUriHelper.GetRelativeUri(_vmlDrawingsHF.Uri, draw.ImageUri), TargetMode.Internal, ExcelPackage.schemaRelationships + "/image"); draw.RelId = rel.Id; } } _vmlDrawingsHF.VmlDrawingXml.Save(_vmlDrawingsHF.Part.GetStream()); } } }
/// <summary> /// Inserts a picture at the end of the text in the header or footer /// </summary> /// <param name="PictureFile">The image object containing the Picture</param> /// <param name="Alignment">Alignment. The image object will be inserted at the end of the Text.</param> public ExcelVmlDrawingPicture InsertPicture(FileInfo PictureFile, PictureAlignment Alignment) { string id = ValidateImage(Alignment); Image Picture; try { if (!PictureFile.Exists) { throw (new FileNotFoundException(string.Format("{0} is missing", PictureFile.FullName))); } Picture = Image.FromFile(PictureFile.FullName); } catch (Exception ex) { throw (new InvalidDataException("File is not a supported image-file or is corrupt", ex)); } ImageConverter ic = new ImageConverter(); string contentType = ExcelPicture.GetContentType(PictureFile.Extension); var uriPic = XmlHelper.GetNewUri(_ws._package.Package, "/xl/media/" + PictureFile.Name.Substring(0, PictureFile.Name.Length - PictureFile.Extension.Length) + "{0}" + PictureFile.Extension); byte[] imgBytes = (byte[])ic.ConvertTo(Picture, typeof(byte[])); var ii = _ws.Workbook._package.AddImage(imgBytes, uriPic, contentType); return(AddImage(Picture, id, ii)); }
private void CopyComment(ExcelWorksheet Copy, ExcelWorksheet workSheet) { //First copy the drawing XML string xml = Copy.Comments.CommentXml.InnerXml; var uriComment = new Uri(string.Format("/xl/comments{0}.xml", workSheet.SheetID), UriKind.Relative); if (_pck.Package.PartExists(uriComment)) { uriComment = XmlHelper.GetNewUri(_pck.Package, "/xl/drawings/vmldrawing{0}.vml"); } var part = _pck.Package.CreatePart(uriComment, "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml", _pck.Compression); StreamWriter streamDrawing = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)); streamDrawing.Write(xml); streamDrawing.Close(); //Add the relationship ID to the worksheet xml. PackageRelationship commentRelation = workSheet.Part.CreateRelationship(PackUriHelper.GetRelativeUri(workSheet.WorksheetUri, uriComment), TargetMode.Internal, ExcelPackage.schemaRelationships + "/comments"); xml = Copy.VmlDrawingsComments.VmlDrawingXml.InnerXml; var uriVml = new Uri(string.Format("/xl/drawings/vmldrawing{0}.vml", workSheet.SheetID), UriKind.Relative); if (_pck.Package.PartExists(uriVml)) { uriVml = XmlHelper.GetNewUri(_pck.Package, "/xl/drawings/vmldrawing{0}.vml"); } var vmlPart = _pck.Package.CreatePart(uriVml, "application/vnd.openxmlformats-officedocument.vmlDrawing", _pck.Compression); StreamWriter streamVml = new StreamWriter(vmlPart.GetStream(FileMode.Create, FileAccess.Write)); streamVml.Write(xml); streamVml.Close(); PackageRelationship newVmlRel = workSheet.Part.CreateRelationship(PackUriHelper.GetRelativeUri(workSheet.WorksheetUri, uriVml), TargetMode.Internal, ExcelPackage.schemaRelationships + "/vmlDrawing"); //Add the relationship ID to the worksheet xml. XmlElement e = workSheet.WorksheetXml.SelectSingleNode("//d:legacyDrawing", _namespaceManager) as XmlElement; if (e == null) { workSheet.CreateNode("d:legacyDrawing"); e = workSheet.WorksheetXml.SelectSingleNode("//d:legacyDrawing", _namespaceManager) as XmlElement; } e.SetAttribute("id", ExcelPackage.schemaRelationships, newVmlRel.Id); }
/// <summary> /// Set the picture from an image file. /// The image file will be saved as a blob, so make sure Excel supports the image format. /// </summary> /// <param name="PictureFile">The image file.</param> public void SetFromFile(FileInfo PictureFile) { DeletePrevImage(); Image img; try { img = Image.FromFile(PictureFile.FullName); } catch (Exception ex) { throw (new InvalidDataException("File is not a supported image-file or is corrupt", ex)); } ImageConverter ic = new ImageConverter(); string contentType = ExcelPicture.GetContentType(PictureFile.Extension); var imageURI = XmlHelper.GetNewUri(_workSheet._package.Package, "/xl/media/" + PictureFile.Name.Substring(0, PictureFile.Name.Length - PictureFile.Extension.Length) + "{0}" + PictureFile.Extension); byte[] fileBytes = (byte[])ic.ConvertTo(img, typeof(byte[])); var ii = _workSheet.Workbook._package.AddImage(fileBytes, imageURI, contentType); if (_workSheet.Part.Package.PartExists(imageURI) && ii.RefCount == 1) //The file exists with another content, overwrite it. { //Remove the part if it exists _workSheet.Part.Package.DeletePart(imageURI); } var imagePart = _workSheet.Part.Package.CreatePart(imageURI, contentType, CompressionOption.NotCompressed); //Save the picture to package. var strm = imagePart.GetStream(FileMode.Create, FileAccess.Write); strm.Write(fileBytes, 0, fileBytes.Length); var rel = _workSheet.Part.CreateRelationship(imageURI, TargetMode.Internal, ExcelPackage.schemaRelationships + "/image"); SetXmlNodeString(BACKGROUNDPIC_PATH, rel.Id); }
private void CopyDrawing(ExcelWorksheet Copy, ExcelWorksheet workSheet /*, PackageRelationship r*/) { //Check if the worksheet has drawings //if(_xlPackage.Package.PartExists(r.TargetUri)) //{ //First copy the drawing XML string xml = Copy.Drawings.DrawingXml.OuterXml; var uriDraw = new Uri(string.Format("/xl/drawings/drawing{0}.xml", workSheet.SheetID), UriKind.Relative); var part = _pck.Package.CreatePart(uriDraw, "application/vnd.openxmlformats-officedocument.drawing+xml", _pck.Compression); StreamWriter streamDrawing = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)); streamDrawing.Write(xml); streamDrawing.Close(); XmlDocument drawXml = new XmlDocument(); drawXml.LoadXml(xml); //Add the relationship ID to the worksheet xml. PackageRelationship drawRelation = workSheet.Part.CreateRelationship(PackUriHelper.GetRelativeUri(workSheet.WorksheetUri, uriDraw), TargetMode.Internal, ExcelPackage.schemaRelationships + "/drawing"); XmlElement e = workSheet.WorksheetXml.SelectSingleNode("//d:drawing", _namespaceManager) as XmlElement; e.SetAttribute("id", ExcelPackage.schemaRelationships, drawRelation.Id); foreach (ExcelDrawing draw in Copy.Drawings) { if (draw is ExcelChart) { ExcelChart chart = draw as ExcelChart; xml = chart.ChartXml.InnerXml; var UriChart = XmlHelper.GetNewUri(_pck.Package, "/xl/charts/chart{0}.xml"); var chartPart = _pck.Package.CreatePart(UriChart, "application/vnd.openxmlformats-officedocument.drawingml.chart+xml", _pck.Compression); StreamWriter streamChart = new StreamWriter(chartPart.GetStream(FileMode.Create, FileAccess.Write)); streamChart.Write(xml); streamChart.Close(); //Now create the new relationship to the copied chart xml var prevRelID = draw.TopNode.SelectSingleNode("xdr:graphicFrame/a:graphic/a:graphicData/c:chart/@r:id", Copy.Drawings.NameSpaceManager).Value; var rel = part.CreateRelationship(PackUriHelper.GetRelativeUri(uriDraw, UriChart), TargetMode.Internal, ExcelPackage.schemaRelationships + "/chart"); XmlAttribute relAtt = drawXml.SelectSingleNode(string.Format("//c:chart/@r:id[.='{0}']", prevRelID), Copy.Drawings.NameSpaceManager) as XmlAttribute; relAtt.Value = rel.Id; } else if (draw is ExcelPicture) { ExcelPicture pic = draw as ExcelPicture; var uri = pic.UriPic; if (!workSheet.Workbook._package.Package.PartExists(uri)) { var picPart = workSheet.Workbook._package.Package.CreatePart(uri, pic.ContentType, CompressionOption.NotCompressed); pic.Image.Save(picPart.GetStream(FileMode.Create, FileAccess.Write), pic.ImageFormat); } var prevRelID = draw.TopNode.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip/@r:embed", Copy.Drawings.NameSpaceManager).Value; var rel = part.CreateRelationship(PackUriHelper.GetRelativeUri(workSheet.WorksheetUri, uri), TargetMode.Internal, ExcelPackage.schemaRelationships + "/image"); XmlAttribute relAtt = drawXml.SelectSingleNode(string.Format("//xdr:pic/xdr:blipFill/a:blip/@r:embed[.='{0}']", prevRelID), Copy.Drawings.NameSpaceManager) as XmlAttribute; relAtt.Value = rel.Id; } } //rewrite the drawing xml with the new relID's streamDrawing = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)); streamDrawing.Write(drawXml.OuterXml); streamDrawing.Close(); //} }