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.

Fri Jul 05, 2019 8:50 am

Hello,

i'm testing this library to generate reports in PDF that contain tables and the text in those tables needs to be formatted differently (table header, first column, results...).

Some cells need to have vertical text.

Some cells need to have part of the text in bold and italics, and the rest in bold.

Other cells may need to include normal text and superscrpt text.

I have been looking the examples and searched the forum and did not find any information about mixing different formats in the same text block.

Is this supported by the library? Could you provide some .NET code?

Thank you very much.

progenika
 
Posts: 2
Joined: Fri Jul 05, 2019 7:18 am

Fri Jul 05, 2019 11:24 am

Hello,

Thanks for your inquiry.
Our Spire.PDF supports setting "bold and italics","bold" and "superscript" text format for different cells like the following sample code. Regarding vertical text, to help us investigate it accurately, please share us with your desired effect of output for reference, then we will guide you accordingly.
Code: Select all
  PdfDocument doc = new PdfDocument();
  PdfPageBase page = doc.Pages.Add();
  PdfGrid grid = new PdfGrid();
  grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
  grid.RepeatHeader = true;
  grid.Columns.Add(3);
 PdfGridRow headerRow = grid.Headers.Add(1)[0];
 string[] header = new string[3] { "COL1", "COL2", "COL3" };
 for (int i = 0; i < header.Length; i++)
 {
     headerRow.Cells[i].Value = header[i];
 }
 //Bold and Italic text
 headerRow.Cells[0].Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Italic | FontStyle.Bold), true);
 //Bold text
 headerRow.Cells[1].Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true);
 //Superscript text
 PdfStringFormat format = new PdfStringFormat();   
 format.SubSuperScript = PdfSubSuperScript.SuperScript;
 headerRow.Cells[2].StringFormat = format;
 grid.Draw(page, 0, 0);
 doc.SaveToFile("result.pdf");

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Fri Jul 05, 2019 12:01 pm

Looking at the provided code, seems that the formatting of the text is for the whole cell.

What I was looking for is combine several texts with several fonts ans formats in the same cell, so it looks like:
SampleCellTextWithMixedFormat

Also, superscript text is also needed in the cell , mixed with other text formats as above.

And other cells need text with mixed formats (bold and bold and italics) and also vertical text, all together.

Thank you

progenika
 
Posts: 2
Joined: Fri Jul 05, 2019 7:18 am

Mon Jul 08, 2019 8:33 am

Hello,

Thanks for your more details.
Please refer to the following sample code to set mixed formats for text in one table cell. Here, I also attached my output PDF file for your reference. If there is any other question, welcome to write back.
Code: Select all
 PdfDocument doc = new PdfDocument();
 PdfPageBase page = doc.Pages.Add();
 //add grid1
 PdfGrid grid = new PdfGrid();
 grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
 PdfGridRow row = grid.Rows.Add();
 row.Height = 100;
 grid.Columns.Add(2);
 grid.Columns[0].Width=250;
 //add embedGrid in the cell of grid1
 PdfGrid embedGrid = new PdfGrid();
 PdfGridRow row1= embedGrid.Rows.Add();
 row1.Height = 60;
 embedGrid.Columns.Add(2);
 embedGrid.Columns[0].Width = 220;
 embedGrid.Columns[1].Width = 10;
 //add different format text in the cell of embedGrid
 PdfGridCellContentList first = new PdfGridCellContentList();
 PdfGridCellContent textAndStyle = new PdfGridCellContent();
 //Add Italic string
 textAndStyle.Text = "Sample";
 textAndStyle.Font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Italic), true);
 textAndStyle.Brush = PdfBrushes.Black;
 first.List.Add(textAndStyle);
 //Add Bold string
 textAndStyle = new PdfGridCellContent();
 textAndStyle.Text = "CellText";
 textAndStyle.Font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold), true);
 textAndStyle.Brush = PdfBrushes.Black;
 first.List.Add(textAndStyle);
 //Add Italic and Bold string
 textAndStyle = new PdfGridCellContent();
 textAndStyle.Text = "WithMixed ";
 textAndStyle.Font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Italic | FontStyle.Bold), true);
 textAndStyle.Brush = PdfBrushes.Black;
 first.List.Add(textAndStyle);
 //Add Superscript Text
 textAndStyle = new PdfGridCellContent();
 textAndStyle.Text = "SuperscriptText";
 textAndStyle.Font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular), true);
 PdfStringFormat format = new PdfStringFormat();
 format.SubSuperScript = PdfSubSuperScript.SuperScript;
 textAndStyle.StringFormat = format;
 textAndStyle.Brush = PdfBrushes.Black;
 first.List.Add(textAndStyle);
 row1.Cells[0].Value = first;
 //add verticalText string in cell of embedGrid2
 String verticalText = "Test";
 char[] charArray = verticalText.ToCharArray();
 int no = charArray.Length;
 PdfGrid embedGrid2 = new PdfGrid();
 embedGrid2.Columns.Add(1);
 for (int i = 0; i < no; i++)
 {
     string cellString = charArray[i].ToString();
     PdfGridRow newRow = embedGrid2.Rows.Add();
     newRow.Cells[0].Value = cellString;
 }
 //set the borders of embedGrid2 in transparent.
 PdfBorders borders = new PdfBorders();
 borders.All = new PdfPen(Color.Transparent, 0.1f);
 foreach (PdfGridRow pgr in embedGrid2.Rows)
 {
     foreach (PdfGridCell pgc in pgr.Cells)
     {
         pgc.Style.Borders = borders;
     }
 }
 row1.Cells[1].Value = embedGrid2;
 //set the borders of embedGrid in transparent.
 foreach (PdfGridRow pgr in embedGrid.Rows)
 {
     foreach (PdfGridCell pgc in pgr.Cells)
     {
         pgc.Style.Borders = borders;
     }
 }
 row.Cells[0].Value = embedGrid;
 //Draw grid
 PdfLayoutResult result = grid.Draw(page, new PointF(10, 20));
 String outputFile = "result.pdf";
 doc.SaveToFile(outputFile);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Wed Jul 10, 2019 2:24 am

Hello,

Greetings from E-iceblue.
Did the sample code help you? Your feedback will be greatly appreciated.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF

cron