与文档分开呈现形状
在处理文档时,一个常见的任务是提取文档中找到的所有图像并将其导出到外部位置。 使用Aspose.WordsAPI,此任务变得简单,它已经提供了提取和保存图像数据的功能。 但是,有时您可能希望类似地提取由不同类型的绘图对象表示的其他类型的图形内容,例如,包含段落,箭头形状和小图像的文本框。 没有直接的方法来呈现这个对象,因为它是单个内容元素的组合。 您可能还会遇到这样的情况,即内容已组合在一起,成为看起来像单个图像的对象。
Aspose.Words提供了提取此类内容的功能,就像从shape中提取简单图像作为渲染内容一样。 本文介绍如何利用此功能独立于文档呈现形状。
Aspose.Words中的形状类型
文档绘图层中的所有内容都由Aspose.Words文档对象模块(DOM)中的Shape或GroupShape节点表示。 这样的内容可以是文本框,图像,AutoShapes,OLE对象等。 某些字段也导入为形状,例如INCLUDEPICTURE
字段。
一个简单的图像由ShapeType.Image的Shape节点表示。 此形状节点没有子节点,但此形状节点中包含的图像数据可以通过Shape.ImageData属性访问。 另一方面,一个形状也可以由许多子节点组成。 例如,由ShapeType.TextBox属性表示的文本框形状可以由许多节点组成,例如Paragraph和Table。 大多数形状可以包括Paragraph和Table块级节点。 这些节点与主体中出现的节点相同。 形状总是某个段落的一部分,可以直接内联或锚定到Paragraph,,但"浮动"在文档页面的任何地方。
文档还可以包含组合在一起的形状。 通过选择多个对象并单击右键菜单中的"组",可以在Microsoft Word中启用分组。
在Aspose.Words中,这些形状组由GroupShape节点表示。 这些也可以以相同的方式调用,以将整个组呈现给图像。
DOCX格式可以包含特殊类型的图像,如图表或图表。 这些形状也通过Aspose.Words中的Shape节点表示,这也提供了将它们渲染为图像的类似方法。 根据设计,一个形状不能包含另一个作为子形状的形状,除非该形状是一个图像(ShapeType.Image)。 例如,Microsoft Word不允许您在另一个文本框中插入文本框。
上面描述的形状类型提供了一种特殊的方法来通过ShapeRenderer类呈现形状。 通过GetShapeRenderer方法或将Shape传递给ShapeRenderer类的构造函数,为Shape或GroupShape检索ShapeRenderer类的实例。 此类提供对成员的访问,这些成员允许将形状呈现为以下内容:
- 使用Save方法重载的磁盘上的文件
- 使用Save方法重载的流
- 通过使用RenderToSize和RenderToScale方法图形对象
渲染到文件或流
Save方法提供将形状直接呈现到文件或流的重载。 两个重载都接受ImageSaveOptions类的实例,该实例允许定义用于呈现形状的选项。 这与Document.Save方法的工作方式相同。 即使此参数是必需的,您也可以传递null值,指定没有自定义选项。
形状可以以SaveFormat枚举中指定的任何图像格式导出。 例如,图像可以通过指定SaveFormat.Jpeg枚举呈现为光栅图像,例如JPEG,或者通过指定SaveFormat.Emf呈现为矢量图像,例如EMF。
下面的代码示例演示将形状与文档分开呈现为EMF图像,并保存到磁盘:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
ShapeRenderer r = shape.getShapeRenderer(); | |
// Define custom options which control how the image is rendered. Render the | |
// shape to the JPEG raster format. | |
ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.EMF); | |
imageOptions.setScale(1.5f); | |
// Save the rendered image to disk. | |
r.save(dataDir + "TestFile.RenderToDisk_Out.emf", imageOptions); |
下面的代码示例说明了将形状与文档分开呈现为JPEG图像,并将其保存为流:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
ShapeRenderer r = new ShapeRenderer(shape); | |
// Define custom options which control how the image is rendered. Render the | |
// shape to the vector format EMF. | |
ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.JPEG); | |
// Output the image in gray scale | |
imageOptions.setImageColorMode(ImageColorMode.GRAYSCALE); | |
// Reduce the brightness a bit (default is 0.5f). | |
imageOptions.setImageBrightness(0.45f); | |
FileOutputStream stream = new FileOutputStream(dataDir + "TestFile.RenderToStream_Out.jpg"); | |
// Save the rendered image to the stream using different options. | |
r.save(stream, imageOptions); |
ImageSaveOptions类允许您指定控制图像呈现方式的各种选项。 上面描述的功能可以以相同的方式应用于GroupShape和Shape节点。
渲染到Graphics
对象
直接呈现到Graphics对象允许您定义自己的设置和Graphics对象的状态。 一个常见的场景是将形状直接呈现到从Windows表单或位图中检索到的Graphics对象中。 渲染Shape节点时,设置将影响形状外观。 例如,可以对Graphics对象使用RotateTransform或ScaleTransform方法来旋转或缩放形状。
下面的示例演示如何将形状与文档分开呈现到Graphics对象,并将旋转应用于呈现的图像:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
// The shape renderer is retrieved using this method. This is made into a | |
// separate object from the shape as it internally | |
// caches the rendered shape. | |
ShapeRenderer r = shape.getShapeRenderer(); | |
// Find the size that the shape will be rendered to at the specified scale and | |
// resolution. | |
Dimension shapeSizeInPixels = r.getSizeInPixels(1.0f, 96.0f); | |
// Rotating the shape may result in clipping as the image canvas is too small. | |
// Find the longest side | |
// and make sure that the graphics canvas is large enough to compensate for | |
// this. | |
int maxSide = Math.max(shapeSizeInPixels.width, shapeSizeInPixels.height); | |
BufferedImage image = new BufferedImage((int) (maxSide * 1.25), (int) (maxSide * 1.25), | |
BufferedImage.TYPE_INT_ARGB); | |
// Rendering to a graphics object means we can specify settings and | |
// transformations to be applied to | |
// the shape that is rendered. In our case we will rotate the rendered shape. | |
Graphics2D gr = (Graphics2D) image.getGraphics(); | |
// Clear the shape with the background color of the document. | |
gr.setBackground(shape.getDocument().getPageColor()); | |
gr.clearRect(0, 0, image.getWidth(), image.getHeight()); | |
// Center the rotation using translation method below | |
gr.translate(image.getWidth() / 8, image.getHeight() / 2); | |
// Rotate the image by 45 degrees. | |
gr.rotate(45 * Math.PI / 180); | |
// Undo the translation. | |
gr.translate(-image.getWidth() / 8, -image.getHeight() / 2); | |
// Render the shape onto the graphics object. | |
r.renderToSize(gr, 0, 0, shapeSizeInPixels.width, shapeSizeInPixels.height); | |
ImageIO.write(image, "png", new File(dataDir + "TestFile.RenderToGraphics_out.png")); | |
gr.dispose(); |
类似地,与RenderToSize方法一样,从NodeRendererBase继承的RenderToSize方法对于创建文档内容的缩略图很有用。 形状大小通过构造函数指定。 RenderToSize方法接受Graphics对象,图像位置的X和Y坐标,以及将绘制到Graphics对象上的图像的大小(宽度和高度)。
可以使用从NodeRendererBase类继承的ShapeRenderer.RenderToScale方法将Shape呈现为一定的比例。 这类似于接受相同主要参数的Document.RenderToScale方法。 这两种方法之间的区别在于,使用ShapeRenderer.RenderToScale方法,而不是文字大小,您可以选择在渲染期间缩放形状的float值。 如果float值等于1.0,则形状将以其原始大小的100%呈现。 Float值0.5会将图像大小减少一半。
渲染形状图像
Shape类表示绘图层中的对象,例如AutoShape、文本框、自由格式、OLE对象、ActiveX控件或图片。 使用Shape类,您可以在Microsoft Word文档中创建或修改形状。 形状的一个重要属性是它的ShapeType。 不同类型的形状在Word文档中可以具有不同的功能。 例如,只有图像和OLE形状可以在其中包含图像,而大多数形状只能包含文本。
下面的示例演示如何将形状图像与文档分开呈现为JPEG图像并将其保存到磁盘:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
dataDir = dataDir + "TestFile.RenderShape_out.jpg"; | |
// Save the Shape image to disk in JPEG format and using default options. | |
shape.getShapeRenderer().save(dataDir, null); |
检索形状大小
ShapeRenderer类还提供了通过GetSizeInPixels方法以像素为单位检索形状大小的功能。 此方法接受两个float(单个)参数-scale和DPI,它们用于在渲染形状时计算形状大小。 该方法返回Size对象,该对象包含计算大小的宽度和高度。 当需要预先知道渲染形状的大小时,这很有用,例如在从渲染输出创建新位图时。
下面的示例演示如何使用要渲染的形状的宽度和高度创建新的位图和图形对象:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
Point2D.Float shapeSizeInDocument = shape.getShapeRenderer().getSizeInPoints(); | |
float width = shapeSizeInDocument.x; // The width of the shape. | |
float height = shapeSizeInDocument.y; // The height of the shape. | |
Dimension shapeRenderedSize = shape.getShapeRenderer().getSizeInPixels(1.0f, 96.0f); | |
BufferedImage image = new BufferedImage(shapeRenderedSize.width, shapeRenderedSize.height, | |
BufferedImage.TYPE_INT_RGB); | |
Graphics gr = image.getGraphics(); | |
// Render shape onto the graphics object using the RenderToScale or RenderToSize | |
// methods of ShapeRenderer class. | |
gr.dispose(); |
使用RenderToSize或RenderToScale方法时,渲染的图像大小也会在SizeF对象中返回。 这可以分配给一个变量,并在必要时使用。
SizeInPoints属性返回以点为单位测量的形状大小(请参阅ShapeRenderer)。 结果是包含宽度和高度的SizeF
对象。