This demo uses the DevExpress Word Processing Document API to generate drawing objects (shapes and pictures) in a new document. Select the shape type: picture, text box, figure, or a figure group. Specify associated parameters. Use the Appearance Settings section to specify color and outline options for the shape.
Click Generate Shapes and Save as… to select an output format, generate a document, and download the result.
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
Stream GenerateDocumentWithImage(Stream imageStream, float scaleFactor, bool lockAspectRatio,
bool shouldCrop, float cropLeft, float cropRight, float cropTop, float cropBottom,
Color outlineColor, float outlineWidth, DocumentFormat outputFormat) {
using var wordProcessor = new RichEditDocumentServer();
var document = wordProcessor.Document;
// Set document unit to inches for precise sizing
document.Unit = DevExpress.Office.DocumentUnit.Inch;
var shape = document.Shapes.InsertPicture(document.Range.End, DocumentImageSource.FromStream(imageStream));
shape.LockAspectRatio = lockAspectRatio;
shape.TextWrapping = TextWrappingType.InLineWithText;
shape.ScaleX = scaleFactor;
shape.ScaleY = scaleFactor;
if(shouldCrop) {
RectangleOffset cropOffset = new RectangleOffset(cropLeft, cropTop, cropRight, cropBottom);
shape.PictureFormat.SourceRect = cropOffset;
}
shape.Line.Color = outlineColor;
shape.Line.Thickness = outlineWidth;
// Create output stream and save document in target format
return CreateOutputStream(wordProcessor, outputFormat);
}
Stream GenerateDocumentWithTextbox(string text, float width, float height, bool lockAspectRatio,
Color fillColor, Color outlineColor, float outlineWidth, DocumentFormat outputFormat) {
using var wordProcessor = new RichEditDocumentServer();
var document = wordProcessor.Document;
// Set document unit to inches for precise sizing
document.Unit = DevExpress.Office.DocumentUnit.Inch;
var shape = document.Shapes.InsertTextBox(document.Range.End);
shape.ShapeFormat.TextBox.Document.AppendText(text);
shape.TextWrapping = TextWrappingType.InLineWithText;
FormatShape(shape, width, height, lockAspectRatio, outlineWidth, outlineColor, fillColor);
// Create output stream and save document in target format
return CreateOutputStream(wordProcessor, outputFormat);
}
Stream GenerateDocumentWithFigure(ShapeGeometryPreset geometry, float width, float height, bool lockAspectRatio,
Color fillColor, Color outlineColor, float outlineWidth, DocumentFormat outputFormat) {
using var wordProcessor = new RichEditDocumentServer();
var document = wordProcessor.Document;
// Set document unit to inches for precise sizing
document.Unit = DevExpress.Office.DocumentUnit.Inch;
var shape = document.Shapes.InsertShape(document.Range.End, geometry);
FormatShape(shape, width, height, lockAspectRatio, outlineWidth, outlineColor, fillColor);
// Create output stream and save document in target format
return CreateOutputStream(wordProcessor, outputFormat);
}
Stream GenerateDocumentWithShapeGroup(ShapeGeometryPreset geometry, int numberOfShapes, float width, float height,
bool lockAspectRatio, Color fillColor, Color outlineColor, float outlineWidth, DocumentFormat outputFormat) {
using var wordProcessor = new RichEditDocumentServer();
var document = wordProcessor.Document;
// Set document unit to inches for precise sizing
document.Unit = DevExpress.Office.DocumentUnit.Inch;
var group = document.Shapes.InsertGroup(document.Range.End);
group.TextWrapping = TextWrappingType.InLineWithText;
var groupItems = group.GroupItems;
for(int i = 0; i < numberOfShapes; i++) {
var shape = groupItems.AddShape(geometry, new RectangleF(i * 0.5f, i * 0.5f, width, height));
FormatShape(shape, width, height, lockAspectRatio, outlineWidth, outlineColor, fillColor);
}
// Create output stream and save document in target format
return CreateOutputStream(wordProcessor, outputFormat);
}
void FormatShape(DrawingObject shape, float width, float height, bool lockAspectRatio, float outlineWidthPoints, Color outlineColor, Color fillColor) {
shape.LockAspectRatio = lockAspectRatio;
shape.Width = width;
shape.Height = height;
shape.Line.Color = outlineColor;
shape.Line.Thickness = outlineWidthPoints;
shape.Fill.Color = fillColor;
}
Stream CreateOutputStream(RichEditDocumentServer wordProcessor, DocumentFormat outputFormat) {
var outputStream = new MemoryStream();
// Save document in target format
wordProcessor.SaveDocument(outputStream, outputFormat);
outputStream.Seek(0, SeekOrigin.Begin);
return outputStream;
}