Dankwansere's Profile User Rating: -----

Reputation: 73 Whiz
Active Members
Active Posts:
233 (0.11 per day)
09-November 09
Profile Views:
Last Active:
User is offline Today, 06:29 AM

Previous Fields

OS Preference:
Favorite Browser:
Favorite Processor:
Favorite Gaming Platform:
Your Car:
Dream Kudos:

Latest Visitors

Icon   Dankwansere has not set their status

Posts I've Made

  1. In Topic: modifying a constructor-Almost got it, but not quite

    Posted 17 Jul 2015

    You are calling getannualInterestRate() method from the Bank Account class but you don't have that method created in the class.
  2. In Topic: Insert Image into Shape in Word Document using OpenXML SDK

    Posted 17 Jun 2015

    View Postandrewsw, on 17 June 2015 - 05:08 PM, said:

    Just to note that we can also see the XML content of an Office document by renaming the file, adding the .zip extension. After extracting, for a Word document, there is a 'word' folder and a 'document.xml'.

    Cool, I never I could do that..if I edit the document.xml file directly, I would see the changes in the document correct?
  3. In Topic: Insert Image into Shape in Word Document using OpenXML SDK

    Posted 17 Jun 2015

    Hey guys, so after an intense week of researching and playing with openXML SDK, I've finally found the answer and solution to my topic question. Since there's no direct help on this issue on the web, because openXML is kinda new, I figured I'd share my solution so anyone who runs into this issue will benefit from this. Maybe I can put this as a tutorial if I get forum permission.

    So here's the initial problem: We have over 300 templates in Microsoft Word format, and those templates have custom shapes inside acting a placeholder/bookmark. Those custom shapes are used for the insertion of barcodes through a C# application. With MS Interop, this task could easily be done with just a few lines of code. But as people are becoming aware, Microsoft does not recommend using MS Interop on the server side, they recommend using its new OpenXML sdk that supports office 2007 and higher. First thing you should know is that OpenXML compared to MS Interop is not easy at all. The Microsoft OpenXML SDK is just lipstick on top of the XML and some things which were easy to do with the interop assemblies will give you nightmares doing it in OpenXML.

    Open XML Solution to my problem:
    1. So the first thing you should realize and also will be helpful when using open XML sdk is to try to visualize every document in XML format, because that is the whole point of office Open XML. The Microsoft SDK provides a "Productivity tool" that will open office documents(eg. Word, excell, powerpoint), and allow you to see the C# and XML equivalent codes(It can be very difficult to decipher the C# equivalent codes). Another great tool to use is the "Open XML Package Editor Extension for visual studio. This extension allows you to see xml structure of your document.

    2. To achieve the inserting picture into a shape, I had to do the following. first I had to use the following OpenXML assemblies

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using DocumentFormat.OpenXml;
    using A = DocumentFormat.OpenXml.Drawing;
    using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
    using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
    using DocumentFormat.OpenXml.Drawing.Wordprocessing;
    using Wps = DocumentFormat.OpenXml.Office2010.Word.DrawingShape;

    After that you open the document, you must add an imagePart that references the picture(in my case the barcode) object to the MainDocumentPart object by using a file stream, and get the ID of the image

    string temp;
    MainDocumentPart mainPart = document.MainDocumentPart;
                                   ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Bmp);
                                   using (FileStream stream = new FileStream(barcodepath, FileMode.Open))
                                   temp = mainPart.GetIdOfPart(imagePart);

    In office OpenXML, a picture that is inserted into a word document is considered a "Blip" Object or element. The class is derived from the DocumentFormat.OpenXml.Drawing the Blip must have an Embed value that is of an imagePart ID. The Blip object then goes inside a BlipFill Object/element, and that also goes inside a graphicData Object/element and that in turn goes into a graphic object element. I'm pretty sure by now you've realized everything works like an xml tree. :lol:
     <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                            <a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
                                  <a:spLocks noChangeArrowheads="1" />
                                <wps:spPr bwMode="auto">
                                    <a:off x="0" y="0" />
                                    <a:ext cx="1234440" cy="1234440" />
                                  <a:prstGeom prst="roundRect">
                                      <a:gd name="adj" fmla="val 16667" />
                                  <a:blipFill dpi="0" rotWithShape="1">
                                    <a:blip r:embed="Raade88ffea8d4c1b" />
                                      <a:fillRect l="10000" t="10000" r="10000" b="10000" />
                                <wps:bodyPr rot="0" vert="horz" wrap="square" lIns="91440" tIns="45720" rIns="91440" bIns="45720" anchor="t" anchorCtr="0" upright="1">
                                  <a:noAutofit />

    Now I have a reference of the image(barcode) I'm trying to add to my document. I want to insert the image into the shapes in the template document. To do this, I would have to use some LINQ to iterate through the document and get a reference to all the shapes in the document. The wps:spPr element you see in the above XML code is the xml element for the shapes in my document. The equivalent C# class is WordprocessingShape

    IEnumerable<DocumentFormat.OpenXml.Office2010.Word.DrawingShape.WordprocessingShape> shapes2 = document.MainDocumentPart.document.Body.Descendants<DocumentFormat.OpenXml.Office2010.Word.DrawingShape.WordprocessingShape>();

    Now that I have a collection of all the shape references in my document. I now loop through the collection with a foreach, and through each iteration I create a Blip object. Set the Blip object embed value to the picture ID reference I captured earlier form the image part. I also create a Stretch object, and FillRectangle object(these are not really necessary, I just used them for proper alignment of the barcode). And append each to its parent object like the XML tree equivalent.

     foreach (DocumentFormat.OpenXml.Office2010.Word.DrawingShape.WordprocessingShape sp in shapes2)
                                       //Wps.ShapeProperties shapeProperties1 = 
                                       A.BlipFill blipFill1 = new A.BlipFill() { Dpi = (UInt32Value)0U, RotateWithShape = true };
                                       A.Blip blip1 = new A.Blip() { Embed = temp };
                                       A.Stretch stretch1 = new A.Stretch();
                                       A.FillRectangle fillRectangle1 = new A.FillRectangle() { Left = 10000, Top = 10000, Right = 10000, Bottom = 10000 };
                                       Wps.WordprocessingShape wordprocessingShape1 = new Wps.WordprocessingShape();
                                           Wps.ShapeProperties shapeProperties1 = sp.Descendants<Wps.ShapeProperties>().First();

    That's pretty much it.. Like I said earlier, openXML compared to MS Interop is very difficult. MS Interop could've achieved the above task in about 5 lines of code :lol: Anyways I would like to really thank AndrewSW for guiding me to the right path.
  4. In Topic: Insert Image into Shape in Word Document using OpenXML SDK

    Posted 12 Jun 2015

    Thanks AndrewSW, we are limited to using C# for our application, but i'm pretty sure, I can use the psuedo VB code you provided into C# and write a quick WPF application that to do the template modification..Now whether my manager or our customer will approve is another different issue. But thank you :chinese:
  5. In Topic: Insert Image into Shape in Word Document using OpenXML SDK

    Posted 12 Jun 2015

    I never thought about that, But I guess I could try the bookmark idea, there are over 300 templates. Would we have to place the book marks in each template manually?

My Information

Member Title:
D.I.C Head
Age Unknown
Birthday Unknown
Years Programming:
Programming Languages:
Java, C#, C, Unix, HTML, Xhtml, PHP

Contact Information

Click here to e-mail me



Dankwansere has no profile comments yet. Why not say hello?