public static void InsertAPicture(string fileName, string document, string bookmark) { OpenSettings openSettings = new OpenSettings(); // Add the MarkupCompatibilityProcessSettings openSettings.MarkupCompatibilityProcessSettings = new MarkupCompatibilityProcessSettings( MarkupCompatibilityProcessMode.ProcessAllParts, FileFormatVersions.Office2007); // string document = @"E:\**项目\**文书\108.docx"; // document = @"F:\Project_Code\SVNProject\SDHS_SZCG_ZCCG\SZZF\SZZFWord\xcjckyyszj.docx"; using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true)) { MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart; ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg); using (FileStream stream = new FileStream(fileName, FileMode.Open)) { imagePart.FeedData(stream); } AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart), bookmark); } }
private static void GenerateImagePart1Content(ImagePart imagePart1) { var data = GetBinaryDataStream(_imagePart1Data); imagePart1.FeedData(data); data.Close(); }
/// <summary> /// Get the picture and add it into the content control /// </summary> public void AddPictureFromUri() { string pic_id = null; Drawing dr = content_control.Descendants <Drawing>().FirstOrDefault(); if (dr != null) { Blip blip = dr.Descendants <Blip>().FirstOrDefault(); if (blip != null) { pic_id = blip.Embed; } } if (pic_id != null) { IdPartPair idpp = WordTemplateManager.document.MainDocumentPart.Parts .Where(pa => pa.RelationshipId == pic_id).FirstOrDefault(); if (idpp != null) { ImagePart ip = (ImagePart)idpp.OpenXmlPart; if (IsImageUrl(image_path)) { Stream stream = DownloadData(image_path); ip.FeedData(stream); stream.Close(); } } } }
/// <summary> /// Sets the document theme /// </summary> /// <param name="theme">Theme package</param> public static OpenXmlPowerToolsDocument SetTheme(WmlDocument doc, OpenXmlPowerToolsDocument themeDoc) { using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc)) { using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument()) { using (OpenXmlMemoryStreamDocument themeStream = new OpenXmlMemoryStreamDocument(themeDoc)) using (Package theme = themeStream.GetPackage()) { // Gets the theme manager part PackageRelationship themeManagerRelationship = theme.GetRelationshipsByType(mainDocumentRelationshipType).FirstOrDefault(); if (themeManagerRelationship != null) { PackagePart themeManagerPart = theme.GetPart(themeManagerRelationship.TargetUri); // Gets the theme main part PackageRelationship themeRelationship = themeManagerPart.GetRelationshipsByType(themeRelationshipType).FirstOrDefault(); if (themeRelationship != null) { PackagePart themePart = theme.GetPart(themeRelationship.TargetUri); XDocument newThemeDocument = XDocument.Load(XmlReader.Create(themePart.GetStream(FileMode.Open, FileAccess.Read))); // Removes existing theme part from document if (document.MainDocumentPart.ThemePart != null) { document.MainDocumentPart.DeletePart(document.MainDocumentPart.ThemePart); } // Creates a new theme part ThemePart documentThemePart = document.MainDocumentPart.AddNewPart <ThemePart>(); var embeddedItems = newThemeDocument .Descendants() .Attributes(relationshipns + "embed"); foreach (PackageRelationship imageRelationship in themePart.GetRelationships()) { // Adds an image part to the theme part and stores contents inside PackagePart imagePart = theme.GetPart(imageRelationship.TargetUri); ImagePart newImagePart = documentThemePart.AddImagePart(GetImagePartType(imagePart.ContentType)); newImagePart.FeedData(imagePart.GetStream(FileMode.Open, FileAccess.Read)); // Updates relationship references into the theme XDocument XAttribute relationshipAttribute = embeddedItems.FirstOrDefault(e => e.Value == imageRelationship.Id); if (relationshipAttribute != null) { relationshipAttribute.Value = documentThemePart.GetIdOfPart(newImagePart); } } documentThemePart.PutXDocument(newThemeDocument); } } } } return(streamDoc.GetModifiedDocument()); } }
public static void AddImagePart(SlidePart slidePart, ImagePartType imagePartType, string id, Stream stream) { stream.Seek(0, SeekOrigin.Begin); ImagePart imagePart = slidePart.AddImagePart(imagePartType, id); imagePart.FeedData(stream); }
public void Render() { ImagePart imagePart = WordDocument.MainDocumentPart.AddImagePart(ImagePartType.Png); using (var ms = new MemoryStream(ParagraphImage.Source)) { imagePart.FeedData(ms); } WordParagraph p = new WordParagraph(); ParagraphProperties pp = new ParagraphProperties() { Indentation = new Indentation() { Left = (500 * Depth).ToString() } }; p.Append(pp); Run run = new Run(GetDrawing(WordDocument.MainDocumentPart.GetIdOfPart(imagePart))); p.Append(run); WordDocument.MainDocumentPart.Document.Body.AppendChild(p); WordDocument.MainDocumentPart.Document.Body.AppendChild(GetParagraphImageTitle()); }
/// <summary> /// Add an image to the body by its stream /// </summary> /// <param name="image"></param> public void AddImage(Stream image) { ImagePart imagePart = _mainDocumentPart.AddImagePart("image/png"); imagePart.FeedData(image); AddImageToBody(_package, _mainDocumentPart.GetIdOfPart(imagePart)); }
/// <summary> /// 按书签插入图片 /// </summary> /// <param name="filePath"></param> /// <param name="picPath"></param> /// <param name="bm"></param> /// <param name="x">宽度厘米</param> /// <param name="y">高度厘米</param> /// <param name="type"></param> public static void InsertBMPicture(string filePath, string picPath, string bm, long x, long y, ImagePartType type) { using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true)) { MainDocumentPart mainPart = doc.MainDocumentPart; BookmarkStart bmStart = findBookMarkStart(doc, bm); if (bmStart == null) { return; } ImagePart imagePart = mainPart.AddImagePart(type); using (FileStream stream = new FileStream(picPath, FileMode.Open)) { imagePart.FeedData(stream); } long cx = 360000L * x;//360000L = 1厘米 long cy = 360000L * y; Run r = AddImageToBody(doc, mainPart.GetIdOfPart(imagePart), cx, cy); bmStart.Parent.InsertAfter <Run>(r, bmStart); mainPart.Document.Save(); } }
public static void GenerateImagePart2Content(ImagePart imagePart2) { var data = GetBinaryDataStream(ImagePart2Data); imagePart2.FeedData(data); data.Close(); }
/// <summary> /// Clones this slide. /// </summary> /// <returns>The clone.</returns> /// <remarks> /// <see href="http://blogs.msdn.com/b/brian_jones/archive/2009/08/13/adding-repeating-data-to-powerpoint.aspx">Adding Repeating Data to PowerPoint</see> /// <see href="http://startbigthinksmall.wordpress.com/2011/05/17/cloning-a-slide-using-open-xml-sdk-2-0/">Cloning a Slide using Open Xml SDK 2.0</see> /// <see href="http://www.exsilio.com/blog/post/2011/03/21/Cloning-Slides-including-Images-and-Charts-in-PowerPoint-presentations-Using-Open-XML-SDK-20-Productivity-Tool.aspx">See Cloning Slides including Images and Charts in PowerPoint presentations and Using Open XML SDK 2.0 Productivity Tool</see> /// </remarks> public PowerpointSlide Clone() { SlidePart slideTemplate = this.slidePart; // Clone slide contents SlidePart slidePartClone = this.presentationPart.AddNewPart <SlidePart>(); using (var templateStream = slideTemplate.GetStream(FileMode.Open)) { slidePartClone.FeedData(templateStream); } // Copy layout part slidePartClone.AddPart(slideTemplate.SlideLayoutPart); // Copy the image parts foreach (ImagePart image in slideTemplate.ImageParts) { ImagePart imageClone = slidePartClone.AddImagePart(image.ContentType, slideTemplate.GetIdOfPart(image)); using (var imageStream = image.GetStream()) { imageClone.FeedData(imageStream); } } return(new PowerpointSlide(this.presentationPart, slidePartClone)); }
public bool ReplaceImageInSlide(SlidePart slidePart, string imageFileLocation) { this.Logger.Log("Trying to replace image in slide Part"); var mySlideLayoutPart = slidePart.SlideLayoutPart; ImagePart imagePart = slidePart.AddImagePart("image/png"); var imgRelId = slidePart.GetIdOfPart(imagePart); FileStream stream = new FileStream(imageFileLocation, FileMode.Open); imagePart.FeedData(stream); stream.Close(); // https://blogs.msdn.microsoft.com/brian_jones/2008/11/18/creating-a-presentation-report-based-on-data/ // Find the first image element and replace try { Drawing.Blip blip = slidePart.Slide.Descendants <Blip>().First(); blip.Embed = imgRelId; this.Logger.Log("Saving slide Part"); slidePart.Slide.Save(); return(true); } catch (Exception) { return(false); } }
private Run AddImage(WordprocessingDocument wordprocessingDocument, OpenXmlElement refChild, string imageBase64) { var bytes = Convert.FromBase64String(imageBase64.Split(',')[1]); var imagePath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".png"; try { MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart; ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png); File.WriteAllBytes(imagePath, bytes); // Carregando a imagem em imagePart using (FileStream stream = new FileStream(imagePath, FileMode.Open)) { imagePart.FeedData(stream); } return(AddImageToBody(wordprocessingDocument, refChild, mainPart.GetIdOfPart(imagePart), imagePath)); } finally { File.Delete(imagePath); } }
public override Run ObtainRun(WordprocessingDocument package) { ImagePart imagePart = package.MainDocumentPart.AddImagePart(ImagePartType.Bmp); imagePart.FeedData(new MemoryStream(m_Image)); return(new Run(CreateImage(package.MainDocumentPart.GetIdOfPart(imagePart), m_Size))); }
private void StreamImagePart(string base64Image) { Stream data = GetStream(base64Image); ImagePart.FeedData(data); data.Close(); }
/// <summary> /// Add ImagePart to the document. /// </summary> string AddImagePart(FileInfo newImg) { ImagePartType type = ImagePartType.Bmp; switch (newImg.Extension.ToLower()) { case ".jpeg": case ".jpg": type = ImagePartType.Jpeg; break; case ".png": type = ImagePartType.Png; break; default: type = ImagePartType.Bmp; break; } ImagePart newImgPart = Document.MainDocumentPart.AddImagePart(type); using (FileStream stream = newImg.OpenRead()) { newImgPart.FeedData(stream); } string rId = Document.MainDocumentPart.GetIdOfPart(newImgPart); return(rId); }
public static void ExportCaseToDoc(Case caseFile, string mapImagePath, string folderName) { using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Create(Path.Combine(folderName, string.Format("CaseReport-{0}.docx", caseFile.CaseNumber)), WordprocessingDocumentType.Document)) { //wordprocessingDocument.AddMainDocumentPart(); MainDocumentPart mainPart = wordprocessingDocument.AddMainDocumentPart(); mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); var para = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph()); var run = para.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Run()); run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text(string.Format("GPS Coordinate Report: Case #{0}", caseFile.CaseNumber))); ApplyStyleToParagraph(wordprocessingDocument, "Title", "Title", para); AddTable(body, caseFile.GPSCoordinates.Where(g => g.IncludedInMap).OrderBy(g => g.FileTime).ToList()); using (FileStream fs = new FileStream(mapImagePath, FileMode.Open, FileAccess.Read)) { ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png); imagePart.FeedData(fs); AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart)); } //wordprocessingDocument.Save(); wordprocessingDocument.Close(); } }
static void Main(string[] args) { string file = @"c:\temp\mydoc.docx"; string imageFile = @"c:\temp\myimage.jpg"; string labelText = "PersonMainPhoto"; using (var document = WordprocessingDocument.Open(file, isEditable: true)) { var mainPart = document.MainDocumentPart; var table = mainPart.Document.Body.Descendants <Table>().First(); var pictureCell = table.Descendants <TableCell>().First(c => c.InnerText == labelText); ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg); using (FileStream stream = new FileStream(imageFile, FileMode.Open)) { imagePart.FeedData(stream); } pictureCell.RemoveAllChildren(); AddImageToCell(pictureCell, mainPart.GetIdOfPart(imagePart)); mainPart.Document.Save(); } }
public static void Main(string[] args) { File.Copy(TemplatePath, OutputPath, true); using (PresentationDocument presentationDoc = PresentationDocument.Open(OutputPath, true)) { PresentationPart presPart = presentationDoc.PresentationPart; // Step 1: Identify the proper Part Id SlidePart contentSlidePart = (SlidePart)presPart.GetPartById("rId2"); // Step 2: Replace one image with external file string imageRel = "rIdImg"; int imageRelId = 1; var imgId = imageRel + imageRelId; ImagePart imagePart = contentSlidePart.AddImagePart(ImagePartType.Jpeg, imgId); using (Image image = Image.FromFile(ImagePath)) { using (MemoryStream m = new MemoryStream()) { image.Save(m, image.RawFormat); m.Position = 0; imagePart.FeedData(m); SwapPhoto(contentSlidePart, imgId); } } // Step 3: Replace text matched by the key SwapPlaceholderText(contentSlidePart, "{{Title}}", "Testing Title for IBM"); // Step 4: Fill out multi value fields by table var tupleList = new List <(string category, string model, string price)> { ("Automobile", "Ford", "$25K"), ("Automobile", "Toyota", "$30K"), ("Computer", "IBM PC", "$2.5K"), ("Laptop", "Dell", "$1K"), ("Laptop", "Microsoft", "$2K") }; Drawing.Table tbl = contentSlidePart.Slide.Descendants <Drawing.Table>().First(); foreach (var row in tupleList) { Drawing.TableRow tr = new Drawing.TableRow(); tr.Height = 100; tr.Append(CreateTextCell(row.category)); tr.Append(CreateTextCell(row.model)); tr.Append(CreateTextCell(row.price)); tbl.Append(tr); } // Step 5: Save the presentation presPart.Presentation.Save(); } }
// Generates content of imagePart1. private void GenerateImagePartContent(ImagePart imagePart, Image image) { MemoryStream memStream = new MemoryStream(); image.Save(memStream, ImageFormat.Png); memStream.Position = 0; imagePart.FeedData(memStream); memStream.Close(); }
//word插入图片 public static void InsertAPicture(string document, string fileName) { string imgType = fileName.Split('.')[fileName.Split('.').Length - 1]; using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true)) { try { MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart; ImagePart imagePart = null; //判断图片的格式 if (imgType.ToUpper() == "JPEG" || imgType.ToUpper() == "JPE") { imagePart = mainPart.AddImagePart(ImagePartType.Jpeg); } else if (imgType.ToUpper() == "PNG") { imagePart = mainPart.AddImagePart(ImagePartType.Png); } else if (imgType.ToUpper() == "GIF") { imagePart = mainPart.AddImagePart(ImagePartType.Gif); } else if (imgType.ToUpper() == "TIFF" || imgType.ToUpper() == "TIF") { imagePart = mainPart.AddImagePart(ImagePartType.Tiff); } if (imagePart != null) { if (File.Exists(fileName)) { using (FileStream stream = new FileStream(fileName, FileMode.Open)) { imagePart.FeedData(stream); } AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart)); } else { Console.WriteLine("图片文件不存在"); } } else { Console.WriteLine("不支持的图片类型"); } } catch (Exception e) { Console.WriteLine("文档被占用!" + e); } } }
/// <summary> /// Adds a new picture to the slide in order to re-use the picture later on. /// </summary> /// <param name="picture">The picture as a byte array.</param> /// <param name="contentType">The picture content type: image/png, image/jpeg...</param> /// <returns>The image part</returns> internal ImagePart AddPicture(byte[] picture, string contentType) { ImagePartType type = 0; switch (contentType) { case "image/bmp": type = ImagePartType.Bmp; break; case "image/emf": // TODO type = ImagePartType.Emf; break; case "image/gif": // TODO type = ImagePartType.Gif; break; case "image/ico": // TODO type = ImagePartType.Icon; break; case "image/jpeg": type = ImagePartType.Jpeg; break; case "image/pcx": // TODO type = ImagePartType.Pcx; break; case "image/png": type = ImagePartType.Png; break; case "image/tiff": // TODO type = ImagePartType.Tiff; break; case "image/wmf": // TODO type = ImagePartType.Wmf; break; } ImagePart imagePart = this.slidePart.AddImagePart(type); // FeedData() closes the stream and we cannot reuse it (ObjectDisposedException) // solution: copy the original stream to a MemoryStream using (MemoryStream stream = new MemoryStream(picture)) { imagePart.FeedData(stream); } // No need to detect duplicated images // PowerPoint do it for us on the next manual save return(imagePart); }
public static void CreateWordprocessingDocument(string filepath, List <Student> st) { String fileName = Constants.Locations.ImagesFolder + "/" + Constants.Locations.ImageFile; using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document)) { // Add a main document part. MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); // Create the document structure and add some text. mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); foreach (var stu in st) { Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); run.AppendChild(new Text("Hello , my name is " + stu.FirstName + " " + stu.LastName + "\n\n")); string imageFilePath = stu.FullPathUrl + "/" + Constants.Locations.ImageFile; var ImageBytes = FTP.DownloadFileBytes(imageFilePath); SaveBytesToFile(fileName, ImageBytes); ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg); using (FileStream stream = new FileStream(fileName, FileMode.Open)) { imagePart.FeedData(stream); } AddImageToBody(wordDocument, mainPart.GetIdOfPart(imagePart)); Paragraph PageBreakParagraph = new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Break() { Type = BreakValues.Page })); body.Append(PageBreakParagraph); } // var styles = ExtractStylesPart(filepath, true); } }
private ImagePart GetImagePart(string path) { using (FileStream stream = new FileStream(path, FileMode.Open)) { ImagePart imagePart = _mainDocumentPart.AddImagePart("image/png"); imagePart.FeedData(stream); return(imagePart); } }
private static void AddImagePart(this Slide slide, string relationshipId, string imageFile) { ImagePart imgPart = slide.SlidePart.AddImagePart(GetImagePartType(imageFile), relationshipId); using (FileStream imgStream = File.Open(imageFile, FileMode.Open)) { imgPart.FeedData(imgStream); } }
public void InsertAutoText(string SignatureName) { const string NEW_DOCUMENT_NAME = "Sample AutoText Insert.docx"; string oldrelIDPic = ""; DocumentFormat.OpenXml.Drawing.Blip imgblip; using (WordprocessingDocument sampleDocument = WordprocessingDocument.Create(pathnewdoc + NEW_DOCUMENT_NAME, WordprocessingDocumentType.Document)) using (WordprocessingDocument wrdTemplate = WordprocessingDocument.Open(pathtemplatedoc + TEMPLATE_NAME, false)) { MainDocumentPart mdp = sampleDocument.AddMainDocumentPart(); mdp.Document = new Document(new Body()); GlossaryDocumentPart gDocPart = wrdTemplate.MainDocumentPart.GetPartsOfType <GlossaryDocumentPart>().FirstOrDefault(); if (gDocPart != null) { GlossaryDocument gDoc = gDocPart.GlossaryDocument; if (gDoc != null) { Console.WriteLine("AutoText Entries!"); foreach (DocPart entry in gDoc.DocParts) { if (entry.DocPartProperties.Category.Gallery.Val == DocPartGalleryValues.AutoText && entry.DocPartProperties.DocPartName.Val == SignatureName) { Console.WriteLine("Entry Name ==> {0}", entry.DocPartProperties.DocPartName.Val); Console.WriteLine(entry.DocPartBody.InnerXml); int paracount = entry.DocPartBody.Descendants <Paragraph>().Count(); Console.WriteLine("Count of paragraphs ==> {0}", paracount); foreach (Paragraph entrypara in entry.DocPartBody.Descendants <Paragraph>()) { // Let's get the relationship ID if it's there int PicCount = entrypara.Descendants <DocumentFormat.OpenXml.Drawing.Blip>().Count(); if (PicCount > 0) { imgblip = entrypara.Descendants <DocumentFormat.OpenXml.Drawing.Blip>().FirstOrDefault(); oldrelIDPic = imgblip.Embed.Value; Console.WriteLine("Old Relationship ID ==> {0}", oldrelIDPic); imgblip.Embed.Value = "rId10"; Console.WriteLine(imgblip.Embed.Value); ImagePart newSigImg = mdp.AddImagePart(ImagePartType.Png, imgblip.Embed.Value); mdp.CreateRelationshipToPart(newSigImg, imgblip.Embed.Value); newSigImg.FeedData(gDocPart.GetPartById(oldrelIDPic).GetStream()); } mdp.Document.Body.AppendChild <Paragraph>(new Paragraph(entrypara.OuterXml)); } } } } } else { Console.WriteLine("No Glossary Document Part (AutoText Entries) found."); } } }
private static List <SlidePart> CreateImageSlideParts(PresentationPart presentationPart, List <SvgDocument> svgDocs) { int id = 256; string relId; SlideId newSlideId; SlideLayoutId newSlideLayoutId; uint uniqueId = GetMaxUniqueId(presentationPart); uint maxSlideId = GetMaxSlideId(presentationPart.Presentation.SlideIdList); // get first slide master part: template SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.First(); List <SlidePart> slideParts = new List <SlidePart>(); for (int i = 0; i < svgDocs.Count; i++) { id++; using (MemoryStream ms = new MemoryStream()) { using (System.Drawing.Bitmap image = svgDocs[i].Draw()) { image.Save(ms, ImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); relId = "rId" + id; // add new slide part SlidePart slidePart = presentationPart.AddNewPart <SlidePart>(relId); // add image part to slide part ImagePart imagePart = slidePart.AddImagePart(ImagePartType.Bmp, relId); imagePart.FeedData(ms); // add image slide CreateImageSlide(relId).Save(slidePart); // add slide layout part to slide part SlideLayoutPart slideLayoutPart = slidePart.AddNewPart <SlideLayoutPart>(); CreateSlideLayoutPart().Save(slideLayoutPart); slideMasterPart.AddPart(slideLayoutPart); slideLayoutPart.AddPart(slideMasterPart); uniqueId++; newSlideLayoutId = new SlideLayoutId(); newSlideLayoutId.RelationshipId = slideMasterPart.GetIdOfPart(slideLayoutPart); newSlideLayoutId.Id = uniqueId; slideMasterPart.SlideMaster.SlideLayoutIdList.Append(newSlideLayoutId); // add slide part to presentaion slide list maxSlideId++; newSlideId = new SlideId(); newSlideId.RelationshipId = relId; newSlideId.Id = maxSlideId; presentationPart.Presentation.SlideIdList.Append(newSlideId); } } } slideMasterPart.SlideMaster.Save(); return(slideParts); }
public void Reparent(MainDocumentPart documentPart) { ImagePart newImage = documentPart.AddImagePart(this._image.ContentType); newImage.FeedData(this._image.GetStream(FileMode.Open, FileAccess.Read)); String newId = documentPart.GetIdOfPart(newImage); this._id = newId; this._image = newImage; }
public static void InsertPicture(string fullPathToImageFile, WordprocessingDocument wordDoc) { MainDocumentPart mainPart = wordDoc.MainDocumentPart; ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg); using (FileStream stream = new FileStream(fullPathToImageFile, FileMode.Open)) { imagePart.FeedData(stream); } AddImageToBody(wordDoc, mainPart.GetIdOfPart(imagePart)); }
private void InsertPicturePng(Stream imageStream, double scaleWidth, double scaleHeight) { MainDocumentPart mainPart = WordDoc.MainDocumentPart; ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png); imagePart.FeedData(imageStream); AddImageToBody(mainPart.GetIdOfPart(imagePart), scaleWidth, scaleHeight); }
public void ApplyHeader(WordprocessingDocument doc) { MainDocumentPart mainDocPart = doc.MainDocumentPart; HeaderPart headerPart1 = mainDocPart.AddNewPart <HeaderPart>("r97"); //ImagePart imagePart = mainDocPart.AddNewPart<ImagePart>("image/jpeg", fileName); ImagePart imagePart = mainDocPart.AddImagePart(ImagePartType.Jpeg); string fileName = @"C:\multigestion\MultiFiscalia\Images\MultiFiscalia\Enviar.png"; //System.Web.HttpContext.Current.Server.MapPath(); using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { imagePart.FeedData(stream); } //var imagePart = doc.MainDocumentPart.AddNewPart<ImagePart>("image/jpeg", "rIdImagePart1"); //GenerateImagePart(imagePart); MemoryStream ms = new MemoryStream(); Header header1 = new Header(); Paragraph paragraph1 = new Paragraph() { }; Run run1 = new Run(); Text text1 = new Text(); text1.Text = "un texto cualquiera"; //run1.Append(ms.ToArray()); run1.Append(text1); paragraph1.Append(run1); header1.Append(paragraph1); headerPart1.Header = header1; SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants <SectionProperties>().FirstOrDefault(); if (sectionProperties1 == null) { sectionProperties1 = new SectionProperties() { }; mainDocPart.Document.Body.Append(sectionProperties1); } HeaderReference headerReference1 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = "r97" }; sectionProperties1.InsertAt(headerReference1, 0); //var element = "imagen"; //mainDocPart.Document.Body.PrependChild(new Paragraph(new Run(element))); }