nasorg.blogg.se

Embed file in word
Embed file in word













embed file in word
  1. #EMBED FILE IN WORD HOW TO#
  2. #EMBED FILE IN WORD CODE#

RunProperties runProperties = new RunProperties() ParagraphProperties.Append(new SpacingBetweenLines() ParagraphProperties.Append(new Justification() ParagraphProperties paragraphProperties = new ParagraphProperties() Private static Paragraph CreateParagraph(string paragraphText) Public static string GetUniqueXmlItemID() / Gets a Unique ID for an XML Item, for reference purposes Paragraph paragraphImage = new Paragraph() OVML.OleObject oleObject = new OVML.OleObject()ĭrawAspect = , Paragraph attachmentHeader = CreateParagraph(String.Format("Attachment: If (!displayAsIcon & !openXmlEmbeddedObject.ObjectIsPicture) OpenXmlEmbeddedObject.FileContentType, embeddedPackagePartId) ĮmbeddedObjectPart embeddedObjectPart = mainDocumentPart.AddNewPart( If (openXmlEmbeddedObject.ObjectIsOfficeDocument)ĮmbeddedPackagePart embeddedObjectPart = mainDocumentPart.AddNewPart( String embeddedPackagePartId = GetUniqueXmlItemID() ImagePart imagePart = mainDocumentPart.AddImagePart(ImagePartType.Emf, imagePartId) String imagePartId = GetUniqueXmlItemID() Using (Stream emfStream = new MemoryStream(Convert.FromBase64String(openXmlEmbeddedObject.OleImageBinaryData))) If (!String.IsNullOrEmpty(openXmlEmbeddedObject.OleImageBinaryData)) Using (Stream dataStream = new MemoryStream(Convert.FromBase64String(openXmlEmbeddedObject.OleObjectBinaryData))) If (!String.IsNullOrEmpty(openXmlEmbeddedObject.OleObjectBinaryData)) OpenXmlEmbeddedObject openXmlEmbeddedObject = new OpenXmlEmbeddedObject(fileInfo, displayAsIcon) Public static void AppendEmbeddedObject(MainDocumentPart mainDocumentPart, FileInfo fileInfo, bool displayAsIcon) / Whether or not to display the embedded file as an Icon (Otherwise it will display a snapshot of the file) / The FileInfo object associated with the file being embedded / The MainDocument Part of your OpenXml Word Doc

embed file in word

/ Appends an Embedded Object into the specified Main Document This class in an OpenXml helper class, holds all the logic to embed an object into your OpenXml File using DocumentFormat.OpenXml.OpenXmlHelper.AppendEmbeddedObject(_mainDocumentPart, fileInfo, displayAsIcon)

embed file in word

Private void AttachFile(string filePathAndName, bool displayAsIcon)įileInfo fileInfo = new FileInfo(filePathAndName) _mainDocumentPart.Document = new Document(new true) _mainDocumentPart = wpDocument.AddMainDocumentPart() Using (WordprocessingDocument wpDocument = WordprocessingDocumentType.Document)) Private MainDocumentPart _mainDocumentPart

  • This the main class that starts everything, opens a WordProcessingDocument and class to have the file attached using.
  • DocumentFormat.OpenXml dll (OpenXml 2.0).
  • I know there is a lot of code, and not much explanation… Hopefully it is easy to follow and will help people out 
  • Use those Streams as the File Object and File Image for your OpenXml Document.
  • Use the binary data from the above step to create Data and Image Streams.
  • Create a custom OpenXmlEmbeddedObject object (this is what holds all the binary data).
  • Create an OpenXml WordProcessingDocument, get the System.IO.FileInfo for the file you want to Embed.
  • #EMBED FILE IN WORD CODE#

    My code below is broken down into several classes, but here is an outline of what I am doing: Basically, I am programmatically looking at the OpenXml 2.0 Document Reflector to get the information I need. In this example I’d like to show how I use COM to PROGRMATICALLY get the OLE-binary data information of the attached file, and then how I used that information within my OpenXml document.

    embed file in word

    If you are like me, “invoking the OLE server application to create an IStorage” doesn’t mean much to you. Since I am already using OpenXml to generate documents, and I am in need of embedding other files into Word, I have decided use a collaboration of OpenXml and COM (Microsoft Office 2007 dll’s) to achieve my goal. I have learned a lot from other people’s code, so this is my attempt to contribute.

    #EMBED FILE IN WORD HOW TO#

    There are several examples out there that show how to embed an Office Document into another Office Document using OpenXml 2.0, what’s not out there and easily understandable is how to embed just about any file into and Office Document. I got a lot from this site, so here I asked and answered my own question in order to give back a little on a topic in which I had difficulty finding answers on, hope it helps people. Embedding Foreign Objects (PDF, TXT, GIF, etc…) into Microsoft Word using OpenXml 2.0















    Embed file in word