Bonjour,

Je souhaite créer un document Word 2007 (WordProcessing) grace à OpenXml v.2.
Dans ce document j'ai plusieurs images mais je ne sais pas comment les insérer.

Pour l'instant j'ai réussi à les intégrer au fichier (dans la part : ImagePart).

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
MainDocumentPart mainPart = documentWord.MainDocumentPart;
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Emf);
 
using (FileStream stream = new FileStream(nomFichier, FileMode.Open))
{
    imagePart.FeedData(stream);
}
Je n'ai rien trouvé pour intégrer ces images à mon document Word (document.xml).

Grace à DocumentReflector, j'ai pu voir le code à insérer pour une image. Mais celui-ci me parait énorme

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using wp = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using a = DocumentFormat.OpenXml.Drawing;
using pic = DocumentFormat.OpenXml.Drawing.Pictures;
 
namespace GeneratedCode
{
    public class GeneratedClass
    {
        public static Document GenerateDocument()
        {
            var element = 
                new Document(
                    new Body(
                        new Paragraph(
                            new Run(
                                new RunProperties(
                                    new NoProof(),
                                    new Languages(){ EastAsia = "fr-FR" }),
                                new Drawing(
                                    new wp.Inline(
                                        new wp.Extent(){ Cx = 2038350L, Cy = 3238500L },
                                        new wp.EffectExtent(){ LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                                        new wp.DocProperties(){ Id = (UInt32Value)1U, Name = "Image 0", Description = "bb409621_structureOpenXML_fig01(fr-fr).jpg" },
                                        new wp.NonVisualGraphicFrameDrawingProperties(
                                            new a.GraphicFrameLocks(){ NoChangeAspect = true }),
                                        new a.Graphic(
                                            new a.GraphicData(
                                                new pic.Picture(
                                                    new pic.NonVisualPictureProperties(
                                                        new pic.NonVisualDrawingProperties(){ Id = (UInt32Value)0U, Name = "bb409621_structureOpenXML_fig01(fr-fr).jpg" },
                                                        new pic.NonVisualPictureDrawingProperties()),
                                                    new pic.BlipFill(
                                                        new a.Blip(){ Embed = "rId4", CompressionState = a.BlipCompressionValues.Print },
                                                        new a.Stretch(
                                                            new a.FillRectangle())),
                                                    new pic.ShapeProperties(
                                                        new a.Transform2D(
                                                            new a.Offset(){ X = 0L, Y = 0L },
                                                            new a.Extents(){ Cx = 2038350L, Cy = 3238500L }),
                                                        new a.PresetGeometry(
                                                            new a.AdjustValueList()
                                                        ){ Preset = a.ShapeTypeValues.Rectangle }))
                                            ){ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                                    ){ DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U }))
                        ){ RsidParagraphAddition = new HexBinaryValue() { Value = "00AE0910"}, RsidRunAdditionDefault = new HexBinaryValue() { Value = "005D1B73"} },
                        new SectionProperties(
                            new PageSize(){ Width = (UInt32Value)11906U, Height = (UInt32Value)16838U },
                            new PageMargin(){ Top = 1417, Right = (UInt32Value)1417U, Bottom = 1417, Left = (UInt32Value)1417U, Header = (UInt32Value)708U, Footer = (UInt32Value)708U, Gutter = (UInt32Value)0U },
                            new Columns(){ Space = (UInt32Value)708U },
                            new DocGrid(){ LinePitch = 360 }
                        ){ RsidR = new HexBinaryValue() { Value = "00AE0910"}, RsidSect = new HexBinaryValue() { Value = "00AE0910"} }));
            return element;
        }
 
    }
}
A vrai dire je suis un peu perdu dans tout ça.
J'ai pu récupérer l'ID de mes images.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
mainPart.GetIdOfPart(imagePart)
De plus, j'ai pu voir qu'on indiquait la taille de l'image. Comment peut-on la récupérer ?

N'existe-t'il pas une méthode plus simplifiée pour insérer une image ?


Par avance je vous remercie.