[英]MS Word interop: How much text can fit into a cell
Is it possible to tell how much of a given text can fit into a table cell?是否可以判断给定文本的多少可以放入表格单元格中? This example puts everything in the first cell, I need to split it in two without resizing the first cell or changing the font.此示例将所有内容放在第一个单元格中,我需要将其一分为二,而不需要调整第一个单元格的大小或更改字体。
using System.IO;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
class Program
{
static void Main()
{
string filename = "example.docx";
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor";
var wordApp = new Word.Application { Visible = false };
string path = Path.Combine(Directory.GetCurrentDirectory(), filename);
var doc = wordApp.Documents.Open(path, ReadOnly: false, Visible: true);
doc.Activate();
var table = doc.Tables[1];
// todo: truncate text by the cell's size
table.Cell(1, 1).Range.Text = text;
string text2 = "";
// todo: put the remainder of the truncated text to text2
table.Cell(2, 1).Range.Text = text2;
doc.Save();
object missing = Missing.Value;
doc.Close(ref missing, ref missing, ref missing);
wordApp.Quit(ref missing, ref missing, ref missing);
}
}
@macropod led me to the idea of one dirty hack (not that there is any clean way to work with Word anyway): just keep pushing text into the cell until its height changes. @macropod 让我想到了一个肮脏的技巧(并不是说有任何干净的方式可以使用 Word):只需继续将文本推入单元格,直到其高度发生变化。 This is enough for my purpose, but one can check width as well.这对我的目的来说已经足够了,但也可以检查宽度。
I use next row's Y position to determine previous one's height.我使用下一行的 Y 位置来确定前一个的高度。 So, as a limitation, there should be at least one more row below.所以,作为一个限制,下面应该至少再多一行。 Also, page number should be checked too.此外,页码也应检查。
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
class Program
{
static void Main()
{
string filename = "example.docx";
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor";
int row = 1;
int column = 1;
var wordApp = new Word.Application { Visible = true };
string path = Path.Combine(Directory.GetCurrentDirectory(), filename);
var doc = wordApp.Documents.Open(path, ReadOnly: false, Visible: true);
doc.Activate();
var table = doc.Tables[1];
// Add as much text to the cell (row,column) as possible without changing the height of the cell.
// Row's bottom Y coordinate can be determined by the position of the next row and its page number.
Debug.Assert(table.Rows.Count > row, "Need at least 1 more row below");
table.Rows[row + 1].Select();
float oldBottom = wordApp.Selection.Information[Word.WdInformation.wdVerticalPositionRelativeToPage];
int oldPage = wordApp.Selection.Information[Word.WdInformation.wdActiveEndPageNumber];
// Binary-searching the length of the longest fitting substring
int min = 0;
int max = text.Length;
int length = 0;
while (min < max)
{
length = (min + max) / 2;
table.Cell(row, column).Range.Text = text.Substring(0, length);
float bottom = wordApp.Selection.Information[Word.WdInformation.wdVerticalPositionRelativeToPage];
float page = wordApp.Selection.Information[Word.WdInformation.wdActiveEndPageNumber];
if (page > oldPage || bottom > oldBottom)
max = length - 1;
else
min = length + 1;
}
// Don't split words
while (length > 0 && char.IsLetterOrDigit(text[length - 1]))
length--;
table.Cell(row, column).Range.Text = text.Substring(0, length);
doc.Save();
object missing = Missing.Value;
doc.Close(ref missing, ref missing, ref missing);
wordApp.Quit(ref missing, ref missing, ref missing);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.