PdfSharp 在矩形顶部书写文本
PdfSharp Writing text on top of a rectangle
我正在尝试制作一个包含数量和单位的产品清单。每隔一行应为浅灰色。
我目前在一个 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");
上面的代码是我从 那里得到的
the linked forum post in the answer
然而,现在这行不通了。所有的灰色线条仍然隐藏着所有的文字。
我有来自 NuGet (1.50.4619-beta4c) 的 PdfSharp 的最新预发布,即使答案说没有必要保存状态,但当我省略保存状态时它也不起作用。
我似乎无法重现您的问题。我还使用来自 NuGet (1.50.4619-beta4c) 的 PdfSharp。我创建了一个 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; }
}
}
我的主窗口XAML只是一个按钮:
<Button Width="100" Height="30" Click="Button_Click">Create PDF</Button>
然后我得到了我想要的 PDF
PDF.
解决您的问题的最佳建议是试一试 lineHeight 等设置。
我正在尝试制作一个包含数量和单位的产品清单。每隔一行应为浅灰色。
我目前在一个 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");
上面的代码是我从
然而,现在这行不通了。所有的灰色线条仍然隐藏着所有的文字。 我有来自 NuGet (1.50.4619-beta4c) 的 PdfSharp 的最新预发布,即使答案说没有必要保存状态,但当我省略保存状态时它也不起作用。
我似乎无法重现您的问题。我还使用来自 NuGet (1.50.4619-beta4c) 的 PdfSharp。我创建了一个 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; }
}
}
我的主窗口XAML只是一个按钮:
<Button Width="100" Height="30" Click="Button_Click">Create PDF</Button>
然后我得到了我想要的 PDF PDF.
解决您的问题的最佳建议是试一试 lineHeight 等设置。