我自动将图像插入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();
}
}
答案 0 :(得分:0)
如果将行高或列宽设置为 exact 值,则图像应缩放至该尺寸。
执行此操作时,还请确保表的自动适应行为不允许单元格扩展。这意味着在将表集DefaultTableBehavior
添加到wdWord8TableBehavior
时。并且AutoFitBehavior
应该是wdAutoFitFixed
。
如果一个表已经存在,则最后一个也可以专门设置。在问题代码中,我看到这是不正确的:
axTable.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow);
您还需要测试使用百分比作为PreferredWidthType是否支持确切的设置。可能需要将其更改为使用点数。