Right tool for the job ?
Posted
Sun, Dec 9 2007 13:33
by
bill
I was catching up on reading some msdn blogs and stumbled across an entry from Yves Dolce. Yves was showing how to create a word document part including a graphic. Thing was, he was using C#, not VB. Here's an example :
var graphic = new XElement(dm.GetName("graphic"),
new XAttribute(XNamespace.Xmlns + "dm", dm.NamespaceName),
new XElement(dm.GetName("graphicData"),
new XAttribute("uri", dp.NamespaceName),
new XElement(dp.GetName("pic"),
new XAttribute(XNamespace.Xmlns + "dp", dp.NamespaceName),
new XElement(dp.GetName("nvPicPr"),
new XElement(dp.GetName("cNvPr"),
new XAttribute("id", 0),
new XAttribute("name", "openxmldeveloper.gif")),
new XElement(dp.GetName("cNvPicPr"))),
new XElement(dp.GetName("blipFill"),
new XElement(dm.GetName("blip"),
new XAttribute(r.GetName("embed"), "rId1")),
new XElement(dm.GetName("stretch"),
new XElement(dm.GetName("fillRect")))),
new XElement(dp.GetName("spPr"),
new XElement(dm.GetName("xfrm"),
new XElement(dm.GetName("off"),
new XAttribute("x", 0),
new XAttribute("y", 0)),
new XElement(dm.GetName("ext"),
new XAttribute("cx", 4448175),
new XAttribute("cy", 1181100))),
new XElement(dm.GetName("prstGeom"),
new XAttribute("prst", "rect"),
new XElement(dm.GetName("avLst")))))));
Okay here's the same bit in VB:
Dim graphic = <dm:graphic>
<dm:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<dp:pic>
<dp:nvPicPr>
<dp:cNvPr id="0" name="openxmldeveloper.gif"/>
<dp:cNvPicPr/>
</dp:nvPicPr>
<dp:blipFill>
<dm:blip r:embed="rId1"/>
<dm:stretch>
<dm:fillRect/>
</dm:stretch>
</dp:blipFill>
<dp:spPr>
<dm:xfrm>
<dm:off x="0" y="0"/>
<dm:ext cx="4448175" cy="1181100"/>
</dm:xfrm>
<dm:prstGeom prst="rect">
<dm:avLst/>
</dm:prstGeom>
</dp:spPr>
</dp:pic>
</dm:graphicData>
</dm:graphic>
And this is the complete example :
Imports <xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
Imports <xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
Imports <xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
Imports <xmlns:dm="http://schemas.openxmlformats.org/drawingml/2006/main">
Imports <xmlns:dp="http://schemas.openxmlformats.org/drawingml/2006/picture">
Module Module1
Sub AddToDocToPackage(ByVal package As Package)
Dim documentUri = New Uri("/word/document.xml", UriKind.Relative)
Dim documentPart = package.CreatePart(documentUri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml")
Dim firstParagraph = <w:p>
<w:r>
<w:t>A paragraph</w:t>
</w:r>
</w:p>
Dim secondPararaph = <w:p>
<w:r>
<w:t xml:space="preserve">A</w:t>
</w:r>
<w:hyperlink r:id="rId2">
<w:r>
<w:rPr>
<w:color w:val="0000FF" w:themeColor="hyperlink"/>
<w:u w:val="single"/>
</w:rPr>
<w:t>hyperlink</w:t>
</w:r>
</w:hyperlink>
</w:p>
Dim graphic = <dm:graphic>
<dm:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<dp:pic>
<dp:nvPicPr>
<dp:cNvPr id="0" name="openxmldeveloper.gif"/>
<dp:cNvPicPr/>
</dp:nvPicPr>
<dp:blipFill>
<dm:blip r:embed="rId1"/>
<dm:stretch>
<dm:fillRect/>
</dm:stretch>
</dp:blipFill>
<dp:spPr>
<dm:xfrm>
<dm:off x="0" y="0"/>
<dm:ext cx="4448175" cy="1181100"/>
</dm:xfrm>
<dm:prstGeom prst="rect">
<dm:avLst/>
</dm:prstGeom>
</dp:spPr>
</dp:pic>
</dm:graphicData>
</dm:graphic>
Dim drawing = <w:drawing>
<wp:inline>
<wp:extent cx="4448175" cy="1181100"/>
<wp:docPr id="1" name="Picture 0" descr="openxmldeveloper.gif"/>
<wp:cNvGraphicFramePr>
<dm:graphicFrameLocks noChangeAspect="1"/>
</wp:cNvGraphicFramePr>
<%= graphic %>
</wp:inline>
</w:drawing>
Dim thirdParagraph = <w:p><w:r><%= drawing %></w:r></w:p>
Dim doc = <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document>
<w:body>
<%= firstParagraph %>
<%= secondPararaph %>
<%= thirdParagraph %>
</body>
</w:document>
Using writer = Xml.XmlWriter.Create(documentPart.GetStream)
doc.WriteTo(writer)
End Using
End Sub
Which is the right tool for the job ?