Insert text with html to a table cell PDF .net

Hi,

i am building a PDF document. i need to add text
Dim s1 As String = "STAFF STATUS: "
to a table cell. since it has html in it, i believe i have to do something different.
here is part the code.

Dim doc As Document = New Document()
doc.Pages.Add()
Dim secEmail As Aspose.Pdf.Generator.Section = pdf.Sections.Add()
Dim ProviderTable As Aspose.Pdf.Table = New Aspose.Pdf.Table()
ProviderTable.ColumnWidths = “200 300”
Dim Provrow1 As Row = ProviderTable.Rows.Add()
Dim Provrow1col As Cell = Provrow1.Cells.Add(CommitteeDate)
Dim s1 As String = "STAFF STATUS: "
Dim text1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(s1)
text1.IsHtmlTagSupported = True
text1.TextInfo.IsUnicode = True

Dim Provrow20 As Row = ProviderTable.Rows.Add()
Dim Provrow20col1 As Cell = Provrow20.Cells.Add()
text1.ID = s1 ‘’ not sure this is right ‘’
Provrow20col1.Paragraphs.Add(s1) ‘’ it does not like this ''

i need to add the text with html to that cell.

thanks in advance.
Ying




Hi Ying,

Thanks for your inquiry. You may use HtmlFragment to add html text in PDF document. Please check the following sample code to add HtmlFragment in table cell and please also check the documentation for adding html string in PDF for more details.

// Load source PDF document
Document doc = new Aspose.Pdf.Document();
Page page = doc.Pages.Add();

// Initializes a new instance of the Table
Table table = new Aspose.Pdf.Table();
table.ColumnAdjustment = Aspose.Pdf.ColumnAdjustment.AutoFitToContent;

// Set the table border color as LightGray
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));

// set the border for table cells
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));

// create a loop to add 10 rows
for (int i = 0; i < 10; i++)
{
    // add row to table
    Row row = table.Rows.Add();
    // add table cells
    row.Cells.Add("Column 1");
    row.Cells.Add("Column 2");
    row.Cells.Add("Column 3");
}

Row row1 = table.Rows.Add();
row1.Cells.Add("Row2 cell1");
row1.Cells.Add("Row2 cell2");
Cell cell1 = row1.Cells.Add();
HtmlFragment htmlFragment = new Aspose.Pdf.HtmlFragment("<span style=\"background-color: rgb(255, 0, 255);\">Te</span>sting o<span style=\"background-color: rgb(255, 255, 0);\">f</span> opportunity <em>description</em>. adding new <span style=\"color: rgb(255, 153, 0);\">description</span> ");
htmlFragment.Margin.Bottom = 10;
cell1.Paragraphs.Add(htmlFragment);

// Add table object to first page of input document
page.Paragraphs.Add(table);

// Save updated document containing table object
doc.Save(myDir + "Table_HtmlFragment.pdf");

Please feel free to contact us for any further assistance.

Best Regards,

