我有一项任务,我需要将omnipage XML转换为alto XML格式。到目前为止,我已经完成了XML转换,我正在检查转换的XML是否落在正确的坐标中。我尝试了一个软件调用aletheia(http://www.primaresearch.org/tools/),坐标落在适当的位置。但我想在我的WPF应用程序中也这样做。
但我的坐标错了(我使用了aletheia软件中使用的输入)。
这是输出:
这是我的代码
这是我从XML文件中获取坐标的代码
public class Rect
{
public double Width { get; set; }
public double Height { get; set; }
public double Left { get; set; }
public double Top { get; set; }
public SolidColorBrush Color { get; set; }
public string textBlockID {get; set;}
}
private void ReadAlto(string filename)
{
try
{
XDocument doc = XDocument.Load(filename);
foreach (var item in doc.Descendants("TextBlock").ToList())
{
double l = 0, t = 0, width = 0, height = 0;
l = double.Parse(item.Attribute("HPOS").Value);
t = double.Parse(item.Attribute("VPOS").Value);
width = double.Parse(item.Attribute("WIDTH").Value);
height = double.Parse(item.Attribute("HEIGHT").Value);
string id = item.Attribute("ID").Value;
Console.WriteLine("L: " + l + " T: " + t + " Width: " + width + " Height: " + height);
if (l!=0 && t !=0 && width != 0 && height !=0)
{
rects.Add(new Rect()
{
Width = width,
Height = height,
Left = l,
Top = t,
Color = Brushes.Red,
textBlockID = id
});
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
}
}
这是我在画布上绘图的代码
foreach (Rect rect in rects)
{
// ... Create Rectangle object.
Rectangle r = new Rectangle();
r.Width = rect.Width;
r.Height = rect.Height;
r.Fill = rect.Color;
// ... Set canvas position based on Rect object.
Canvas.SetLeft(r, rect.Left);
Canvas.SetTop(r, rect.Top);
// ... Add to canvas.
canvas.Children.Add(r);
}