从ImagePart中提取流

时间:2014-02-04 14:36:25

标签: c# xml openxml openxml-sdk

我在运行中有一个w:pict/Picture,我试图获取Stream,将其复制到文档的另一部分。

我已经接受了关系(Id = r1),并从MainDocumentPart加载了部分,得到了CustomXmlPart(这似乎不对,但XML建议它是...)但在检查我的Relationships.xml后,部分Id链接到参考书目而不是图像数据....

思想?

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>

1 个答案:

答案 0 :(得分: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);
    } 
}

获得图像流数据之后,只需将其转换为Bitmap对象并将其放置在您想要复制的Run中。

我没有执行此代码但是,这应该可行。 也请查看此link