How to set a document title in C# and .NET


In modern .NET application development, document conversion and formatting are often necessary. One popular tool is the component RTF TO HTML .NET from SautinSoft SDK. In this article, we'll look at how to set a document title when converting RTF to HTML.

Here are some reasons why setting HTML titles is important:

  • Search engine optimization (SEO): A proper title helps search engines better understand the page's content.
  • Improving user experience: The title is displayed in the browser tab, facilitating navigation.
  • Facilitating automated document processing: Systems can use titles for indexing and content organization.

This method is widely used. Many systems require automatic generation of HTML documents from RTF sources, especially in cases where unique titles must be maintained for SEO or internal organization.

Interesting nuances and additional features:

  • Automation: The script can be extended to extract headings from RTF or another source and insert them automatically.
  • Tags and comments: Insert not only the heading, but also metadata about creation, author, or date.
  • Integration with other systems: Use libraries for further HTML processing, such as inserting scripts, styles, or additional meta tags.
  • Image and style processing: For more complex documents, consider preserving graphics and formatting.

If you are looking also for a .Net solution to Create or Modify HTML documents, see our Document .Net.

Complete code

using System;
using System.IO;
using System.Text;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            string inpFile = @"..\..\..\Images.rtf";
            string outFile = @"..\..\..\Title.html";

            SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();
            SautinSoft.RtfToHtml.HtmlFixedSaveOptions opt = new SautinSoft.RtfToHtml.HtmlFixedSaveOptions
            {
                // Set document title, <title>...</title>
                Title = "Here is the custom TITLE!",
                // If you want to disable embedded images
                EmbedImages = false,
                // Set the folder to store images
                ImagesDirectoryPath = @"..\..\..\Images",
                // Set the folder in <image src="..." >
                ImagesDirectorySrcPath = "Images"
            };

            try
            {
                r.Convert(inpFile, outFile, opt);

                // Open the results for demonstration purposes.
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile)
                { UseShellExecute = true });
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error: {e.Message}");
                Console.WriteLine("Press any key ...");
                Console.ReadKey();
            }
        }
    }
}

Download

Imports System
Imports System.IO
Imports System.Text

Namespace Sample
	Friend Class Sample
		Shared Sub Main(ByVal args() As String)
			Dim inpFile As String = "..\..\..\Images.rtf"
			Dim outFile As String = "..\..\..\Title.html"

			Dim r As New SautinSoft.RtfToHtml()
			Dim opt As New SautinSoft.RtfToHtml.HtmlFixedSaveOptions With {
				.Title = "Here is the custom TITLE!",
				.EmbedImages = False,
				.ImagesDirectoryPath = "..\..\..\Images",
				.ImagesDirectorySrcPath = "Images"
			}

			Try
				r.Convert(inpFile, outFile, opt)

				' Open the results for demonstration purposes.
				System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
			Catch e As Exception
				Console.WriteLine($"Error: {e.Message}")
				Console.WriteLine("Press any key ...")
				Console.ReadKey()
			End Try
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:


Captcha

Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.