Split HTML Webpage into Multiple Files Programmatically using C#

Split HTML Webpage

HTML files are usually used to display information on websites or web applications. It is actually a mark-up text which lets you format the text as well. Sometimes HTML files are quite big because they might contain a lot of data including text, images, charts, diagrams, and many other contents. You may need to split HTML files under different requirements or use cases. Let us explore how to split an HTML file into multiple files using C#.

HTML Splitter in C# – API Installation

Aspose.HTML for .NET API supports creating or manipulating HTML webpages in your .NET Framework-based applications using C# programming language. You can easily install the API by downloading the latest DLL files from the Downloads section, or via NuGet gallery with the following installation command:

PM> Install-Package Aspose.Html

Split HTML Webpage into Multiple Files in C#

Aspose.HTML for .NET API has a rich set of features. It allows to copy content of loaded HTML document into another HTML document. However, since the HTML is not a fixed-layout format and does not has pages until we print it into particular device like PDF, DOCX, etc. So we cannot find any method or property which can split the content into pages like other fixed-layout formats. Therefore, you need to find the proper place to split the HTML and design the splitting logic accordingly.

In this example, we will be considering an example where you need to split an HTML webpage based on paragraph elements. Therefore, the following steps show how to split HTML webpage by paragraph:

  1. Load input HTML webpage
  2. Find all paragraph elements inside HTML document by using Query Selector
  3. Create an empty document to split HTML file
  4. Save output HTML files

The code snippet below further elaborates how to split HTML webpage into multiple files using C#:

Conclusion

In this article, we have learned about splitting an HTML webpage into multiple files using C#. We have considered the example of splitting the file based on different paragraph elements. Likewise, you can improvise the logic to split 5 or 10 paragraphs in each output file. There can be different other use cases for splitting the HTML webpage as per your requirements. Please feel free to write to us at Free Support Forum if you need any assistance about any other use case or scenario. We will be honored to assist you!

See Also

Convert HTML to Plain Text or Extract Text from HTML in C#