Insert a Line Break in PDF Grid Cell in C#/VB.NET

2015-01-20 02:30:23 Written by  support iceblue
Rate this item
(0 votes)

In some cases, you have several items that you want them displayed on multiple lines within a PDF grid cell. However if you don't enter a line break at a specific point in a cell, these items will appear as a whole sentence. In the article, you can learn how to insert line breaks in PDF grid cell via Spire.PDF in C#, VB.NET.

Here come the detailed steps:

Step 1: Initialize a new instance of PdfDocument and add a new page to PDF document.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Create a PDF gird with one row and three columns.

PdfGrid grid = new PdfGrid();
grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
PdfGridRow row = grid.Rows.Add();
grid.Columns.Add(3);
grid.Columns[0].Width = 80;
grid.Columns[1].Width = 80;
grid.Columns[2].Width = 80;

Step 3: Initialize a new instance of PdfGridCellTextAndStyleList class and PdfGridCellTextAndStyle class. Set parameters of the variable textAndStyle such as text, font and brush. Add textAndStlye into PdfGridCellTextAndStyleList.

PdfGridCellTextAndStyleList lst = new PdfGridCellTextAndStyleList(); 
PdfGridCellTextAndStyle textAndStyle = new PdfGridCellTextAndStyle();
textAndStyle.Text = "Line 1";
textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font("Airal", 8f, FontStyle.Regular), true);
textAndStyle.Brush = PdfBrushes.Black;
lst.List.Add(textAndStyle);

Step 4: Repeat step 3 to add three other lines. Here you should insert '\n' to where you want this line break appears.

textAndStyle = new PdfGridCellTextAndStyle();
textAndStyle.Text = "\nLine 2";
textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font("Arial", 8f, FontStyle.Regular), true);
textAndStyle.Brush = PdfBrushes.Black;
lst.List.Add(textAndStyle);
textAndStyle = new PdfGridCellTextAndStyle();
textAndStyle.Text = "\nLine 3";
textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font("Arial", 8f, FontStyle.Regular), true);
textAndStyle.Brush = PdfBrushes.Black;
lst.List.Add(textAndStyle);
textAndStyle = new PdfGridCellTextAndStyle();
textAndStyle.Text = "\nLine 4";
textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font("Arial",8f, FontStyle.Regular), true);
textAndStyle.Brush = PdfBrushes.Black;
lst.List.Add(textAndStyle);
row.Cells[0].Value = lst;

Step 5: Draw the gird on PDF page and save the file.

grid.Draw(page, new PointF(10, 20));
String outputFile = "..\\..\\Sample.pdf";
doc.SaveToFile(outputFile, FileFormat.PDF);
System.Diagnostics.Process.Start(outputFile);

Result:

How to Insert a Line Break in PDF Grid Cell in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System;
using System.Drawing;


namespace LineBreak
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

            PdfGrid grid = new PdfGrid();
            grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
            PdfGridRow row = grid.Rows.Add();
            grid.Columns.Add(3);
            grid.Columns[0].Width = 80;
            grid.Columns[1].Width = 80;
            grid.Columns[2].Width = 80;

            PdfGridCellContentList lst = new PdfGridCellContentList();
            PdfGridCellContent textAndStyle = new PdfGridCellContent();
            textAndStyle.Text = "Line 1";
            textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font("Airal", 8f, FontStyle.Regular), true);
            textAndStyle.Brush = PdfBrushes.Black;
            lst.List.Add(textAndStyle);
            textAndStyle = new PdfGridCellContent();
            textAndStyle.Text = "\nLine 2";
            textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font("Arial", 8f, FontStyle.Regular), true);
            textAndStyle.Brush = PdfBrushes.Black;
            lst.List.Add(textAndStyle);
            textAndStyle = new PdfGridCellContent();
            textAndStyle.Text = "\nLine 3";
            textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font("Arial", 8f, FontStyle.Regular), true);
            textAndStyle.Brush = PdfBrushes.Black;
            lst.List.Add(textAndStyle);
            textAndStyle = new PdfGridCellContent();
            textAndStyle.Text = "\nLine 4";
            textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font("Arial", 8f, FontStyle.Regular), true);
            textAndStyle.Brush = PdfBrushes.Black;
            lst.List.Add(textAndStyle);
            row.Cells[0].Value = lst;

            grid.Draw(page, new PointF(10, 20));
            String outputFile = "..\\..\\Sample.pdf";
            doc.SaveToFile(outputFile, FileFormat.PDF);
            System.Diagnostics.Process.Start(outputFile);
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Grid
Imports System.Drawing


Namespace LineBreak
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			Dim page As PdfPageBase = doc.Pages.Add()

			Dim grid As New PdfGrid()
			grid.Style.CellPadding = New PdfPaddings(1, 1, 1, 1)
			Dim row As PdfGridRow = grid.Rows.Add()
			grid.Columns.Add(3)
			grid.Columns(0).Width = 80
			grid.Columns(1).Width = 80
			grid.Columns(2).Width = 80

			Dim lst As New PdfGridCellContentList()
			Dim textAndStyle As New PdfGridCellContent()
			textAndStyle.Text = "Line 1"
			textAndStyle.Font = New PdfTrueTypeFont(New System.Drawing.Font("Airal", 8F, FontStyle.Regular), True)
			textAndStyle.Brush = PdfBrushes.Black
			lst.List.Add(textAndStyle)
			textAndStyle = New PdfGridCellContent()
			textAndStyle.Text = vbLf & "Line 2"
			textAndStyle.Font = New PdfTrueTypeFont(New System.Drawing.Font("Arial", 8F, FontStyle.Regular), True)
			textAndStyle.Brush = PdfBrushes.Black
			lst.List.Add(textAndStyle)
			textAndStyle = New PdfGridCellContent()
			textAndStyle.Text = vbLf & "Line 3"
			textAndStyle.Font = New PdfTrueTypeFont(New System.Drawing.Font("Arial", 8F, FontStyle.Regular), True)
			textAndStyle.Brush = PdfBrushes.Black
			lst.List.Add(textAndStyle)
			textAndStyle = New PdfGridCellContent()
			textAndStyle.Text = vbLf & "Line 4"
			textAndStyle.Font = New PdfTrueTypeFont(New System.Drawing.Font("Arial", 8F, FontStyle.Regular), True)
			textAndStyle.Brush = PdfBrushes.Black
			lst.List.Add(textAndStyle)
			row.Cells(0).Value = lst

			grid.Draw(page, New PointF(10, 20))
			Dim outputFile As [String] = "..\..\Sample.pdf"
			doc.SaveToFile(outputFile, FileFormat.PDF)
			System.Diagnostics.Process.Start(outputFile)
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Insert a Line Break in PDF Grid Cell
Last modified on Sunday, 26 September 2021 01:55