Insert HTML string to PDF Page header using C# | Aspose.PDF for .NET

hi.
i try to insert html into pdf file.
i don’t know why he add new page.
i want to insert into exist file at the header (top of the file)

  Dim pdfDocument As Aspose.Pdf.Document = New Aspose.Pdf.Document(fileFolder & fileName)
      
      Dim page As Page = pdfDocument.Pages.add()
      Dim htmlFragment As HtmlFragment = New HtmlFragment(addHeader)
      htmlFragment.IsInNewPage  = false
      page.Paragraphs().add(htmlFragment)
      pdfDocument.Save(fileFolder & fileName)

@eranlipi

Can you please try to use the below sample code snippet in order to add the HTML string in the PDF Page Header and let us know in case you still face any issue:

[C#]

Document doc = new Document();
doc.Pages.Add();

HtmlFragment htf = new HtmlFragment("<span style='color: #ff0000; white-space: nowrap; -ms-writing-mode: tb-rl; transform: rotate(180deg); background-color: #ffff00; -webkit-writing-mode: vertical-rl;'>123456</span>");
HeaderFooter header = new HeaderFooter();
header.Paragraphs.Add(htf);
doc.Pages[1].Header = header;
doc.Save(dataDir + "HTMLInHeader.pdf");

Hi,
i try to add the HTML to Existing PDF File.

when i try your code i get the next error

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30456: ‘Paragraphs’ is not a member of ‘HeaderFooter’.

  Dim htf As HtmlFragment = New HtmlFragment("<span style='color: #ff0000; white-space: nowrap; -ms-writing-mode: tb-rl; transform: rotate(180deg); background-color: #ffff00; -webkit-writing-mode: vertical-rl;'>123456</span>")
  Dim header As HeaderFooter = New HeaderFooter()
   header.Paragraphs.Add(htf)
  doc.Pages(1).Header = header
   doc.Save(fileFolder & fileName)

Screenshot (3).png (24.9 KB)

@eranlipi

Please make sure that you are using Aspose.PDF for .NET 21.3. In case you are using other Aspose APIs as well, you can initialize HeaderFooter Class with full namespace like Aspose.Pdf.HeaderFooter. Please share a sample application for our reference in case issue still persists. We will again test the scenario in our environment and address it accordingly.

1 Like