var celllabelQuotation = rowCoverpage.Cells.Add();
var html1 = new HtmlFragment("

QUOTATION

");
html1.HorizontalAlignment = HorizontalAlignment.Center;
celllabelQuotation.Paragraphs.Add(html1);var celllabelQuotation = rowCoverpage.Cells.Add();
var html1 = new HtmlFragment("

QUOTATION

");
html1.HorizontalAlignment = HorizontalAlignment.Center; ////NOT WORKING
celllabelQuotation.Paragraphs.Add(html1);

rollyhernando:

var celllabelQuotation = rowCoverpage.Cells.Add();
var html1 = new HtmlFragment("<div style='text-align:center;'><h3 style='font-family:Arial;'>QUOTATION</h3></div>");
html1.HorizontalAlignment = HorizontalAlignment.Center;
celllabelQuotation.Paragraphs.Add(html1);
var celllabelQuotation = rowCoverpage.Cells.Add();
var html1 = new HtmlFragment("<div style='text-align:center;'><h3 style='font-family:Arial;'>QUOTATION</h3></div>");
html1.HorizontalAlignment = HorizontalAlignment.Center; ////NOT WORKING
celllabelQuotation.Paragraphs.Add(html1);

Hi Rolly,

I have tested the scenario using the following code snippet (based on the code snippet shared above) and I am unable to notice any problem. The HTML contents are properly being displayed inside table cells. Can you please share some details regarding the issue which you are facing.

For your reference, I have also attached the resultant PDF generated on my end.

C#

// Load source PDF document
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
doc.Pages.Add();

// Initializes a new instance of the Table
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
doc.Pages[1].Paragraphs.Add(table);
table.ColumnWidths = "100 100 100";

// Set the table border color as LightGray
table.Border = new Aspose.Pdf.BorderInfo(
    Aspose.Pdf.BorderSide.All,
    .5f,
    Aspose.Pdf.Color.FromRgb(
        System.Drawing.Color.LightGray
    )
);

// set the border for table cells
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(
    Aspose.Pdf.BorderSide.All,
    .5f,
    Aspose.Pdf.Color.FromRgb(
        System.Drawing.Color.LightGray
    )
);

// create a loop to add 10 rows

// add row to table
Aspose.Pdf.Row rowCoverpage = table.Rows.Add();
rowCoverpage.FixedRowHeight = 20;

// add table cells
rowCoverpage.Cells.Add();
var html1 = new HtmlFragment("<div style='text-align:center;'><h3 style='font-family:Arial;'>QUOTATION</h3></div>");
html1.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
rowCoverpage.Cells[0].Paragraphs.Add(html1);

rowCoverpage.Cells.Add("Column 2");

var celllabelQuotation = rowCoverpage.Cells.Add();
var html2 = new HtmlFragment("<div style='text-align:center;'><h3 style='font-family:Arial;'>QUOTATION</h3></div>");
html2.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center; ////NOT WORKING
celllabelQuotation.Paragraphs.Add(html2);

// Save updated document containing table object
doc.Save("c:/pdftest/HtmlInPDF.pdf");

Nayyer, I attached the output image. Below code is use html1.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center; to alignt the text of the cell in the center position. But its not working.

I want all text to be centered position.

var pdfdoc = new Aspose.Pdf.Document();
pdfdoc.PageInfo.Width = 650;
pdfdoc.PageInfo.Height = 850;
pdfdoc.PageInfo.Margin.Left = 40;
pdfdoc.PageInfo.Margin.Right = 40;
pdfdoc.PageInfo.Margin.Bottom = 25;
pdfdoc.PageInfo.Margin.Top = 50;
var pdfpage = pdfdoc.Pages.Add();

//cover page logo
var imgISClogo1 = new Aspose.Pdf.Image();
imgISClogo1.ImageStream = new FileStream(Server.MapPath(@"~/images/isc_blue.jpg").ToString(), FileMode.Open, FileAccess.Read);
imgISClogo1.FixHeight = 112;
imgISClogo1.FixWidth = 100;
var imgISCtext = new Aspose.Pdf.Image();
imgISCtext.ImageStream = new FileStream(Server.MapPath(@"~/images/isc_text_blue.png").ToString(), FileMode.Open, FileAccess.Read);
imgISCtext.FixHeight = 85;
imgISCtext.FixWidth = 480;

//cover page body
var tblCoverpage = new Aspose.Pdf.Table();
tblCoverpage.ColumnWidths="570";
tblCoverpage.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F, Aspose.Pdf.Color.FromArgb(104, 100, 100));
tblCoverpage.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F, Aspose.Pdf.Color.FromArgb(104, 100, 100));
var rowCoverpage = tblCoverpage.Rows.Add();
rowCoverpage.DefaultCellTextState.HorizontalAlignment = HorizontalAlignment.Center;
var cellisclogo1 = rowCoverpage.Cells.Add();
cellisclogo1.Paragraphs.Add(imgISClogo1);
rowCoverpage = tblCoverpage.Rows.Add();
var cellisclogo2 = rowCoverpage.Cells.Add();
cellisclogo2.Paragraphs.Add(imgISCtext);
rowCoverpage = tblCoverpage.Rows.Add();
var celllabelQuotation = rowCoverpage.Cells.Add();
var html1 = new HtmlFragment("

QUOTATION

");
///html1.Margin.Left = 190;
html1.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
celllabelQuotation.Paragraphs.Add(html1);

//celllabelQuotation.Paragraphs.Add();
rowCoverpage = tblCoverpage.Rows.Add();
var celllabelCustomer = rowCoverpage.Cells.Add();

if(enq.customernumber==null||enq.customernumber==0)
celllabelCustomer.Paragraphs.Add(new HtmlFragment("

Customer

"));
else
celllabelCustomer.Paragraphs.Add(new HtmlFragment("

Customer: " + enq.customernumber + "

"));

rowCoverpage = tblCoverpage.Rows.Add();
var celllabelCustomerName = rowCoverpage.Cells.Add();
celllabelCustomerName.Paragraphs.Add(new HtmlFragment("

" + enq.customername + "

"));
rowCoverpage = tblCoverpage.Rows.Add();
var celllabelFacilityNumber = rowCoverpage.Cells.Add();
celllabelFacilityNumber.Paragraphs.Add(new HtmlFragment("

Facility Project: " + enq.fpid + "

"));
rowCoverpage = tblCoverpage.Rows.Add();
var celllabelFacilityName = rowCoverpage.Cells.Add();
celllabelFacilityName.Paragraphs.Add(new HtmlFragment("

" + enq.fpname + ""));
pdfpage.Paragraphs.Add(tblCoverpage);

tblCoverpage.IsInNewPage = true;v

Hi Rolly,

Thanks for sharing the details.

I have tested the scenario and I am able to reproduce the same problem that table contents are not being displayed in Center Alignment. For the sake of correction, I have logged it in our issue tracking system as PDFNEWNET-38436. We will investigate this issue in details and will keep you updated on the status of a correction.

We apologize for your inconvenience.

Hi Nayyer,

Any update???

Hi Rolly,

As we recently have been able to notice this issue, and until or unless we have investigated and have figured out the actual reasons of this problem, we might not be able to share any timelines by which this problem will be resolved.

However, as soon as we have made some significant progress towards the resolution of this issue, we would be more than happy to update you with the status of correction. Please be patient and spare us some time. Your patience and comprehension is greatly appreciated in this regard.