最近在弄自己工作上的一个小工具,大概流程就是:我们会拿到一张含有二维码的word文档,这一张word文档等于时一个模板,右下角会有一个二维码,代表着不同的人,从001-032这种格式,同时每张word中的表格内人名也是不同的,以往的流程时:复制出来需要数量的文档副本,更改文件名字,按照001-032这样排序,然后替换掉内部的001二维码为002-032这样子,同时还要把表格内的人名替换掉,需要和二维码对应起来,于是就有了这款小工具(目前还在测试中,目前还没有投入实际使用中)。
001写这个贴子的目的
当初在着手开发的时候去找Aspose.words获取图片指定位置的时候,在各大博客和论坛都没找到,甚至去了stackoverflow寻找答案,依然没有找到,后面是自己摸索了一个下午才知道怎么样去做的,也希望将来有同样需求的人会搜索到我的贴子,也希望可以帮到他。
002原理
写的小工具原理很简单,加载Word之后寻找word内的二维码图片,找到以后获取图片的Bounds信息。
在Apose的官方文档中给出了一个在指定位置插入图片的Example,所以我觉得应该也是支持提取一个图片的所在位置的
于是我就开始搜索Aspose的Api参考,锁定了这三个属性,其描述就是为一个Shape的位置和大小,所以猜测着三个属性就是图片的位置信息,只是三个属性使用的单位不同,最终我的测试结果锁定了第一个Bounds属性,因为根据这个属性取出来并且插入的二维码位置是正确的。
扫一眼Bounds属性的简介,首先看一下他的结构信息
public RectangleF Bounds { get; set; }
可以看出是一个RectangleF类型,接着看一下官方给的Demo:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertShape(ShapeType.Line, RelativeHorizontalPosition.LeftMargin, 50,
RelativeVerticalPosition.TopMargin, 50, 100, 100, WrapType.None);
shape.StrokeColor = Color.Orange;
// Even though the line itself takes up little space on the document page,
// it occupies a rectangular containing block, the size of which we can determine using the "Bounds" properties.
Assert.AreEqual(new RectangleF(50, 50, 100, 100), shape.Bounds);
Assert.AreEqual(new RectangleF(50, 50, 100, 100), shape.BoundsInPoints);
// Create a group shape, and then set the size of its containing block using the "Bounds" property.
GroupShape group = new GroupShape(doc);
group.Bounds = new RectangleF(0, 100, 250, 250);
Assert.AreEqual(new RectangleF(0, 100, 250, 250), group.BoundsInPoints);
// Create a rectangle, verify the size of its bounding block, and then add it to the group shape.
shape = new Shape(doc, ShapeType.Rectangle)
{
Width = 100,
Height = 100,
Left = 700,
Top = 700
};
Assert.AreEqual(new RectangleF(700, 700, 100, 100), shape.BoundsInPoints);
group.AppendChild(shape);
// The group shape's coordinate plane has its origin on the top left-hand side corner of its containing block,
// and the x and y coordinates of (1000, 1000) on the bottom right-hand side corner.
// Our group shape is 250x250pt in size, so every 4pt on the group shape's coordinate plane
// translates to 1pt in the document body's coordinate plane.
// Every shape that we insert will also shrink in size by a factor of 4.
// The change in the shape's "BoundsInPoints" property will reflect this.
Assert.AreEqual(new RectangleF(175, 275, 25, 25), shape.BoundsInPoints);
doc.FirstSection.Body.FirstParagraph.AppendChild(group);
// Insert a shape and place it outside of the bounds of the group shape's containing block.
shape = new Shape(doc, ShapeType.Rectangle)
{
Width = 100,
Height = 100,
Left = 1000,
Top = 1000
};
group.AppendChild(shape);
// The group shape's footprint in the document body has increased, but the containing block remains the same.
Assert.AreEqual(new RectangleF(0, 100, 250, 250), group.BoundsInPoints);
Assert.AreEqual(new RectangleF(250, 350, 25, 25), shape.BoundsInPoints);
doc.Save(ArtifactsDir + &