/
ShapesActions.cs
186 lines (150 loc) · 7.54 KB
/
ShapesActions.cs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit;
namespace RichEditDocumentServerAPIExample.CodeExamples
{
class ShapesActions
{
public static Action<RichEditDocumentServer> AddFloatingPictureAction = AddFloatingPicture;
public static Action<RichEditDocumentServer> FloatingPictureOffsetAction = FloatingPictureOffset;
public static Action<RichEditDocumentServer> ChangeZorderAndWrappingAction = ChangeZorderAndWrapping;
public static Action<RichEditDocumentServer> AddTextBoxAction = AddTextBox;
public static Action<RichEditDocumentServer> InsertRichTextInTextBoxAction = InsertRichTextInTextBox;
public static Action<RichEditDocumentServer> RotateAndResizeAction = RotateAndResize;
static void AddFloatingPicture(RichEditDocumentServer wordProcessor)
{
#region #AddFloatingPicture
// Access a document.
Document document = wordProcessor.Document;
// Append text to the document.
document.AppendText("Line One\nLine Two\nLine Three");
// Insert a picture at the specified position from the file.
Shape myPicture = document.Shapes.InsertPicture(document.CreatePosition(15),
System.Drawing.Image.FromFile("Documents\\beverages.png"));
// Specify the picture alignment.
myPicture.HorizontalAlignment = ShapeHorizontalAlignment.Center;
#endregion #AddFloatingPicture
}
static void FloatingPictureOffset(RichEditDocumentServer wordProcessor)
{
#region #FloatingPictureOffset
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
// Access a document.
Document document = wordProcessor.Document;
// Specify the document's measure units.
document.Unit = DevExpress.Office.DocumentUnit.Centimeter;
if (document.Shapes.Count > 1)
{
// Access a picture.
Shape myPicture = document.Shapes[1];
// Clear the horizontal and vertical alignment values.
myPicture.HorizontalAlignment = ShapeHorizontalAlignment.None;
myPicture.VerticalAlignment = ShapeVerticalAlignment.None;
// The picture's horizontal position is relative to the left margin.
myPicture.RelativeHorizontalPosition = ShapeRelativeHorizontalPosition.LeftMargin;
// The picture's vertical position is relative to the top margin.
myPicture.RelativeVerticalPosition = ShapeRelativeVerticalPosition.TopMargin;
// Specify the offset value.
myPicture.Offset = new System.Drawing.PointF(4.5f, 2.0f);
}
#endregion #FloatingPictureOffset
}
static void ChangeZorderAndWrapping(RichEditDocumentServer wordProcessor)
{
#region #ChangeZorderAndWrapping
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
// Access a document.
Document document = wordProcessor.Document;
if (document.Shapes.Count > 1)
{
// Access a picture.
Shape myPicture = document.Shapes[1];
// Align the picture vertically.
myPicture.VerticalAlignment = ShapeVerticalAlignment.Top;
// Specify the picture position in the z-order.
myPicture.ZOrder = document.Shapes[0].ZOrder - 1;
// Display document text over the picture.
myPicture.TextWrapping = TextWrappingType.BehindText;
}
#endregion #ChangeZorderAndWrapping
}
static void AddTextBox(RichEditDocumentServer wordProcessor)
{
#region #AddTextBox
// Access a document.
Document document = wordProcessor.Document;
// Append text to the document.
document.AppendText("Line One\nLine Two\nLine Three");
// Insert a text box at the specified position.
Shape myTextBox = document.Shapes.InsertTextBox(document.CreatePosition(15));
// Align the text box horizontally.
myTextBox.HorizontalAlignment = ShapeHorizontalAlignment.Center;
// Specify the text box background color.
myTextBox.Fill.Color = System.Drawing.Color.WhiteSmoke;
// Draw a border around the text box.
myTextBox.Line.Color = System.Drawing.Color.Black;
myTextBox.Line.Thickness = 1;
// Modify text box content.
SubDocument textBoxDocument = myTextBox.ShapeFormat.TextBox.Document;
textBoxDocument.AppendText("TextBox Text");
// Format the boxed text.
CharacterProperties cp = textBoxDocument.BeginUpdateCharacters(textBoxDocument.Range.Start, 7);
cp.ForeColor = System.Drawing.Color.Orange;
cp.FontSize = 24;
textBoxDocument.EndUpdateCharacters(cp);
#endregion #AddTextBox
}
static void InsertRichTextInTextBox(RichEditDocumentServer wordProcessor)
{
#region #InsertRichTextInTextBox
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
// Access a document.
Document document = wordProcessor.Document;
// Access a text box.
Shape myTextBox = document.Shapes[0];
// Allow text box resize to fit contents.
myTextBox.ShapeFormat.TextBox.HeightRule = TextBoxSizeRule.Auto;
SubDocument boxedDocument = myTextBox.ShapeFormat.TextBox.Document;
int appendPosition = myTextBox.ShapeFormat.TextBox.Document.Range.End.ToInt();
// Append the second paragraph of the main document to the boxed text.
DocumentRange newRange = boxedDocument.AppendDocumentContent(document.Paragraphs[1].Range);
boxedDocument.Paragraphs.Insert(newRange.Start);
// Insert an image form the main document into the text box.
boxedDocument.Images.Insert(boxedDocument.CreatePosition(appendPosition), document.Images[0].Image.NativeImage);
// Resize the image so that its size equals the image in the main document.
boxedDocument.Images[0].Size = document.Images[0].Size;
#endregion #InsertRichTextInTextBox
}
static void RotateAndResize(RichEditDocumentServer wordProcessor)
{
#region #RotateAndResize
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
// Access a document.
Document document = wordProcessor.Document;
// Check all shapes in the document.
foreach (Shape s in document.Shapes)
{
// Rotate pictures.
if (s.Type == ShapeType.Picture)
{
s.RotationAngle = 45;
}
// Resize text boxes.
else
{
s.ScaleX = 0.1f;
s.ScaleY = 0.1f;
}
}
#endregion #RotateAndResize
}
}
}