MS Word Interop:多少文本可以适合单元格

发布于 2025-02-10 08:36:34 字数 1083 浏览 1 评论 0原文

是否可以说出给定文本有多少适合桌单元格?这个示例将所有内容都放在第一个单元格中,我需要将其分为两个,而不调整第一个单元格或更改字体。

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);
    }
}

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

镜花水月 2025-02-17 08:36:34

@macropod带领我了解了一个肮脏的hack的想法(并不是说无论如何都有任何干净的方法):只要继续将文本推入单元,直到其高度变化为止。这足以满足我的目的,但是也可以检查宽度。

我使用下一行的Y位置来确定上一个的高度。因此,作为一个限制,下面至少应该再有一排。另外,也应检查页码。

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);
    }
}

@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. 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. 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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文