我正在尝试列出具有数量和单位的产品。每隔一行应该是浅灰色。
我目前正在for循环中执行此操作,如下所示:
for (int i = 0; i < OrderList.Count; i++)
{
double lineY = lineHeight * (i + 1);
if (i % 2 == 1)
{
XSolidBrush brush = new XSolidBrush(XColors.LightGray);
graph.DrawRectangle(brush, marginLeft, lineY - 2, size.Width - marginLeft - marginRight, lineHeight - 2);
}
var state = graph.Save();
graph.DrawString(
OrderList[i].Product.Name,
fontParagraph,
XBrushes.Black,
new XRect(nameX, marginTop + lineY, pdfPage.Width.Point, pdfPage.Height.Point),
XStringFormats.TopLeft);
graph.DrawString(
OrderList[i].Quantity.ToString(),
fontParagraph,
XBrushes.Black,
new XRect(quantityX, marginTop + lineY, pdfPage.Width.Point, pdfPage.Height.Point),
XStringFormats.TopLeft);
graph.DrawString(
OrderList[i].Unit,
fontParagraph,
XBrushes.Black,
new XRect(unitX, marginTop + lineY, pdfPage.Width.Point, pdfPage.Height.Point),
XStringFormats.TopLeft);
graph.Restore(state);
}
pdf.Save("C:\\Users\\Tobias\\Desktop\\firstpage.pdf");
以上代码是我从PDFsharp draws text under graphics获得的代码 the linked forum post in the answer
然而,现在,这不起作用。所有灰线仍然隐藏所有文本。 我从NuGet(1.50.4619-beta4c)获得了PdfSharp的最新预发布版,即使答案说没有必要保存状态,但是当我省略保存状态时它也不起作用
答案 0 :(得分:1)
我似乎无法复制你的问题。我也在使用NuGet的PdfSharp(1.50.4619-beta4c)。我创建了一个WPF测试程序,其中包含lineHeight,marginLeft等的设置:
public partial class MainWindow : Window
{
private PdfDocument document;
private double lineHeight = 20;
private XFont fontParagraph = new XFont("Verdana", 12, XFontStyle.Regular);
private int marginLeft = 20;
private int marginRight = 20;
private int marginTop = 20;
private int nameX = 0;
private int quantityX = 100;
private int unitX = 200;
private string filename = @"C:\Users\Christian\Desktop\Orders.pdf";
XSolidBrush TextBackgroundBrush = new XSolidBrush(XColors.LightGray);
public MainWindow()
{
InitializeComponent();
document = new PdfDocument();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var OrderList = new List<Order>();
OrderList.Add(new Order {
Unit = "L",
Quantity = 100,
Product = new Product { Name = "Coca Cola" }
});
OrderList.Add(new Order
{
Unit = "L",
Quantity = 50,
Product = new Product { Name = "Coca Cola Zero" }
});
PdfPage pdfPage = document.AddPage();
XGraphics graph = XGraphics.FromPdfPage(pdfPage);
for (int i = 0; i < OrderList.Count; i++)
{
double lineY = lineHeight * (i + 1);
if (i % 2 == 1)
{
graph.DrawRectangle(TextBackgroundBrush, marginLeft, lineY - 2 + marginTop, pdfPage.Width - marginLeft - marginRight, lineHeight - 2);
}
graph.DrawString(
OrderList[i].Product.Name,
fontParagraph,
XBrushes.Black,
new XRect(nameX + marginLeft, marginTop + lineY, pdfPage.Width.Point, pdfPage.Height.Point),
XStringFormats.TopLeft);
graph.DrawString(
OrderList[i].Quantity.ToString(),
fontParagraph,
XBrushes.Black,
new XRect(quantityX + marginLeft, marginTop + lineY, pdfPage.Width.Point, pdfPage.Height.Point),
XStringFormats.TopLeft);
graph.DrawString(
OrderList[i].Unit,
fontParagraph,
XBrushes.Black,
new XRect(unitX + marginLeft, marginTop + lineY, pdfPage.Width.Point, pdfPage.Height.Point),
XStringFormats.TopLeft);
}
document.Save(filename);
Process.Start(filename);
}
private class Order
{
public int Quantity { get; set; }
public string Unit { get; set; }
public Product Product { get; set; }
}
private class Product
{
public string Name { get; set; }
}
}
我的MainWindow XAML只是一个按钮:
<Button Width="100" Height="30" Click="Button_Click">Create PDF</Button>
我按照需要获得PDF PDF
我解决您问题的最佳建议是使用lineHeight等设置。