Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Jan 16, 2020 12:00 pm

I created a grid like this.
Image

I want to change the color like this.
Image

Code.
Code: Select all
public HttpResponseMessage GET_MOTOR_RENWAL_PDF()
{
    //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;
}

dushanm135
 
Posts: 6
Joined: Thu Jan 16, 2020 11:53 am

Fri Jan 17, 2020 9:02 am

Hello,

Thanks for your inquiry.
Please refer to the below code to achieve your demand. If there is any question, just feel free to contact us.
Code: Select all
//......
//Set border color
foreach (PdfGridRow row in grid.Rows)
{
    row.Cells[0].Style.Borders.Right = new PdfPen(PdfBrushes.Red);
    row.Cells[1].Style.Borders.Left = new PdfPen(PdfBrushes.Red);
}
//......


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Jan 17, 2020 11:07 am

rachel.lei wrote:Hello,

Thanks for your inquiry.
Please refer to the below code to achieve your demand. If there is any question, just feel free to contact us.
Code: Select all
//......
//Set border color
foreach (PdfGridRow row in grid.Rows)
{
    row.Cells[0].Style.Borders.Right = new PdfPen(PdfBrushes.Red);
    row.Cells[1].Style.Borders.Left = new PdfPen(PdfBrushes.Red);
}
//......


Sincerely,
Rachel
E-iceblue support team


Thanks it worked

dushanm135
 
Posts: 6
Joined: Thu Jan 16, 2020 11:53 am

Fri Jan 17, 2020 11:10 am

How do I achieve this? Color these lines which I have colored in red

Image

dushanm135
 
Posts: 6
Joined: Thu Jan 16, 2020 11:53 am

Sun Jan 19, 2020 2:05 am

Hello,

Thanks for your reply.
Please refer to the following code snippet. If there is any question, just feel free to contact us.
Code: Select all
//......
for (int i = 0; i < grid.Rows.Count; i++)
{
    if (i == 0)
    {
        grid.Rows[i].Cells[2].Style.Borders.Bottom = new PdfPen(PdfBrushes.Red);
    }
    else if (i == grid.Rows.Count - 1)
    {
        grid.Rows[i].Cells[2].Style.Borders.Top = new PdfPen(PdfBrushes.Red);
    }
    else
    {
        grid.Rows[i].Cells[2].Style.Borders.Top = new PdfPen(PdfBrushes.Red);
        grid.Rows[i].Cells[2].Style.Borders.Bottom = new PdfPen(PdfBrushes.Red);
    }
}
//......


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Jan 20, 2020 3:32 am

rachel.lei wrote:Hello,

Thanks for your reply.
Please refer to the following code snippet. If there is any question, just feel free to contact us.
Code: Select all
//......
for (int i = 0; i < grid.Rows.Count; i++)
{
    if (i == 0)
    {
        grid.Rows[i].Cells[2].Style.Borders.Bottom = new PdfPen(PdfBrushes.Red);
    }
    else if (i == grid.Rows.Count - 1)
    {
        grid.Rows[i].Cells[2].Style.Borders.Top = new PdfPen(PdfBrushes.Red);
    }
    else
    {
        grid.Rows[i].Cells[2].Style.Borders.Top = new PdfPen(PdfBrushes.Red);
        grid.Rows[i].Cells[2].Style.Borders.Bottom = new PdfPen(PdfBrushes.Red);
    }
}
//......


Sincerely,
Rachel
E-iceblue support team



thankz

dushanm135
 
Posts: 6
Joined: Thu Jan 16, 2020 11:53 am

Mon Jan 20, 2020 3:52 am

Hello,

You're welcome. Any question, please feel free to contact us. Have a nice day!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF