吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2212|回复: 2
收起左侧

[其他转载] 使用Aspose.words根据图片位置批量生成新的文档

  [复制链接]
jidesheng6 发表于 2021-6-4 20:48

最近在弄自己工作上的一个小工具,大概流程就是:我们会拿到一张含有二维码的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 + "Shape.Bounds.docx");

在Aspose.words中,每一个图片、或者图表这种信息,都是属于一个Shape,而且Shape中有一个属性叫做Hasimage,就是这个Shape中是否存在图片,所以只需要判断是否为图片,然后获取Bounds信息存到一个公共类中,之后在进行插入图片的时候,从公共类中取出位置信息,根据位置进行插入图片即可。

003开始编写代码

PublicDataArea.cs:在这里定义好将来会被其他类调用的变量,这些变量的值会在其他地方执行完成以后被写入,而且设计的时候需要按步骤来,否则按钮是被禁用的,所以可以减少判断null值的情况(不过也是自用,不太需要这个判断)

using Aspose.Words.Drawing;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MakeSignleDocumentByTemplate
{
    static class PublicDataArea
    {
        public struct FilesInfo
        {
            public static ArrayList Doc_FilesCollect;
            public static ArrayList BMP_FileCollect;
            public static float Template_XPoint;
            public static float Template_YPoint;
            public static float Template_Width;
            public static float Template_Height;
            public static RelativeHorizontalPosition Template_RH;
            public static RelativeVerticalPosition Template_RV;

        }
    }
}

获取文档中右下角二维码的位置信息(做模板):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using Aspose.Words;
using Aspose.Words.Drawing;
using System.Drawing;
using System.Collections;

namespace MakeSignleDocumentByTemplate
{
    class GetTemplateSignlePicPoints:MainForm//获取word中单人表的相对位置信息
    {
         private struct ErrorCode//错误代码
        {
            public static int PagesNumberError;//页数错误
            public static int PicNumberError;//图片数量错误
            public static int SignPicCountError;//单人码数量错误
            public static int ShapeCountError;//shape数量错误
        };
        public GetTemplateSignlePicPoints()
        {
            ErrorCode.PagesNumberError = -3;
            ErrorCode.PicNumberError = -4;
            ErrorCode.SignPicCountError = -5;
            ErrorCode.ShapeCountError = -6;
        }
        private Document GetDocumentObject()//返回一个Document对象
        {
            Stream TemplateStream = new SelectTemplateFile().GetFileNameStream();//获取模板数据流
            if (TemplateStream != null)
            {
                Document Template_Document = new Document(TemplateStream);
                if (Template_Document != null)
                {
                    TemplateStream.Close();//释放资源
                    return Template_Document;
                }
                else
                {
                    TemplateStream.Close();
                    return null;
                }
            }
            else
            {
                return null;
            }

        }
        public void GetSignlePicPoint(out ArrayList Return_List,out int errorcode)//获取表中单人码的图片
        {
            Document Template_D = GetDocumentObject();
            Return_List = new ArrayList();
            errorcode = 0;
            int ToatlImageCount = 0;
            if (Template_D != null)
            {
                NodeCollection Template_Shapes = Template_D.GetChildNodes(NodeType.Shape, true);
                int PageCount = Template_D.PageCount;//获取页数
                //Debug.WriteLine(PageCount);
                int SignPicCount = 0;
                if (PageCount <= 1)//处理一页
                {
                    if (Template_Shapes.Count > 0)
                    {
                        foreach (Shape T_Shape in Template_Shapes)
                        {

                            if (T_Shape.HasImage)
                            {

                                ToatlImageCount++;//获取图片总数
                                Image Temp_Pic = T_Shape.ImageData.ToImage();
                                Bitmap Temp_Bitmap = new Bitmap(Temp_Pic);
                                int BitMap_Width = Temp_Pic.Width;
                                int BitMap_Height = Temp_Pic.Height;
                                if ((BitMap_Width == 196 && BitMap_Height == 256) || (BitMap_Width == 108 && BitMap_Height == 141))
                                {
                                        SignPicCount++;//此处探寻单人码图片并且计算
                                        RectangleF SignlePicRect = T_Shape.Bounds;
                                        RelativeHorizontalPosition R_H = T_Shape.RelativeHorizontalPosition;//水平相对位置方式
                                        RelativeVerticalPosition R_V = T_Shape.RelativeVerticalPosition;//垂直相对位置方式
                                        Return_List.Insert(0,SignlePicRect);
                                        Return_List.Insert(1,R_H);
                                        Return_List.Insert(2,R_V);
                                }

                            }
                        }
                        Debug.WriteLine(SignPicCount);
                        if (SignPicCount != 1)
                        {
                            errorcode = ErrorCode.SignPicCountError;
                            Return_List = null;
                            //单人码数量错误
                        }
                        else if (ToatlImageCount < 3)
                        {
                            errorcode = ErrorCode.PicNumberError;
                            Return_List = null;
                        }
                    }
                    else
                    {
                        errorcode = ErrorCode.ShapeCountError;
                        Return_List = null;
                        //处理没有shape的情况
                    }

                }
                else
                {
                    errorcode = ErrorCode.PagesNumberError;
                    Return_List = null;
                    //多于一页的情况
                }

            }
            else
            {
                Return_List = null;
                errorcode = -1;
            }

        }
    }
}

