我希望在我的文档中使用图像来填充、填充我的页面区域。
我看到有 IDOMImage
和 IDOMImageBrush
,但我不确定如何使用它们来缩放和平铺我的源图像。
如何使用 Mako SDK 执行此操作?
最佳答案
Mako 可以将图像平铺到给定区域,还可以翻转交替的平铺以创建图案。使用缩放变换来控制其大小。此代码向您展示了如何操作。
// Declare an output pointer
IOutputPtr output;
// Create new assembly, document and page
IDocumentAssemblyPtr assembly = IDocumentAssembly::create(jawsMako);
IDocumentPtr document = IDocument::create(jawsMako);
IPagePtr page = IPage::create(jawsMako);
// Add the page to the document, and the document to the assembly
document->appendPage(page);
assembly->appendDocument(document);
// Create a fixed page to work with
double pageWidth = 10 * 96.0;
double pageHeight = 20 * 96.0;
IDOMFixedPagePtr fixedPage = IDOMFixedPage::create(jawsMako, pageWidth, pageHeight);
// Load the image file into an image
IDOMImagePtr image = IDOMJPEGImage::create(jawsMako, IInputStream::createFromFile(jawsMako, imageFilePath));
// Find its dimensions
IImageFramePtr frame;
image->getFirstImageFrame(jawsMako, frame);
double imageWidth = frame->getWidth();
double imageHeight = frame->getHeight();
// Create a rect to hold the image
FRect printBounds(0.0, 0.0, pageWidth, pageHeight);
// Create a transformation matrix to scale the image, taking into account the page proportions
// Scaling factor is a float ranging from 0.0 to 1.0
double pageWidthHeightRatio = pageWidth / pageHeight;
FMatrix transform;
transform.scale(scalingFactor, scalingFactor * pageWidthHeightRatio);
// Stick the image in a brush
IDOMBrushPtr imageBrush = IDOMImageBrush::create(jawsMako, image, FRect(), printBounds, transform, 1.0, eFlipXY);
// And now create a path using the image brush
IDOMPathNodePtr path = IDOMPathNode::createFilled(jawsMako, IDOMPathGeometry::create(jawsMako, printBounds), imageBrush);
// Add the path to the fixed page
fixedPage->appendChild(path);
// This becomes the page contents
page->setContent(fixedPage);
// Write to the output
output = IPDFOutput::create(jawsMako);
output->writeAssembly(assembly, outputFilePath);
使用此代码和此图片:
产生了这个耕作图像:
该代码使用枚举 eTileXY。这些是可用的平铺选项:
eTilingMode 平铺模式类型枚举。
eNoTile
没有平铺。如果要绘制的区域比图像大,只需绘制一次图像(在画笔视口(viewport)指定的位置),其余区域保持透明。
电子瓷砖
平铺图像而不翻转或旋转图像。在这种模式下平铺时,由对角之间的单条对角线组成的正方形图像会产生对角线。
eFlipX
平铺图像,使交替的平铺列水平翻转。在此模式下平铺时,由对角之间的单条对角线组成的正方形图像会在整个区域产生水平延伸的人字形。
eFlipY
平铺图像,使交替的平铺行垂直翻转。在此模式下平铺时,由对角之间的单条对角线组成的正方形图像会产生垂直穿过该区域的人字形。
eFlipXY
平铺图像,这样交替的瓷砖列被水平翻转并且交替的瓷砖行被垂直翻转。在此模式下平铺时,由对角之间的单条对角线组成的正方形图像将生成在其点上平衡的正方形网格。
关于c++ - 如何使用 Mako SDK 在文档页面的给定区域内平铺图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54887883/