InsertHTML for PDF

Does Aspose.PDF have an equivalent of the InsertHTML method in Aspose.Words?

I want to add a HTML string (with an un/ordered list) to a PDF document. I tried Adding a Text object with IsHTMLTagSupported set to true, but the text shows all the HTML tags.

How can I do this?

Thanks.

Please refer to [Working with Text Containing HTML Tags](https://forum.aspose.com/wiki/default.aspx/Aspose.Pdf/TextContainingHtmlTags.html). If it still won’t work, please provide an example that can reproduce the problem.

In this help 'Text Containing Html Tags' there seems to be no support for html lists ie tags

    ,
    ,
  • . Is this the case? What is the easiest way to create these lists in PDF format. The code I have works fine if each item is on a single line, but does not align correctly is an item is on more than one line.

Example web form :-

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using Aspose.Pdf;

namespace TestPdf

{

///

/// Summary description for Form1.

///

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button1;

///

/// Required designer variable.

///

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

///

/// Clean up any resources being used.

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

///

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(48, 80);

this.button1.Name = "button1";

this.button1.TabIndex = 0;

this.button1.Text = "CreatePDF";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

this.ResumeLayout(false);

}

#endregion

///

/// The main entry point for the application.

///

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)

{

Pdf doc;

Section sec;

TextInfo CurrentTextInfo = CreateTextInfo(AlignmentType.Left, false, false);

Text CurrentText = new Text("");

Segment CurrentSegment = new Segment();

Table table;

Row row;

Cell cell;

TextInfo tiDefault = CreateTextInfo(AlignmentType.Left, false, false);

TextInfo tiBoldCentre = CreateTextInfo(AlignmentType.Center, true, false);

TextInfo tiRight = CreateTextInfo(AlignmentType.Right, false, false);

TextInfo tiBoldRight = CreateTextInfo(AlignmentType.Right, true, false);

TextInfo tiBold = CreateTextInfo(AlignmentType.Left, true, false);

TextInfo tiBoldUnderline = CreateTextInfo(AlignmentType.Left, true, true);

doc = new Pdf();

//headers

//Create the Table Of Contents. Add it to the pdf like a common Section.

ListSection tocSection = new ListSection("The Contents");

// tocSection.Title = new Text("The Contents", CurrentTextInfo);

// tocSection.Title.TextInfo.IsTrueTypeFontBold = true;

tocSection.ListType = ListType.TableOfContents;

tocSection.PageInfo.Margin.Top = 50;

tocSection.PageInfo.Margin.Bottom = 50;

tocSection.PageInfo.Margin.Left = 50;

tocSection.PageInfo.Margin.Right = 50;

doc.Sections.Add(tocSection);

//

// //Define the format of the four levels' list.

tocSection.ListFormatArray.Length = 4;

tocSection.ListFormatArray[0].LeftMargin = 0;

tocSection.ListFormatArray[0].TextInfo.FontSize = 10;

tocSection.ListFormatArray[0].TextInfo.IsTrueTypeFontBold = true;

// tocSection.ListFormatArray[0].TextInfo.IsTrueTypeFontItalic = true;

tocSection.ListFormatArray[1].LeftMargin = 10;

// tocSection.ListFormatArray[1].TextInfo.IsUnderline = true;

tocSection.ListFormatArray[1].TextInfo.FontSize = 10;

tocSection.ListFormatArray[2].LeftMargin = 20;

tocSection.ListFormatArray[2].TextInfo.FontSize = 10;

// tocSection.ListFormatArray[2].TextInfo.IsTrueTypeFontBold = true;

tocSection.ListFormatArray[3].LeftMargin = 30;

tocSection.ListFormatArray[3].TextInfo.FontSize = 10;

// tocSection.ListFormatArray[3].TextInfo.IsTrueTypeFontBold = true;

//Add four headings.

// sec = doc.Sections.Add();

// for (int Level = 1;Level != 5; Level++)

// {

// Heading heading2 = new Heading(doc,sec,Level);

//

// Segment segment2 = new Segment(heading2);

// heading2.Segments.Add(segment2);

// heading2.IsAutoSequence = true;

// segment2.Content = "this is heading of level ";

// segment2.Content += Level.ToString();

// //Add the heading into Table Of Contents.

// heading2.IsInList = true;

// sec.Paragraphs.Add(heading2);

// }

//-------------------------------------------------------------------------

sec = doc.Sections.Add();

HeaderFooter hf1 = new HeaderFooter();

sec.OddHeader= hf1;

sec.EvenHeader = hf1;

// hf1.IsFirstPageOnly = true;

Text text = new Text(hf1);

hf1.Paragraphs.Add(text);

Segment segment = new Segment(text);

text.Segments.Add(segment);

segment.Content = "headers 123";

HeaderFooter hf2 = new HeaderFooter();

sec.OddFooter= hf2;

sec.EvenFooter = hf2;

// hf2.IsFirstPageOnly = true;

text = new Text(hf2);

hf2.Paragraphs.Add(text);

segment = new Segment(text);

text.Segments.Add(segment);

segment.Content = "footers 345";

sec.PageInfo.Margin.Top = 50;

sec.PageInfo.Margin.Bottom = 50;

sec.PageInfo.Margin.Left = 50;

sec.PageInfo.Margin.Right = 50;

AddHeader (doc, sec, 1, "Section One");

CurrentTextInfo = tiDefault;

CurrentText = new Text("First Segment ", CurrentTextInfo);

sec.Paragraphs.Add(CurrentText);

CurrentTextInfo = tiBoldUnderline;

CurrentSegment = new Segment("Second Segment ", CurrentTextInfo);

CurrentText.Segments.Add (CurrentSegment);

CurrentTextInfo = tiDefault;

CurrentSegment = new Segment("Third Segment ", CurrentTextInfo);

CurrentText.Segments.Add (CurrentSegment);

CurrentTextInfo = tiBold;

CurrentSegment = new Segment("Forth Segment " + Environment.NewLine, CurrentTextInfo);

CurrentText.Segments.Add (CurrentSegment);

sec.Paragraphs.Add(CurrentText);

// -------------------------------------------------------------------------

CurrentText = null;

CurrentText = new Text("testing html", CurrentTextInfo);

CurrentText.IsHtmlTagSupported = true;

sec.Paragraphs.Add(CurrentText);

CurrentText.IsHtmlTagSupported = false;

CurrentText = null;

CurrentText = new Text("

  • testing html 1
  • testing html 2
", CurrentTextInfo);

CurrentText.IsHtmlTagSupported = true;

sec.Paragraphs.Add(CurrentText);

CurrentText.IsHtmlTagSupported = false;

// -------------------------------------------------------------------------

CurrentTextInfo = tiBold;

CurrentText = new Text("Text", CurrentTextInfo);

CurrentTextInfo = tiDefault;

CurrentSegment = new Segment("Segment 2", CurrentTextInfo);

CurrentText.Segments.Add (CurrentSegment);

AddHeader (doc, sec, 2, "Section OneOne");

sec.Paragraphs.Add(CurrentText);

// -------------------------------------------------------------------------

sec = doc.Sections.Add();

AddHeader (doc, sec, 2, "Section OneTwo");

sec.Paragraphs.Add(new Text("Text", tiDefault));

sec.IsNewPage=true;

hf1 = new HeaderFooter();

sec.OddHeader= hf1;

sec.EvenHeader = hf1;

hf1.IsFirstPageOnly = true;

text = new Text(hf1);

hf1.Paragraphs.Add(text);

segment = new Segment(text);

text.Segments.Add(segment);

segment.Content = "headers adas asd asd asd asd asd";

hf2 = new HeaderFooter();

sec.OddFooter= hf2;

sec.EvenFooter = hf2;

hf2.IsFirstPageOnly = true;

text = new Text(hf2);

hf2.Paragraphs.Add(text);

segment = new Segment(text);

text.Segments.Add(segment);

segment.Content = "footers asdasd aasd asd ads";

sec = doc.Sections.Add();

sec.OddHeader= hf1;

sec.EvenHeader = hf1;

sec.OddFooter= hf2;

sec.EvenFooter = hf2;

AddHeader (doc, sec, 1, "Section Two");

sec.Paragraphs.Add(new Text("Text", tiDefault));

sec.IsNewPage=true;

sec = doc.Sections.Add();

sec.OddHeader= hf1;

sec.EvenHeader = hf1;

sec.OddFooter= hf2;

sec.EvenFooter = hf2;

AddHeader (doc, sec, 2, "Section TwoOne");

sec.Paragraphs.Add(new Text("Text", tiDefault));

sec.IsNewPage=true;

sec = doc.Sections.Add();

sec.OddHeader= hf1;

sec.EvenHeader = hf1;

sec.OddFooter= hf2;

sec.EvenFooter = hf2;

AddHeader (doc, sec, 2, "Section TwoTwo");

sec.Paragraphs.Add(new Text("Text", tiDefault));

sec.IsNewPage=true;

// -------------------------------------------------------------------------

CurrentText = new Text("First Segment 1 ", CurrentTextInfo);

sec.Paragraphs.Add(CurrentText);

CurrentTextInfo = tiBoldUnderline;

CurrentSegment = new Segment("Second Segment ", CurrentTextInfo);

CurrentText.Segments.Add (CurrentSegment);

CurrentTextInfo = tiDefault;

CurrentSegment = new Segment("Third Segment ", CurrentTextInfo);

CurrentText.Segments.Add (CurrentSegment);

CurrentTextInfo = tiBold;

CurrentSegment = new Segment("Forth Segment 4 ", CurrentTextInfo);

CurrentText.Segments.Add (CurrentSegment);

//

// -------------------------------------------------------------------------

sec.TextInfo.FontName = "Times-Roman";

sec.TextInfo.FontSize = 12;

sec.PageInfo.Margin.Top = 50;

sec.PageInfo.Margin.Bottom = 50;

sec.PageInfo.Margin.Left = 50;

sec.PageInfo.Margin.Right = 50;

sec.Paragraphs.Add(new Text("test",CreateTextInfo(AlignmentType.Center, true, false)));

sec.Paragraphs.Add(new Text(""));

table = new Table();

table.ColumnWidths = "100 50";

table.Border = new BorderInfo((int)BorderSide.All,2,new Aspose.Pdf.Color(0));

row = table.Rows.Add();

cell = row.Cells.Add("Col_1", tiBoldCentre);

cell.Border = new BorderInfo((int)(BorderSide.Bottom | BorderSide.Top | BorderSide.Left | BorderSide.Right),1,new Aspose.Pdf.Color(100));

cell.BackgroundColor= new Aspose.Pdf.Color("Gray");

cell = row.Cells.Add("Col_2", tiBoldCentre);

cell.Border = new BorderInfo((int)(BorderSide.Bottom | BorderSide.Top | BorderSide.Left | BorderSide.Right),1,new Aspose.Pdf.Color(100));

cell.BackgroundColor= new Aspose.Pdf.Color("Gray");

row = table.Rows.Add();

cell = row.Cells.Add("");

cell.Paragraphs.Add(CurrentText);

cell.Border = new BorderInfo((int)(BorderSide.Bottom | BorderSide.Top | BorderSide.Left | BorderSide.Right),1,new Aspose.Pdf.Color(100));

cell = row.Cells.Add("");

cell.Border = new BorderInfo((int)(BorderSide.Bottom | BorderSide.Top | BorderSide.Left | BorderSide.Right),1,new Aspose.Pdf.Color(100));

row = table.Rows.Add();

cell = row.Cells.Add();

cell.Border = new BorderInfo((int)(BorderSide.Bottom | BorderSide.Top | BorderSide.Left | BorderSide.Right),1,new Aspose.Pdf.Color(100));

cell.ColumnsSpan = 2;

// Set image

// Aspose.Pdf.Image img1 = new Aspose.Pdf.Image();

// img1.ImageInfo.File = @"c:\test.jpg";

// img1.ImageInfo.ImageFileType = ImageFileType.Jpeg;

// // img1.ImageInfo.FixWidth = 81;

// // sec.Paragraphs.Add(img1);

// cell.Paragraphs.Add(img1);

sec.Paragraphs.Add(table);

doc.Save(@"../../test3.pdf");

}

///

///

///

///

///

///

///

private TextInfo CreateTextInfo (AlignmentType alignmentType, bool isBold, bool isUnderline)

{

TextInfo ti = new TextInfo();

ti.Alignment = alignmentType;

if (isUnderline)

{

ti.IsUnderline = true;

}

if (isBold)

{

ti.FontName = "Times-Bold";

}

return ti;

}

private void AddHeader (Pdf doc, Section sec, int level, string text)

{

Heading heading2 = new Heading(doc,sec,level);

Segment segment2 = new Segment(heading2);

heading2.Segments.Add(segment2);

heading2.IsAutoSequence = false;

heading2.FirstLineIndent = 0;

heading2.LabelWidth=0;

heading2.IsSpaced=false;

segment2.Content = text;

TextInfo ti = CreateTextInfo(AlignmentType.Left, true, false);

ti.FontSize = 16;

segment2.TextInfo = ti;

heading2.IsInList = true; //Add the heading into Table Of Contents.

sec.Paragraphs.Add(heading2);

}

private void Form1_Load(object sender, System.EventArgs e)

{

}

}

}

I have finished the function to support the unordered list, but it will cost much more time that the ordered list is a little complex.

The new fix will be available soon.

Thanks a lot!