请教一个C#操作word的位置计算问题,100分送出

时间:2022-11-16 18:00:44
我的文件夹可能有N个图片,我想要实现的是9个图片分一页,且每一页的第一个图片位置要在指定位置上(picture.VerticalPosition = 10f;//图片起始位置),下面的代码遇到的问题是,分页后,图片不会出现在下一页上。只会在第一页,其他页是空的(虽然根据图片计算分成几页是对的)
 
  private void button1_Click(object sender, EventArgs e)
        {

            Document doc = new Document();
            //添加section
            Section section = doc.AddSection();
            Image imagecode;
            DocPicture picture;
            Paragraph paragraph;


            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                DirectoryInfo theFolder = new DirectoryInfo(foldPath);
                FileInfo[] dirInfo = theFolder.GetFiles();
                //遍历文件夹  
                int i = 0;//图片循环计数
                int j = 0; //图片位置控制计数
                foreach (FileInfo file in dirInfo)
                {
                    string tmp = file.FullName; //取文件全名
                    imagecode = Image.FromFile(tmp);//获取每个图片

                     paragraph = section.AddParagraph(); //增加段
                    //加载图片
                    picture = paragraph.AppendPicture(imagecode);//每段一个加载图片

                    //设置文字环绕方式
                    picture.TextWrappingStyle = TextWrappingStyle.Square;
                    if (i == 0) //第一个图片
                    {
                        picture.VerticalPosition = 10f;//图片起始位置
                    } else
                    picture.VerticalPosition = j*90+10.0f;//之后每个图片间隔90像素

                    //设置图片大小
                    picture.Width = 50;
                    picture.Height = 50;

                    if(i!=0 && i%9==0)  //9个图片分页
                    {
                        paragraph.AppendBreak(BreakType.PageBreak); //分页
                        //就是这里的问题,图片不会继续往新的页面上增加,是不是 picture.VerticalPosition 的问题?
                       //有点搞不清楚,word的这个纵向坐标,在分页后是怎么计算的?如果我把j重新计数,比如在这里
                      //我加上j=-1,让下面的j++重新为0开始计算位置,这时候,新的页面会有图片,但是第10个图片仍然会出现
                     //在第一页最下面(只露出一部分),不知道这个要怎么控制才会比较完美的实现我想要的
                    }

                    i++;
                    j++;

               }
            }
                   doc.SaveToFile("Image.doc", FileFormat.Doc);
        }

2 个解决方案

#1


请参考以下代码:
Document document = new Document();
            Section section = document.AddSection();
            Paragraph para = section.AddParagraph();
            Image image;
            int x = 60, y = 0, lineCount = 0;

            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                DirectoryInfo theFolder = new DirectoryInfo(foldPath);
                FileInfo[] dirInfo = theFolder.GetFiles();
                foreach (FileInfo file in dirInfo)
                {
                    string tmp = file.FullName;
                    image = Image.FromFile(tmp);

                    if (lineCount > 0 && lineCount % 9 == 0)
                    {
                        para.AppendBreak(BreakType.PageBreak);
                        x = 60;
                        y = 0;
                        lineCount = 0;
                    }

                    DocPicture picture = para.AppendPicture(image);
                    picture.Width = 50;
                    picture.Height = 50;
                    picture.TextWrappingStyle = TextWrappingStyle.Square;
                    picture.HorizontalPosition = x;
                    picture.HorizontalOrigin = HorizontalOrigin.Page;
                    picture.VerticalPosition = y * 90 + 50;

                    lineCount++;
                    y++;
                }
            }
            document.SaveToFile("Images.docx", FileFormat.Docx2013);

#2


1楼是正解!你只是分页了,没有把图片加到新建的页上

#1


请参考以下代码:
Document document = new Document();
            Section section = document.AddSection();
            Paragraph para = section.AddParagraph();
            Image image;
            int x = 60, y = 0, lineCount = 0;

            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                DirectoryInfo theFolder = new DirectoryInfo(foldPath);
                FileInfo[] dirInfo = theFolder.GetFiles();
                foreach (FileInfo file in dirInfo)
                {
                    string tmp = file.FullName;
                    image = Image.FromFile(tmp);

                    if (lineCount > 0 && lineCount % 9 == 0)
                    {
                        para.AppendBreak(BreakType.PageBreak);
                        x = 60;
                        y = 0;
                        lineCount = 0;
                    }

                    DocPicture picture = para.AppendPicture(image);
                    picture.Width = 50;
                    picture.Height = 50;
                    picture.TextWrappingStyle = TextWrappingStyle.Square;
                    picture.HorizontalPosition = x;
                    picture.HorizontalOrigin = HorizontalOrigin.Page;
                    picture.VerticalPosition = y * 90 + 50;

                    lineCount++;
                    y++;
                }
            }
            document.SaveToFile("Images.docx", FileFormat.Docx2013);

#2


1楼是正解!你只是分页了,没有把图片加到新建的页上