Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Allow custom User-Agent header configuration for HttpClient requests.

using System;

class UserAgentHandler : Aspose.Html.Net.MessageHandler
{
public override void Invoke(Aspose.Html.Net.INetworkOperationContext context)
{
context.Request.Headers["User-Agent"] = "MyCustomUserAgent/1.0";
Next(context);
}
}

class Program
{
static void Main()
{
try
{
Aspose.Html.Configuration configuration = new Aspose.Html.Configuration();
Aspose.Html.Services.INetworkService network = configuration.GetService<Aspose.Html.Services.INetworkService>();
network.MessageHandlers.Add(new UserAgentHandler());

using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument("https://example.com", configuration))
{
Aspose.Html.Saving.PdfSaveOptions options = new Aspose.Html.Saving.PdfSaveOptions();
Aspose.Html.Converters.Converter.ConvertHTML(document, options, "output.pdf");
}

Console.WriteLine("Conversion completed successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Batch process multiple page URLs by looping through a list and applying the extraction workflow.

using System;
using System.IO;
using System.Collections.Generic;
using System.Text.Json;
using Aspose.Html;
using Aspose.Html.Dom;
using Aspose.Html.Collections;
using Aspose.Html.IO;

class OutlineNode
{
public string Text { get; set; }
public int Level { get; set; }
public List<OutlineNode> Children { get; set; }
}

class Program
{
static void Main()
{
try
{
string inputFolder = "InputHtml";
string outputFolder = "OutputJson";

if (!Directory.Exists(inputFolder))
Directory.CreateDirectory(inputFolder);
if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder);

// Create a sample HTML file if none exist
string[] existingFiles = Directory.GetFiles(inputFolder, "*.html");
if (existingFiles.Length == 0)
{
string samplePath = Path.Combine(inputFolder, "sample.html");
string sampleContent = "<html><body><h1>Title</h1><h2>Section</h2><h3>Subsection</h3></body></html>";
File.WriteAllText(samplePath, sampleContent);
}

foreach (string htmlPath in Directory.GetFiles(inputFolder, "*.html"))
{
HTMLDocument document = new HTMLDocument(htmlPath);
var headings = document.QuerySelectorAll("h1, h2, h3, h4, h5, h6");
var root = new List<OutlineNode>();
var stack = new Stack<OutlineNode>();

for (int i = 0; i < headings.Length; i++)
{
HTMLElement element = (HTMLElement)headings[i];
string tag = element.TagName.ToLower();
int level = int.Parse(tag.Substring(1));

OutlineNode node = new OutlineNode
{
Text = element.TextContent.Trim(),
Level = level,
Children = new List<OutlineNode>()
};

while (stack.Count > 0 && stack.Peek().Level >= level)
stack.Pop();

if (stack.Count == 0)
root.Add(node);
else
stack.Peek().Children.Add(node);

stack.Push(node);
}

string json = JsonSerializer.Serialize(root, new JsonSerializerOptions { WriteIndented = true });
string jsonPath = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(htmlPath) + ".json");
File.WriteAllText(jsonPath, json);
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Cache downloaded external SVG files locally to avoid redundant network requests.

using System;
using System.IO;
using System.Net.Http;
using Aspose.Html.Dom.Svg;
using Aspose.Html.Saving;
using Aspose.Html.Converters;
using Aspose.Html.Rendering.Image;

class Program
{
static void Main()
{
try
{
// Sample SVG URLs
string[] svgUrls = new string[]
{
"https://upload.wikimedia.org/wikipedia/commons/6/6b/Bitmap_VS_SVG.svg",
"https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/acid.svg"
};

// Cache directory
string cacheDir = Path.Combine(Directory.GetCurrentDirectory(), "svgCache");
Directory.CreateDirectory(cacheDir);

// HttpClient for downloading
using (HttpClient httpClient = new HttpClient())
{
foreach (string url in svgUrls)
{
// Determine local file name
string fileName = Path.GetFileName(new Uri(url).AbsolutePath);
if (string.IsNullOrEmpty(fileName))
{
fileName = Guid.NewGuid().ToString() + ".svg";
}

string localPath = Path.Combine(cacheDir, fileName);

// Download if not cached
if (!File.Exists(localPath))
{
byte[] data = httpClient.GetByteArrayAsync(url).Result;
File.WriteAllBytes(localPath, data);
Console.WriteLine($"Downloaded and cached: {url}");
}
else
{
Console.WriteLine($"Using cached file for: {url}");
}

// Load SVG content
string svgContent = File.ReadAllText(localPath);
string baseUri = new Uri(localPath).AbsoluteUri;

// Prepare output image path
string outputImagePath = Path.Combine(Directory.GetCurrentDirectory(),
Path.GetFileNameWithoutExtension(fileName) + ".png");

// Set image save options
ImageSaveOptions options = new ImageSaveOptions();
options.Format = ImageFormat.Png;

// Convert SVG to PNG
Aspose.Html.Converters.Converter.ConvertSVG(svgContent, baseUri, options, outputImagePath);
Console.WriteLine($"Converted to PNG: {outputImagePath}");
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}

This file was deleted.

Loading
Loading