接着根据模板信息将模板的位置等等写入公共类中:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using Aspose.Words.Drawing;

namespace MakeSignleDocumentByTemplate
{
    class GetTemplateInfo:MainForm
    {
        public void GetStart()
        {
            ArrayList TempaltePicPointsInfo;
            int TemplateErrorCode;
            new GetTemplateSignlePicPoints().GetSignlePicPoint(out TempaltePicPointsInfo, out TemplateErrorCode);
            if (TempaltePicPointsInfo == null)
            {
                //M_Frm.StatusLabel.ForeColor = Color.Red;
                switch (TemplateErrorCode)
                {
                    case -3://页数错误
                        //M_Frm.StatusLabel.Text = "模板页数不正确,只能选择单页的文档进行操作";
                        MessageBox.Show("模板页数不正确,只能选择单页的文档进行操作","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                        break;
                    case -4://图片数量错误
                        //M_Frm.StatusLabel.Text = "图片数量不正确,请检查是否不符合单人表标准";
                        MessageBox.Show("图片数量不正确,请检查是否不符合单人表标准", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    case -5://单人码数量错误
                        //M_Frm.StatusLabel.Text = "单人码数量错误,请检查";
                        MessageBox.Show("单人码数量错误,请检查", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    case -6://没有图片
                        //M_Frm.StatusLabel.Text = "所选模板中没有图片";
                        MessageBox.Show("所选模板中没有图片", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    case -1:
                        break;
                    default:
                        //M_Frm.StatusLabel.Text = "遇到未定义错误,可能是出现bug";
                        MessageBox.Show("遇到未定义错误,可能是出现bug", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                }
            }
            else
            {
                //M_Frm.StatusLabel.ForeColor = Color.DarkGreen;
                RectangleF TemplateRectangle = (RectangleF)TempaltePicPointsInfo[0];
                RelativeHorizontalPosition RH_T_Pic = (RelativeHorizontalPosition)TempaltePicPointsInfo[1];//对齐方式
                RelativeVerticalPosition RV_T_Pic = (RelativeVerticalPosition)TempaltePicPointsInfo[2];//对齐方式
                M_Frm.TemplateCheckBox.Checked = true;
                /***模板图片的位置和宽高信息(Word中)***/
                PublicDataArea.FilesInfo.Template_XPoint = TemplateRectangle.X;
                PublicDataArea.FilesInfo.Template_YPoint = TemplateRectangle.Y;
                PublicDataArea.FilesInfo.Template_Width = TemplateRectangle.Width;
                PublicDataArea.FilesInfo.Template_Height = TemplateRectangle.Height;
                PublicDataArea.FilesInfo.Template_RH = RH_T_Pic;
                PublicDataArea.FilesInfo.Template_RV = RV_T_Pic;

                //M_Frm.StatusLabel.Text = $"模板配置正确,单人码X轴坐标为:{X_Point}|Y轴坐标为{Y_Point},图片宽度为:{Template_Pic_Width}|高度为:{Template_Pic_Height}";
                M_Frm.ClickBtn2.Enabled = true;
            }
        }
    }
}

中间还有很多处理过程,我就不一一描述了,有了前面两部操作,就可以开始插入图片了,接下来就是读取位置信息,插入图片:

private void StartRun_Click(object sender, EventArgs e)
        {
            Search_Replace.RegexStr = SearchBox.Text;
            int status = -1;
            string Path__ = new SelectFloder().GetSaveFloder();
            if (Path__ != String.Empty)
            {
                for (int i = 0; i < PublicDataArea.FilesInfo.Doc_FilesCollect.Count; i++)
                {
                    Document Temp_Doc = new Document(PublicDataArea.FilesInfo.Doc_FilesCollect[i].ToString());//加载Word文档
                    DocumentBuilder DB = new DocumentBuilder(Temp_Doc);
                    string ReStr = Search_Replace.RegexStr;
                    switch (ReStr)
                    {
                        case "-3"://不进行内容替换
                            DB.InsertImage(PublicDataArea.FilesInfo.BMP_FileCollect[i].ToString(), PublicDataArea.FilesInfo.Template_RH, PublicDataArea.FilesInfo.Template_XPoint, PublicDataArea.FilesInfo.Template_RV, PublicDataArea.FilesInfo.Template_YPoint, PublicDataArea.FilesInfo.Template_Width, PublicDataArea.FilesInfo.Template_Height, WrapType.None);
                            Temp_Doc.Save(Path__ + "\\" + System.IO.Path.GetFileName(PublicDataArea.FilesInfo.Doc_FilesCollect[i].ToString()));
                            status = 0;
                            break;
                        default://进行内容替换
                            DB.InsertImage(PublicDataArea.FilesInfo.BMP_FileCollect[i].ToString(), PublicDataArea.FilesInfo.Template_RH, PublicDataArea.FilesInfo.Template_XPoint, PublicDataArea.FilesInfo.Template_RV, PublicDataArea.FilesInfo.Template_YPoint, PublicDataArea.FilesInfo.Template_Width, PublicDataArea.FilesInfo.Template_Height, WrapType.None);
                            Debug.WriteLine(Search_Replace.ReplaceStrs[i]);
                            Temp_Doc.Range.Replace(ReStr,Search_Replace.ReplaceStrs[i].ToString().Trim());
                            Temp_Doc.Save(Path__ + "\\"+System.IO.Path.GetFileName(PublicDataArea.FilesInfo.Doc_FilesCollect[i].ToString()));
                            status = 1;
                            break;
                    }
                }
                switch (status)
                {
                    case 0:
                        MessageBox.Show("图片插入完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                    case 1:
                        MessageBox.Show("内容替换和图片插入完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                }
            }
            else
            {
                label5.Text = "";
            }
            SearchBox.Enabled = true;
            LoadFile.Enabled = true;
            StartRun.Enabled = true;
        }

因为涉及到表格的内容替换,所以我还在软件中增加了支持正则表达式搜索替换的功能,只是替换的信息是通过加载文本文档方式执行顺序替换的。

以上代码我写完的时候自己已经看不懂了,写的很烂,大佬忽视我的烂代码,这里提取出来获取图片和插入图片的两处关键代码

 foreach (Shape T_Shape in Template_Shapes)
                        {

                            if (T_Shape.HasImage)
                            {

                                ToatlImageCount++;//获取图片总数
                                Image Temp_Pic = T_Shape.ImageData.ToImage();
                                Bitmap Temp_Bitmap = new Bitmap(Temp_Pic);
                                int BitMap_Width = Temp_Pic.Width;
                                int BitMap_Height = Temp_Pic.Height;
                                if ((BitMap_Width == 196 && BitMap_Height == 256) || (BitMap_Width == 108 && BitMap_Height == 141))
                                {
                                        SignPicCount++;//此处探寻单人码图片并且计算
                                        RectangleF SignlePicRect = T_Shape.Bounds;
                                        RelativeHorizontalPosition R_H = T_Shape.RelativeHorizontalPosition;//水平相对位置方式
                                        RelativeVerticalPosition R_V = T_Shape.RelativeVerticalPosition;//垂直相对位置方式
                                        Return_List.Insert(0,SignlePicRect);
                                        Return_List.Insert(1,R_H);
                                        Return_List.Insert(2,R_V);
                                }

                            }

//上方是获取图片位置信息的过程

DB.InsertImage(PublicDataArea.FilesInfo.BMP_FileCollect[i].ToString(), PublicDataArea.FilesInfo.Template_RH, PublicDataArea.FilesInfo.Template_XPoint, PublicDataArea.FilesInfo.Template_RV, PublicDataArea.FilesInfo.Template_YPoint, PublicDataArea.FilesInfo.Template_Width, PublicDataArea.FilesInfo.Template_Height, WrapType.None);
                            Temp_Doc.Save(Path__ + "\\" + System.IO.Path.GetFileName(PublicDataArea.FilesInfo.Doc_FilesCollect[i].ToString()));
//上面代码是对指定位置进行插入图片的操作

004将Aspose.dll嵌入到exe

最让人头疼的应该是这个,当初第一次用Aspose的时候根据网上的方法尝试将dll嵌入到exe中,但是单独拿出来使用就会直接触发appcash,搜索了很多结果都无解,今天搜索这个内容的时候,在StackOverflow搜索到,只要使用nuget下载并引用Costura.Fody这个库再生成就可以自动将dll打包进exe,而且还不会引发appcash。也可以使用命令行输入Install-Package Costura.Fody便会自动引入,直接生成就可打包。

005Word文档和软件截图

文档截图:实际情况是只有这一个有这个二维码,其他的文档除了这个位置没有二维码,其余内容都是相同的

软件截图:。。。不知道为什么win10显示的是这个样子,在win7上面是正常的

006Github地址

本着学习的心态,所有的项目全是开源的,但是代码写的很随性,随性到我看了一遍都不记得这是我的垃圾代码,让大家见笑了

地址:https://github.com/jidesheng6/MakeSignleDocumentByTemplate

其实还有个姊妹项目,就是根据做好的单人表,进行识别验证二维码插入的是否正确(代替人工进行复查),地址:https://github.com/jidesheng6/SignleDocumentVerifity

谢谢大家啦,也当作是自己的笔记啦。

免费评分

参与人数 5吾爱币 +3 热心值 +5 收起 理由
woyucheng + 1 + 1 谢谢@Thanks!
sdaza + 1 我很赞同!
皆为泡影 + 1 + 1 我很赞同!
muyu1314520 + 1 热心回复!
夜泉 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

夜泉 发表于 2021-6-4 20:52
很不错,能参考~~
zds18909531256 发表于 2021-6-4 21:03
nb  大神 收下我的膜拜 奈何本领不高 只好减能看懂的部分 学习了~
龍謹 发表于 2021-6-5 09:05
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-28 20:19

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表