Bonjour,

j'essaie de me familiariser avec la librairie DocumentFormat.OpenXml.
je créé un document word, j'ajoute un logo et ensuite du texte et ensuite une page.

mon texte est ajouté sur la deuxième page bien que le code pour le texte se trouve avant l'ajout d'une nouvelle page. donc le texte devrait, en toute logique, également se trouver sur la première page
Je vous avoue que je ne comprend pas... Car si j'enlève ma méthode AddPage(...) le texte est mis à la bonne place.
Voici mon code :

Code VB.NET : 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Sub Main()
        Dim path = "c:\temp\test.docx"
 
        Using ms As New MemoryStream
            Using doc As WordprocessingDocument = WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document, True)
                Dim docBody As New Body()
                Dim mainPart As MainDocumentPart = doc.AddMainDocumentPart
                mainPart.Document = New Document()
                doc.MainDocumentPart.Document.Append(docBody)
                'doc.AddMainDocumentPart.Document = New Document()
 
                Dim imagePart As ImagePart = mainPart.AddImagePart(ImagePartType.Jpeg)
                Using stream As New FileStream("D:\Logo\Icons\logo_c1.png", FileMode.Open)
                    imagePart.FeedData(stream)
                End Using
 
                AddImageToBody(doc, mainPart.GetIdOfPart(imagePart), True)
                AddParagraphe(doc, 22, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sit amet dui ante. Sed eget purus in nisi tincidunt vulputate vel at turpis. Nullam non pharetra quam, non malesuada nunc. In hac habitasse platea dictumst. Praesent suscipit nibh non leo vulputate consectetur. Aliquam est nibh, pretium vel arcu sit amet, scelerisque facilisis velit. Pellentesque orci odio, iaculis non eros id, convallis mattis nibh. Aenean vehicula lacus vel pellentesque iaculis. Curabitur nec semper orci, a sagittis tellus. Duis mollis placerat elit vel rutrum. Proin mollis libero sed ex pretium, a ornare nibh malesuada. Maecenas ultrices purus vel nisl cursus, consectetur vestibulum massa dignissim. Nam imperdiet mattis mi scelerisque dictum. Phasellus non ullamcorper ligula, eleifend porta tortor. Vestibulum odio dolor, vehicula non suscipit non, euismod vitae augue.
Maecenas viverra leo neque, interdum maximus dui luctus id. Quisque commodo, lorem sed ullamcorper dignissim, neque nibh venenatis leo, sed imperdiet risus libero eu risus. Nulla ultrices, justo in eleifend bibendum, sem nulla egestas augue, sit amet consequat nulla dui sit amet dui. Quisque feugiat libero eu ex dignissim pellentesque. Curabitur aliquam ex eu consectetur congue. Suspendisse venenatis neque vel odio scelerisque, at mattis magna venenatis. Nulla facilisi. Fusce pulvinar consectetur viverra. Duis eu eros vitae arcu viverra dapibus. Aliquam porttitor elit vel elit dignissim, ut blandit tortor molestie.")
                AddPage(doc)
 
                If IO.File.Exists(path) Then
                    IO.File.Delete(path)
                End If
 
                doc.SaveAs(path)
            End Using
        End Using
    End Sub
 
    Private Sub AddParagraphe(ByVal wordDoc As WordprocessingDocument, ByVal fontSize As Integer, ByVal txt As String)
        Dim runFont As New RunFonts With {.Ascii = "Comic Sans MS"}
        Dim runProp As New RunProperties
        Dim runPara As New Run
        Dim para As New Paragraph
 
        runProp.Append(runFont)
        runProp.FontSize = New FontSize() With {.Val = fontSize}
 
        runPara.Append(runProp)
        runPara.Append(New Text(txt))
        para.Append(runPara)
 
        wordDoc.MainDocumentPart.Document.AppendChild(para)
    End Sub
 
    Private Sub AddImageToBody(ByVal wordDoc As WordprocessingDocument, ByVal relationshipId As String, ByVal Optional center As Boolean = False)
        Dim img = New BitmapImage(New Uri("D:\Logo\Icons\logo_c1.png", UriKind.RelativeOrAbsolute))
        Dim iWidth As Integer = CInt(System.Math.Round(img.Width * 9525))
        Dim iHeight As Integer = CInt(System.Math.Round(img.Height * 9525))
 
        ' Define the reference of the image.
        Dim element = New Drawing(
                              New DW.Inline(
                          New DW.Extent() With {.Cx = iWidth, .Cy = iHeight},
                          New DW.EffectExtent() With {.LeftEdge = 0L, .TopEdge = 0L, .RightEdge = 0L, .BottomEdge = 0L},
                          New DW.DocProperties() With {.Id = CType(1UI, UInt32Value), .Name = "Picture1"},
                          New DW.NonVisualGraphicFrameDrawingProperties(
                              New A.GraphicFrameLocks() With {.NoChangeAspect = True}
                              ),
                          New A.Graphic(New A.GraphicData(
                                        New PIC.Picture(
                                            New PIC.NonVisualPictureProperties(
                                                New PIC.NonVisualDrawingProperties() With {.Id = 0UI, .Name = "Koala.jpg"},
                                                New PIC.NonVisualPictureDrawingProperties()
                                                ),
                                            New PIC.BlipFill(
                                                New A.Blip(
                                                    New A.BlipExtensionList(
                                                        New A.BlipExtension() With {.Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"})
                                                    ) With {.Embed = relationshipId, .CompressionState = A.BlipCompressionValues.Print},
                                                New A.Stretch(
                                                    New A.FillRectangle()
                                                    )
                                                ),
                                            New PIC.ShapeProperties(
                                                New A.Transform2D(
                                                    New A.Offset() With {.X = 0L, .Y = 0L},
                                                    New A.Extents() With {.Cx = iWidth, .Cy = iHeight}),
                                                New A.PresetGeometry(
                                                    New A.AdjustValueList()
                                                    ) With {.Preset = A.ShapeTypeValues.Rectangle}
                                                )
                                            )
                                        ) With {.Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"}
                                    )
                                ) With {.DistanceFromTop = 0UI,
                                        .DistanceFromBottom = 0UI,
                                        .DistanceFromLeft = 0UI,
                                        .DistanceFromRight = 0UI})
 
        Dim para As New Paragraph
        Dim justification As New Justification With {.Val = JustificationValues.Center}
        Dim paraProp As New ParagraphProperties()
        Dim run As New Run()
 
        If center Then
            paraProp.Append(justification)
            para.Append(paraProp)
        End If
 
        run.Append(element)
        para.Append(run)
        ' Append the reference to body, the element should be in a Run.
        wordDoc.MainDocumentPart.Document.Body.AppendChild(para) 'New Paragraph(New Run(element)))
    End Sub
 
    Private Sub AddPage(ByVal wordDoc As WordprocessingDocument)
        wordDoc.MainDocumentPart.Document.Body.Append(New Paragraph(New Run(New Break() With {.Type = BreakValues.Page})))
    End Sub