Изменить цвет границы в отдельных ячейках сетки Spire.PDF C# - PullRequest
1 голос
/ 16 января 2020

Я использую библиотеку Spire.PDF, чтобы создать PDF на стороне сервера и вернуть его на мобильную сторону. Все работает отлично. Но я хочу получить доступ к границе каждой ячейки в сетке. Я хочу изменить цвет границы.

https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Table/How-to-Change-the-Color-of-Grid-Border-in-PDF-in-C.html

Текущий код ..

public HttpResponseMessage GET_MOTOR_RENWAL_PDF()
{
    AgentDAL agntDAL = new AgentDAL();

    //Create a pdf document.
    PdfDocument doc = new PdfDocument();
    PdfSection sec = doc.Sections.Add();
    sec.PageSettings.Width = PdfPageSize.A4.Width;
    sec.PageSettings.Height = PdfPageSize.A4.Height;
    sec.PageSettings.Margins = new PdfMargins(20f, 5f, 20f, 5f);
    PdfPageBase page = sec.Pages.Add();



    //title
    PdfBrush brush1 = PdfBrushes.Black;
    PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Bold));
    PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
    page.Canvas.DrawString("Monthly", font1, brush1, page.Canvas.ClientSize.Width / 2, 60, format1);
    page.Canvas.DrawString("Report generated", font1, brush1, page.Canvas.ClientSize.Width / 2, 76, format1);

    //grid
    PdfGrid grid = new PdfGrid();
    grid.Columns.Add(5);
    float gridWidth = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
    for (int j = 0; j < grid.Columns.Count; j++)
    {
        grid.Columns[j].Width = gridWidth * 0.20f;
    }

    PdfGridRow row0 = grid.Rows.Add();
    PdfGridRow row1 = grid.Rows.Add();
    PdfGridRow row2 = grid.Rows.Add();
    PdfGridRow row3 = grid.Rows.Add();

    row0.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);
    row1.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);
    row2.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);
    row3.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 11f, FontStyle.Regular), true);


    row0.Cells[2].ColumnSpan = 3;
    row1.Cells[2].ColumnSpan = 3;
    row2.Cells[2].ColumnSpan = 3;
    row3.Cells[2].ColumnSpan = 3;
    row0.Cells[2].RowSpan = 4;

    row0.Cells[0].Value = "Policy";
    row0.Cells[1].Value = "VM5115001510000009";
    row1.Cells[0].Value = "Vehicle";
    row1.Cells[1].Value = "BBP";
    row2.Cells[0].Value = "Premium";
    row2.Cells[1].Value = "7,366.10";
    row3.Cells[0].Value = "Date";
    row3.Cells[1].Value = "03-Jan-2020";

    float gridHeight = 20.0f;
    for (int i = 0; i < grid.Rows.Count; i++)
    {
        grid.Rows[i].Height = gridHeight;
    }

    grid.Draw(page, new PointF(0, 120));

    MemoryStream stream = new MemoryStream();
    doc.SaveToStream(stream);

    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new ByteArrayContent(stream.ToArray());
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    doc.Close();

    return result;
}

Что я получаю от это ...

enter image description here

Что я хочу ...

enter image description here

Я пытался сделать это, но это не сработало.

        row0.Cells[0].Style.Borders.Right.Color = Color.DarkRed;
        row1.Cells[0].Style.Borders.Right.Color = Color.DarkRed;
        row2.Cells[0].Style.Borders.Right.Color = Color.DarkRed;
        row3.Cells[0].Style.Borders.Right.Color = Color.DarkRed;

1 Ответ

1 голос
/ 20 января 2020

Попробуйте следующий код:

float gridHeight = 20.0f;
for (int i = 0; i < grid.Rows.Count; i++)
{
   grid.Rows[i].Height = gridHeight;
   grid.Rows[i].Cells[0].Style.Borders.Right = new PdfPen(PdfBrushes.DarkRed);
   grid.Rows[i].Cells[1].Style.Borders.Left = new PdfPen(PdfBrushes.DarkRed); 
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...