0

I have a w:pict/Picture within a run, to which I am trying to get the Stream of, to copy it to another part of the document.

I've taken the relationship (Id = r1), and loaded the part from the MainDocumentPart, to be given a CustomXmlPart (which doesn't seem right, but the XML suggests it is)... but upon checking my Relationships.xml, the part Id links to bibliography not image data....

Thoughts?

internal static Run CreateImageFromPicObj(WordprocessingDocument sourceDoc, Run sourceRun, OpenXmlPart headerFooterPart, Picture pic)
{
    ImageData data = pic.Descendants<ImageData>().FirstOrDefault();

    OpenXmlPart customP = sourceDoc.MainDocumentPart.GetPartById(data.RelationshipId);

    return CreateCustomImageRun(sourceDoc, sourceRun, headerFooterPart, customP, pic);        
}

private static Run CreateCustomImageRun(WordprocessingDocument sourceDoc, Run sourceRun, OpenXmlPart headerFooterPart, OpenXmlPart customP, Picture pic)
{
    Bitmap image = new Bitmap(customP.GetStream());

    ...omitted - fails on this line
}

RunCode

<w:p w:rsidR="00624DF2" w:rsidRDefault="009E3005" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr>
    <w:pStyle w:val="OfficeAddress" />
    <w:rPr>
      <w:sz w:val="10" />
    </w:rPr>
  </w:pPr>
  <w:r>
    <w:pict>
      <v:shapetype id="_x0000_t75" coordsize="21600,21600" filled="f" stroked="f" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">
        <v:stroke joinstyle="miter" />
        <v:formulas>
          <v:f eqn="if lineDrawn pixelLineWidth 0" />
          <v:f eqn="sum @0 1 0" />
          <v:f eqn="sum 0 0 @1" />
          <v:f eqn="prod @2 1 2" />
          <v:f eqn="prod @3 21600 pixelWidth" />
          <v:f eqn="prod @3 21600 pixelHeight" />
          <v:f eqn="sum @0 0 1" />
          <v:f eqn="prod @6 1 2" />
          <v:f eqn="prod @7 21600 pixelWidth" />
          <v:f eqn="sum @8 21600 0" />
          <v:f eqn="prod @7 21600 pixelHeight" />
          <v:f eqn="sum @10 21600 0" />
        </v:formulas>
        <v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f" />
        <o:lock v:ext="edit" aspectratio="t" />
      </v:shapetype>
      <v:shape id="_x0000_s2049" style="position:absolute;left:0;text-align:left;margin-left:395.25pt;margin-top:3.6pt;width:87pt;height:50.45pt;z-index:251657728" type="#_x0000_t75" xmlns:v="urn:schemas-microsoft-com:vml">
        <v:imagedata o:title="" r:id="rId1" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:o="urn:schemas-microsoft-com:office:office" />
      </v:shape>
    </w:pict>
  </w:r>
</w:p>

Document.xml.rels - ommited for brevity

<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml" Target="../customXml/item1.xml" />
</Relationships>

CustomXml/item.xml

<b:Sources SelectedStyle="\APASixthEditionOfficeOnline.xsl" StyleName="APA" Version="6" xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns="http://schemas.openxmlformats.org/officeDocument/2006/bibliography"></b:Sources>
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Stuart.Sklinar
  • 3,683
  • 4
  • 35
  • 89

1 Answers1

1
using (WordprocessingDocument doc = WordprocessingDocument.Open("document.docx", false)){ 
    var imageParts = doc.MainDocumentPart.ImageParts; 
    foreach(var imagePart in imageParts) { 
        var imageStream = imagePart.GetStream(); 
        //Do somtehing with the stream here. 
        Image bmp = new Bitmap(imageStream);
    } 
}

Once you have the image stream data, its just a matter of converting that into a Bitmap object and placing it inside the Run where you want it copied.

I haven't executed this code but, this should work. Check this link out as well.

Varun Rathore
  • 7,730
  • 3
  • 27
  • 30
  • I'll give this a go - not sure if it contains an ImagePart though :/ – Stuart.Sklinar Feb 05 '14 at 11:42
  • I'd highly recommend you use the [Open XML SDK 2.5 Productivity tool](http://www.microsoft.com/en-in/download/details.aspx?id=30425). Also, if you have a picture/image in the word document, there should be an image part, use the tool to check. – Varun Rathore Feb 06 '14 at 06:36
  • I did use the tool - that's how I got the XML as in my question. Thx – Stuart.Sklinar Feb 06 '14 at 09:08