Beispiel #1
0
        private void InsertPicture(Paragraph paragraphToInsert, ObservableCollection <Picture> pictures)
        {
            Table picTable = paragraphToInsert.InsertTableAfterSelf(1, 2);

            picTable.AutoFit   = AutoFit.Window;
            picTable.Alignment = Alignment.left;

            foreach (var p in pictures)
            {
                p.CreatePicture();
            }

            var i = 0;

            while (i < pictures.Count)
            {
                Xceed.Words.NET.Image   img = _templateDocx.AddImage(pictures[i].FullPath);
                Xceed.Words.NET.Picture p   = img.CreatePicture();

                //非手機圖
                if (!IsCellPhoneScreenshot(pictures[i]))
                {
                    var row = picTable.InsertRow();
                    row.MergeCells(0, 1);

                    var row2 = picTable.InsertRow();
                    row2.MergeCells(0, 1);

                    //按照比例縮小圖片,如圖片不足600寬,則保留原大小
                    ResizePicture(pictures[i], 600, out var width, out var height);
                    p.Width  = width;
                    p.Height = height;

                    //新增table放圖片
                    row.Cells[0].Paragraphs.First().Append(pictures[i].Caption).Font(Font);
                    row2.Cells[0].Paragraphs.First().InsertPicture(p);

                    if (i == pictures.Count - 1)
                    {
                        break;
                    }

                    i++;
                }

                //第一張手機圖
                else
                {
                    //最後一張圖
                    if (i == pictures.Count - 1)
                    {
                        var row = picTable.InsertRow();
                        row.MergeCells(0, 1);

                        var row2 = picTable.InsertRow();
                        row2.MergeCells(0, 1);

                        //按照比例縮小圖片,如圖片不足200寬,則保留原大小
                        ResizePicture(pictures[i], 200, out var width, out var height);
                        p.Width  = width;
                        p.Height = height;

                        //新增table放圖片
                        row.Cells[0].Paragraphs.First().Append(pictures[i].Caption).Font(Font);
                        row2.Cells[0].Paragraphs.First().InsertPicture(p);

                        break;
                    }
                    //下一張不是手機圖
                    if (!IsCellPhoneScreenshot(pictures[i + 1]))
                    {
                        var row = picTable.InsertRow();
                        row.MergeCells(0, 1);

                        var row2 = picTable.InsertRow();
                        row2.MergeCells(0, 1);

                        //按照比例縮小圖片,如圖片不足200寬,則保留原大小
                        ResizePicture(pictures[i], 200, out var width, out var height);
                        p.Width  = width;
                        p.Height = height;

                        //新增table放圖片
                        row2.Cells[0].Paragraphs.First().InsertPicture(p);
                        row.Cells[0].Paragraphs.First().Append(pictures[i].Caption).Font(Font);

                        i++;
                    }

                    //下一張也是手機圖
                    else
                    {
                        Xceed.Words.NET.Image   img2 = _templateDocx.AddImage(pictures[i + 1].FullPath);
                        Xceed.Words.NET.Picture p2   = img2.CreatePicture();

                        var row  = picTable.InsertRow();
                        var row2 = picTable.InsertRow();


                        //按照比例縮小圖片,如圖片不足200寬,則保留原大小
                        ResizePicture(pictures[i], 200, out var width, out var height);
                        p.Width  = width;
                        p.Height = height;

                        ResizePicture(pictures[i + 1], 200, out var width2, out var height2);
                        p2.Width  = width2;
                        p2.Height = height2;

                        //新增table放圖片
                        row.Cells[0].Paragraphs.First().Append(pictures[i].Caption).Font(Font);
                        row2.Cells[0].Paragraphs.First().InsertPicture(p);

                        row.Cells[1].Paragraphs.First().Append(pictures[i + 1].Caption).Font(Font);
                        row2.Cells[1].Paragraphs.First().InsertPicture(p2);

                        i += 2;
                    }
                }
            }
            picTable.RemoveRow(0);
            picTable.InsertParagraphAfterSelf("\r\n");

            foreach (var p in pictures)
            {
                p.ReleasePictureResource();
            }
        }