How to Wrap Text in a PDF Grid Cell in C#

INTRODUCTION

In my previous post, I have introduced how to wrap text in Excel. It is quite easy to wrap text in Excel when there is an IsWarpText property in sheet.Rang class. However, in this post, I want to share a totally different method to wrap text in PDF grid cell because there isn’t such a similar property like IsWarpText in Spire.PDF.

For the purpose that we want the several paragraphs to be displayed in multiple lines, we could create a list with the class of PdfGridCellTextAndStyleList, then add every formatted phrase/sentence in the list. This is the main thought in this solution. Now, please check the targeted effect in the following screenshot.

wraptextinPdf

SOLUTION

In this solution, Spire.PDF will be used for creating PDF grid and adding text into grid cell. Please download Spire.PDF for .NET and reference its dll file in your project. Then let’s see the code:

Step 1. Create a new PDF document and add a page to PDF through function Pages.Add().

PdfDocument doc = new PdfDocument();

PdfPageBase page = doc.Pages.Add();

Step 2. Create a one cell PDF grid and set column width.

PdfGrid grid = new PdfGrid();

grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);

PdfGridRow row = grid.Rows.Add();

grid.Columns.Add(1);

grid.Columns[0].Width = 150;

Step 3. Initialize a new instance of PdfGridCellTextAndStyleList class.

PdfGridCellTextAndStyleList lst = new PdfGridCellTextAndStyleList();

Step 4. Define a new PDF gird cell text style by set its properties like Text, Font, Bruch. Then add the formatted text to the list.

PdfGridCellTextAndStyle textAndStyle = new PdfGridCellTextAndStyle();

textAndStyle.Text = “Main reasons that cause high blood pressure:”;

textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 10f, FontStyle.Regular), true);

textAndStyle.Brush = PdfBrushes.Black;

lst.List.Add(textAndStyle);

Step 5. Repeat step 4 to add more text(paragraphs) to the list. Here you should insert ‘\n’ to where you want this line break appears.

textAndStyle = new PdfGridCellTextAndStyle();

textAndStyle.Text = “\n1.Smoking”;

textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 10f, FontStyle.Regular), true);

textAndStyle.Brush = PdfBrushes.Brown;

lst.List.Add(textAndStyle);         

textAndStyle = new PdfGridCellTextAndStyle();

textAndStyle.Text = “\n2.Being overweight”;

textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 10f, FontStyle.Regular), true);

textAndStyle.Brush = PdfBrushes.Brown;

lst.List.Add(textAndStyle);

textAndStyle = new PdfGridCellTextAndStyle();

textAndStyle.Text = “\n3.Lack of physical activity”;

textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 10f, FontStyle.Regular), true);

textAndStyle.Brush = PdfBrushes.Brown;

lst.List.Add(textAndStyle);

textAndStyle = new PdfGridCellTextAndStyle();

textAndStyle.Text = “\n4.Too much salt in the diet”;

textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 8f, FontStyle.Regular), true);

textAndStyle.Brush = PdfBrushes.Brown;

lst.List.Add(textAndStyle);

textAndStyle = new PdfGridCellTextAndStyle();

textAndStyle.Text = “\n5.Drinking too much alcohol”;

textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 8f, FontStyle.Regular), true);

textAndStyle.Brush = PdfBrushes.Brown;

lst.List.Add(textAndStyle);

Step 6. Set the first cell value as the value of the list.

row.Cells[0].Value = lst;

Step 7. Draw the grid on PDF.

grid.Draw(page, new PointF(40, 50));

String outputFile = “..\\..\\ result.pdf”;

doc.SaveToFile(outputFile, FileFormat.PDF);

System.Diagnostics.Process.Start(outputFile);

Run the program, you’ll get the output as the screenshot above.

FULL CODE

using Spire.Pdf;

using Spire.Pdf.Grid;

using Spire.Pdf.Graphics;

using System.Drawing;

 

namespace WrapTextInPdfGridCell

{

    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(1);

            grid.Columns[0].Width = 150;

        

            PdfGridCellTextAndStyleList lst = new PdfGridCellTextAndStyleList();  

            PdfGridCellTextAndStyle textAndStyle = new PdfGridCellTextAndStyle();

            textAndStyle.Text = “Main reasons that cause high blood pressure:”;

            textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 10f, FontStyle.Regular), true);

            textAndStyle.Brush = PdfBrushes.Black;

            lst.List.Add(textAndStyle);

 

            textAndStyle = new PdfGridCellTextAndStyle();

            textAndStyle.Text = “\n1.Smoking”;

            textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 10f, FontStyle.Regular), true);

            textAndStyle.Brush = PdfBrushes.Brown;

            lst.List.Add(textAndStyle);         

            textAndStyle = new PdfGridCellTextAndStyle();

            textAndStyle.Text = “\n2.Being overweight”;

            textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 10f, FontStyle.Regular), true);

            textAndStyle.Brush = PdfBrushes.Brown;

            lst.List.Add(textAndStyle);

            textAndStyle = new PdfGridCellTextAndStyle();

            textAndStyle.Text = “\n3.Lack of physical activity”;

            textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 10f, FontStyle.Regular), true);

            textAndStyle.Brush = PdfBrushes.Brown;

            lst.List.Add(textAndStyle);

            textAndStyle = new PdfGridCellTextAndStyle();

            textAndStyle.Text = “\n4.Too much salt in the diet”;

            textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 8f, FontStyle.Regular), true);

            textAndStyle.Brush = PdfBrushes.Brown;

            lst.List.Add(textAndStyle);

            textAndStyle = new PdfGridCellTextAndStyle();

            textAndStyle.Text = “\n5.Drinking too much alcohol”;

            textAndStyle.Font = new PdfTrueTypeFont(new System.Drawing.Font(“Arial”, 8f, FontStyle.Regular), true);

            textAndStyle.Brush = PdfBrushes.Brown;

            lst.List.Add(textAndStyle);

 

            row.Cells[0].Value = lst;

 

            grid.Draw(page, new PointF(40, 50));

            String outputFile = “..\\..\\ result.pdf”;

            doc.SaveToFile(outputFile, FileFormat.PDF);

            System.Diagnostics.Process.Start(outputFile);

 

        }

    }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s