数量と単位のある商品のリストを作成しようとしています。 2行目は明るいグレーの色にする必要があります。PdfSharp長方形の上にテキストを書く
私は現在、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の最新のプレリリース版を入手していますが、状態を保存する必要はありませんが、状態を保存しない場合は機能しません。
あなたのソリューションが動作しますが、その差は、この行にある: 'graph.DrawRectangle(TextBackgroundBrush、marginLeft、lineY - 2 + marginTop、pdfPage.Width - marginLeft - marginRight、lineHeight - 2);' ここでは、 marginTopを追加する - 私が忘れた何か。だから私のコードでは、私はそれの上の行をカバーします。 ありがとうございます。 –