Hi Saleem,
Thanks for your inquiry. The shared VBA code inserts the shape (image) in the document and group the shapes If more than one images are inserted.
You can insert an image into document using DocumentBuilder.InsertImage method. I suggest you please read the overloaded methods of DocumentBuilder.InsertImage from here:
Document doc = new
Document(@“C:\test\in.docx”);
GroupShape gs = new GroupShape(doc);
double left = double.MaxValue;
double right = double.MinValue;
double top = double.MaxValue;
double bottom = double.MinValue;
Node[] shapes = doc.GetChildNodes(NodeType.Shape, true).ToArray();
foreach (Shape s in shapes)
{
gs.AppendChild(s);
left = s.Left < left ? s.Left : left;
right = s.Right > right ? s.Right : right;
top = s.Top < top ? s.Top : top;
bottom = s.Bottom > bottom ? s.Bottom : bottom;
}
double width =right - left;
double height = bottom - top;
gs.Width = width;
gs.Height = height;
gs.CoordSize = new Size((int)width, (int)height);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(gs);
doc.Save(@“C:\test\out.docx”);
Thanks a lot for your quick reply
I’ll check this code out and see how ti works for me.
Hi Tahir.
Imports Aspose.Words.Drawing
Imports Aspose.Words
Imports System.DrawingPublic Class Class1
<span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> DiagramDraw(<span style="color:blue;">ByVal</span> mapset <span style="color:blue;">As</span> <span style="color:#2b91af;">NPMap</span>, <span style="color:blue;">ByVal</span> Pic_Bookmark <span style="color:blue;">As</span> <span style="color:blue;">String</span>, <span style="color:blue;">Optional</span> <span style="color:blue;">ByVal</span> Pic_Scale <span style="color:blue;">As</span> <span style="color:blue;">Double</span> = 1.0#) <span style="color:blue;">Dim</span> i <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> <span style="color:blue;">Dim</span> pic <span style="color:blue;">As</span> System.Drawing.<span style="color:#2b91af;">Image</span> <span style="color:green;">'Add the Map</span> pic = System.Drawing.<span style="color:#2b91af;">Bitmap</span>.FromFile(mapset.DiagramFileName) <span style="color:blue;">If</span> gs <span style="color:blue;">Is</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span> gs = <span style="color:blue;">New</span> <span style="color:#2b91af;">GroupShape</span>(docReport) gs.WrapType = <span style="color:#2b91af;">WrapType</span>.Tight gs.RelativeHorizontalPosition = <span style="color:#2b91af;">RelativeHorizontalPosition</span>.Page gs.RelativeVerticalPosition = <span style="color:#2b91af;">RelativeVerticalPosition</span>.Page gs.VerticalAlignment = <span style="color:#2b91af;">VerticalAlignment</span>.Center gs.HorizontalAlignment = <span style="color:#2b91af;">HorizontalAlignment</span>.Center gs.Left = 0 gs.Top = 0 gs.Height = (pic.Height / pic.VerticalResolution) * 72 gs.Width = (pic.Width / pic.HorizontalResolution) * 72 gs.CoordOrigin = <span style="color:blue;">New</span> <span style="color:#2b91af;">Point</span>(0, 0) gs.CoordSize = <span style="color:blue;">New</span> <span style="color:#2b91af;">Size</span>(gs.Width, gs.Height) gs.AnchorLocked = <span style="color:blue;">True</span> builder.MoveToBookmark(Pic_Bookmark) <span style="color:blue;">Dim</span> DiagramShape <span style="color:blue;">As</span> <span style="color:#2b91af;">Shape</span> = builder.InsertImage(mapset.DiagramFileName) gs.AppendChild(DiagramShape) <span style="color:green;">'Add the Annotations</span> <span style="color:blue;">For</span> i = 1 <span style="color:blue;">To</span> UBound(mapset.mapsite) addAnnotation() <span style="color:blue;">Next</span> i gs.Height = gs.Height * Pic_Scale gs.Width = gs.Width * Pic_Scale builder.MoveToBookmark(Pic_Bookmark) builder.InsertNode(gs) <span style="color:blue;">End</span> <span style="color:blue;">Sub</span> <span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> addAnnotation() <span style="color:blue;">Dim</span> EffectShape <span style="color:blue;">As</span> <span style="color:#2b91af;">Shape</span> = <span style="color:blue;">Nothing</span> <span style="color:blue;">Dim</span> EffectShape2 <span style="color:blue;">As</span> <span style="color:#2b91af;">Shape</span> <span style="color:blue;">Dim</span> TextShape <span style="color:blue;">As</span> <span style="color:#2b91af;">Shape</span> <span style="color:blue;">Dim</span> str <span style="color:blue;">As</span> <span style="color:blue;">String</span> = <span style="color:#a31515;">"GS: 2+3=5"</span> & ChrW(11) & <span style="color:#a31515;">"PTI: 50%"</span> TextShape = <span style="color:blue;">New</span> <span style="color:#2b91af;">Shape</span>(docReport, <span style="color:#2b91af;">ShapeType</span>.TextBox) TextShape.Left = 23 TextShape.Top = 38.75 TextShape.Width = 46.75 TextShape.Height = 26.25 <span style="color:green;">'****** I did'nt find how to do these in ASPOSE ******</span> <span style="color:green;">'TextShape.Fill.Transparency = 1</span> <span style="color:green;">'TextShape.line.Visible = msoFalse</span> <span style="color:blue;">Dim</span> para <span style="color:blue;">As</span> <span style="color:#2b91af;">Paragraph</span> = <span style="color:blue;">New</span> <span style="color:#2b91af;">Paragraph</span>(docReport) <span style="color:blue;">Dim</span> rn <span style="color:blue;">As</span> <span style="color:blue;">New</span> <span style="color:#2b91af;">Run</span>(docReport, str) rn.Font.Size = 6 rn.Font.Name = <span style="color:#a31515;">"Times New Roman"</span> para.AppendChild(rn) TextShape.AppendChild(para) <span style="color:green;">'****** I did'nt find how to do these in ASPOSE ******</span> <span style="color:green;">'TextShape.TextFrame.TextRange.Font.Size = mapsite.fontSize</span> <span style="color:green;">'TextShape.TextFrame.TextRange.Font.Bold = False</span> <span style="color:green;">'TextShape.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphCenter</span> TextShape.ZOrder = 1 TextShape.TextBox.FitShapeToText = <span style="color:blue;">True</span> gs.AppendChild(TextShape) EffectShape = <span style="color:blue;">New</span> <span style="color:#2b91af;">Shape</span>(docReport, <span style="color:#2b91af;">ShapeType</span>.Rectangle) EffectShape.Left = 33 EffectShape.Top = 33.75 EffectShape.Width = 35 EffectShape.Height = 10 DefaultShapeFormat(EffectShape, <span style="color:#a31515;">"Red"</span>) EffectShape2 = <span style="color:blue;">New</span> <span style="color:#2b91af;">Shape</span>(docReport, <span style="color:#2b91af;">ShapeType</span>.Rectangle) EffectShape2.Left = 50.5 EffectShape2.Top = 33.75 EffectShape2.Width = 17.5 EffectShape2.Height = 8.75 DefaultShapeFormat(EffectShape2, <span style="color:#a31515;">"Blue"</span>) gs.AppendChild(EffectShape2) <span style="color:blue;">End</span> <span style="color:blue;">Sub</span> <span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> DefaultShapeFormat(shpAnno <span style="color:blue;">As</span> <span style="color:#2b91af;">Shape</span>, lngcolor <span style="color:blue;">As</span> <span style="color:blue;">String</span>) <span style="color:blue;">With</span> shpAnno .FillColor = System.Drawing.<span style="color:#2b91af;">Color</span>.FromName(lngcolor) .Font.Color = <span style="color:#2b91af;">Color</span>.Black .WrapType = <span style="color:#2b91af;">WrapType</span>.Square .StrokeWeight = 0.75 <span style="color:blue;">End</span> <span style="color:blue;">With</span> <span style="color:blue;">End</span> <span style="color:blue;">Sub</span>
End Class
Thanks,
Saleem
Hi Saleem,
Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.
- Please attach your input Word document.
- Please
create a standalone/runnable simple application (for example a Console
Application Project) that demonstrates the code (Aspose.Words code) you used to generate
your output document - Please attach the output Word file that shows the undesired behavior.
- Please
attach your target Word document showing the desired behavior. You can
use Microsoft Word to create your target Word document. I will
investigate as to how you are expecting your final document be generated
like. It seems that Result.docx is your expected document.
us we’ll start our investigation into your issue.
Hi Tahir,
So I will not make you waste your time on it anymore.
Thanks again.
Hi Saleem,
Thanks for your feedback. It is nice to hear from you that you have found the solution of your issues. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.