2017-12-12 13 views
-1

数量と単位のある商品のリストを作成しようとしています。 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 graphicsthe linked forum post in the answer

から得たものだった、しかし、それは動作しません。すべての灰色の線はまだすべてのテキストを隠しています。 NuGet(1.50.4619-beta4c)からPdfSharpの最新のプレリリース版を入手していますが、状態を保存する必要はありませんが、状態を保存しない場合は機能しません。

答えて

1

問題を再現できないようです。私はNuGet(1.50.4619-beta4c)のPdfSharpも使用しています。私はなどlineHeight、marginLeft、の設定でWPFのテストプログラムを作成しました:

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などの設定を使用することをお勧めします。

+0

あなたのソリューションが動作しますが、その差は、この行にある: 'graph.DrawRectangle(TextBackgroundBrush、marginLeft、lineY - 2 + marginTop、pdfPage.Width - marginLeft - marginRight、lineHeight - 2);' ここでは、 marginTopを追加する - 私が忘れた何か。だから私のコードでは、私はそれの上の行をカバーします。 ありがとうございます。 –

関連する問題