首页 > 解决方案 > 如何以编程方式将图像插入表格单元格并自动缩放以适应

问题描述

我自动将图像插入到 ms word 文档表格单元格中。但是.. 我需要自动缩放图像(不丢失 dpi)以适合表格单元格。有 2 个表格单元格,它们都应该占据表格分配宽度的 50%。所以..如您所见...图像未缩放以适合表格单元格。并且单元格宽度不相等。基本上,图像应该放在 2 列中,每个图像行之后都有一个空白行。对于每 6 个图像,将插入一个带有新表格的新页面。我发布图像和代码

图像不缩放

public void InsertTable()
        {
            List<string> pics = AmendPictures();

            Microsoft.Office.Interop.Word._Document WordDoc = null;         
            Microsoft.Office.Interop.Word._Application axWord = null;
            Microsoft.Office.Interop.Word.Table axTable = null;
            try
            {              
                axWord = new Microsoft.Office.Interop.Word.Application();
                axWord.Visible = true;

                object oMissing = System.Reflection.Missing.Value;
                WordDoc = axWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                // This is For Header columns 
                object missing = System.Reflection.Missing.Value;

                int totalRows = (pics.Count % 2 == 0) ? pics.Count : (pics.Count + 1);
                int rowsPerTable = 6;
                int numberOfPages = Convert.ToInt32(Math.Round(Convert.ToDouble(totalRows/6)));

                int pageBreakCounter = 1;
                int y = 1;
                for (int x = 0; x < pics.Count; x += 2)
                {

                    if (((x % rowsPerTable) == 0) || (x ==0))
                    {
                        axWord.Selection.GoTo(WdGoToItem.wdGoToLine, WdGoToDirection.wdGoToLast, oMissing, oMissing);
                        axTable = WordDoc.Tables.Add(axWord.Selection.Range, rowsPerTable, 2);
                        axTable.PreferredWidthType = WdPreferredWidthType.wdPreferredWidthPercent;
                        axTable.PreferredWidth = 100;
                        axTable.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow);
                        axTable.Range.Rows.Alignment = WdRowAlignment.wdAlignRowCenter;
                        axTable.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                        axTable.Range.ParagraphFormat.SpaceAfter = 0;
                        axTable.Range.ParagraphFormat.SpaceBefore = 0;
                        // Show Borders

                        axTable.Range.Columns.Borders.Enable = 1;

                        axTable.Columns[1].PreferredWidthType = WdPreferredWidthType.wdPreferredWidthPercent;
                        axTable.Columns[1].SetWidth(50, WdRulerStyle.wdAdjustNone);

                        axTable.Columns[2].PreferredWidthType = WdPreferredWidthType.wdPreferredWidthPercent;
                        axTable.Columns[2].SetWidth(50, WdRulerStyle.wdAdjustNone);
                    }

                    InlineShape inline_shape = null;
                    InlineShape inline_shape2 = null;

                    Range rngPic1 = axTable.Cell((x+1) % 6, 1).Range;
                    rngPic1.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    inline_shape = rngPic1.InlineShapes.AddPicture(pics.ElementAt(x).ToString(), ref missing, ref missing, ref missing);
                    if (inline_shape != null)
                    {
                        Shape shape = inline_shape.ConvertToShape();
                        shape.WrapFormat.Type = WdWrapType.wdWrapInline;
                    }

                    if ((x + 1) < pics.Count)
                    {
                        Range rngPic2 = axTable.Cell((x + 1) % 6, 2).Range;
                        rngPic2.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                        inline_shape2 = rngPic2.InlineShapes.AddPicture(pics.ElementAt(x+1).ToString(), ref missing, ref missing, ref missing);
                        if (inline_shape2 != null)
                        {
                            Shape shape = inline_shape2.ConvertToShape();
                            shape.WrapFormat.Type = WdWrapType.wdWrapInline;
                        }
                    }

                    pageBreakCounter++;

                    y = y + 2;
                    if ((x % rowsPerTable) == 0)
                    {
                        axWord.Selection.GoTo(WdGoToItem.wdGoToLine, WdGoToDirection.wdGoToLast, oMissing, oMissing);
                        axWord.Selection.InsertNewPage();
                        y = 0;
                    }
                }
                WordDoc.Content.Font.Size = 12;
                WordDoc.Content.Font.Name = "Calibri";

                Microsoft.Office.Interop.Word.Dialog dialog = axWord.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFileSaveAs];
                dialog.Show(ref oMissing);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "     " + ex.InnerException);
            }
            finally
            {
                if (axTable != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(axTable);
                    // Release all Interop objects.
                }

                if (WordDoc != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(WordDoc);
                }

                if (axWord != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(axWord);
                }

                WordDoc = null;
                axWord = null;
                GC.Collect();
            }
        }

标签: c#ms-wordword-table

解决方案


如果将行高或列宽设置为精确值,则图像应缩放到该尺寸。

执行此操作时,还要确保表格自动调整行为不允许单元格扩展。这意味着当添加一个表设置DefaultTableBehaviorwdWord8TableBehavior. AutoFitBehavior应该是wdAutoFitFixed

如果一个表已经存在,也可以专门设置最后一个。在问题的代码中,我发现这是不正确的:

axTable.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow);

您还需要测试使用百分比作为 PreferredWidthType 是否支持精确设置。这可能需要更改为使用积分。


推荐阅读