将文本从网格复制到未保留的编辑器表格文本列 [英] Copy text from the grid to editor tabular text columns not preserved

查看:76
本文介绍了将文本从网格复制到未保留的编辑器表格文本列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户面临将文本从我的应用程序网格复制到另一个应用程序文本编辑器的问题(具体 - 接收站点上的应用程序是Bloomberg IB Chat) - 文本列未正确对齐。



你可以看到我的代码 - 这适用于纯文本编辑器,如记事本,记事本++。我认为这是有效的,因为记事本是单声道字体编辑器,每个字符宽度是相同的。例如,当我将文本复制到Word时,文本没有正确显示在我的网格应用程序上。当我将文本复制到彭博的IB聊天时也是如此。



附加代码,当用户点击ctrl + c时,我调用FormatFromClipBoard()方法。请帮忙。



我尝试过:



User is facing problem copying text from my application grid into another application text editor (to be specific - app on the receiving site is Bloomberg IB Chat) - text columns not aligned properly.

You can see my code - this works on plain text editors such as notepad, notepad++. I think it is working because notepad is mono font editor each character width is the same. When I copy text into Word, for example, text not showing up properly aligned as it was on my grid app. Same happens when I copy text into IB Chat in Bloomberg.

Code attached, I call FormatFromClipBoard() method when user clicks ctrl+c. Please help.

What I have tried:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;

namespace Utils
{
    public class CopyFormatter
    {
        Literal [][] matrix;

        public void FormatFromClipBoard()
        {
            try
            {
                string clipboardText = Clipboard.GetText(TextDataFormat.Text);

                BuildMatrx(clipboardText);
                CalculateTabs();
                string sReformatedText = ReformatText();
                Clipboard.SetText(sReformatedText);
            }
            catch( Exception e)
            {
            }
        }

        private string ReformatText()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < matrix.Length; i++)
            {
                for (int j = 0; j < matrix[0].Length; j++)
                {
                    sb.Append(matrix[i][j].StringWithTabs());
                }
                sb.AppendLine();
            }
            return sb.ToString();
        }

        private void CalculateTabs()
        {
            for (int j = 0; j < matrix[0].Length; j++)
            {
                //scan vertically all first elements, all second elements to get longest token for each column
                ReferenceInt maxChars = new ReferenceInt();
                for (int i = 0; i < matrix.Length; i++)
                {
                    maxChars.LocalInt = maxChars.LocalInt > matrix[i][j].Length ? maxChars.LocalInt : matrix[i][j].Length;
                    maxChars.ContainsPeriod = matrix[i][j].text.Contains('.') ? true : maxChars.ContainsPeriod;
                    matrix[i][j].MaxCharCount = maxChars;
                }
            }
        }


        private void BuildMatrx(string clipboardText)
        {
            string[] arr = clipboardText.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            if (arr != null && arr.Length > 0)
            {
                //number of lines
                matrix = new Literal[arr.Length][];

                List<string[]> lst = new List<string[]>();
                int maxlen = 0;
                Array.ForEach(arr, line =>
                {
                    string[] l = line.Split(new char[] { '\t' }, StringSplitOptions.None);
                    lst.Add(l);
                    maxlen = l.Length > maxlen ? l.Length : maxlen;
                });

                if (maxlen > 0)
                {
                    int i = 0;
                    lst.ForEach(line =>
                    {
                        matrix[i] = new Literal[maxlen];
                        int j = 0;
                        Array.ForEach(line, token =>
                        {
                            matrix[i][j++] = new Literal(token);
                        });
                        for(; j < maxlen; j++)
                        {
                            matrix[i][j] = new Literal(string.Empty);
                        }
                        i++;
                    });
                }
            }
        }
    }

    private class Literal
    {
        public Literal(string token)
        {
            text = token;
        }
        public string text { get; set; } = string.Empty;
        public int Length { get { return text.Length; } }
        public ReferenceInt MaxCharCount { get; set; }

        public string StringWithTabs()
        {
            int tabSize = 5;
            int numberOfFlights = MaxCharCount.LocalInt / tabSize;                    
            int currWordFlights = Length / tabSize;
            int totalTabs = numberOfFlights - currWordFlights + 1;
            string s = new String('\t', totalTabs);
            return text + s;
        }
    }

    private class ReferenceInt
    {
        public int LocalInt { get; set; } = 0;
    }
}

推荐答案

文字是文字。



字体(例如单声道,大小,重量等)由接收器/文档/容器/电子邮件客户端/浏览器/字体可用性控制。



除非有允许按原样粘贴的功能(例如单声道的 ),否则这些东西会获得操作系统认为最接近的字体一个给定的家庭(字体)。
Text is text.

The "font" (e.g. mono, size, weight, etc.) is controlled by the receiver / document / "container" / email client / browser / font availability.

Unless there are features to allow for pasting "as is" (like for mono), the stuff gets which ever font the OS "thinks" is closest for a given "family" (of fonts).


格里谢谢。我理解你的观点。然而,发布一个问题,我想也许社区中有一些人做了类似的事情:

1.设置全局挂钩粘贴功能。

2.检测接收粘贴结果的窗口的字体系列。

3.找出如何根据每列中最长的单词对齐数据。
Gerry thanks. I understand your point. However, posting a question I thought maybe there are some people in the community who are done something like:
1. Set global hook to paste function.
2. Detect the font family of the window that receives paste result.
3. Figure out how to align data based on the longest word in each column.


这篇关于将文本从网格复制到未保留的编辑器表格文本列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