我試圖對準一些字符串爲列,但打印預覽總是顯示我像對齊串入列打印
Artikelnr. Name Gewicht (kg) Menge Einheit
1 Dog 1 10 kg
2 Cat 1 8 kg
3 Horse 1 22 kg
前兩列看起來不錯,但一切之後被搞砸的時候。
這是代碼的樣子:
String header = String.Format("{0, -15} {1, -20} {2, -10} {3, -10} {4, -10}", "Artikelnr.", "Name", "Gewicht (kg)", "Menge", "Einheit");
e.Graphics.DrawString(header , font, Brushes.Black, e.MarginBounds.Left, rowPosition);
rowPosition += 20;
e.Graphics.DrawLine(new Pen(Brushes.Black), new Point(e.MarginBounds.Left, rowPosition), new Point(e.MarginBounds.Right, rowPosition));
rowPosition += 15;
font = new Font("Arial", 8);
String item = String.Empty;
foreach (var i in activeOrder.OrderItem)
{
item += String.Format("{0, 15} {1, -30} {2, -30} {3, -30} {4, -30}", i.ItemId, i.Item.Name, i.Item.Weight, i.Quantity, i.Item.Unit);
item += String.Empty;
}
e.Graphics.DrawString(item, font, Brushes.Black, e.MarginBounds.Left, rowPosition);
也試過這種早期
font = new Font("Arial", 8);
String item = String.Empty;
foreach (var i in activeOrder.OrderItem)
{
item = String.Format("{0, 15} {1, -30} {2, -30} {3, -30} {4, -30}", i.ItemId, i.Item.Name, i.Item.Weight, i.Quantity, i.Item.Unit);
e.Graphics.DrawString(item, font, Brushes.Black, e.MarginBounds.Left, rowPosition);
rowPosition += 15;
}
我看幾個例子,其中大部分是寫控制檯。這是什麼造成了差異,還是我在這裏錯過了一些東西?
乾杯!
編輯: 調試時,文本可視化工具爲我提供了正確的項目字符串輸出,但打印預覽和打印版本不正確。
int columnPosition = e.MarginBounds.Left;
int rowPosition = e.MarginBounds.Top + 300;
// Item header
String columnHeaders = String.Format("{0, -15} {1, -20} {2, -10} {3, -10} {4, -10}", "Artikelnr.", "Name", "Gewicht (kg)", "Menge", "Einheit");
e.Graphics.DrawString(columnHeaders, font, Brushes.Black, e.MarginBounds.Left, rowPosition);
rowPosition += 20;
e.Graphics.DrawLine(new Pen(Brushes.Black), new Point(e.MarginBounds.Left, rowPosition), new Point(e.MarginBounds.Right, rowPosition));
rowPosition += 15;
font = new Font("Courier", 8);
String item = String.Empty;
foreach (var i in activeOrder.OrderItem)
{
item += String.Format("{0, -10} {1, -30} {2, -20} {3, -20} {4, -0}", i.ItemId, i.Item.Name, i.Item.Weight, i.Quantity, i.Item.Unit);
item += Environment.NewLine;
}
e.Graphics.DrawString(item, font, Brushes.Black, e.MarginBounds.Left, rowPosition);
有沒有更好的方法來打印項目列表中的數據?也許把它放入數據表或其他東西?
我把字符串分成兩部分,分別打印出來。不知何故,這解決了這個問題。 – Lahey