@GlebLevotsky
To begin with, we would like to say that it is better to set page margins not through CSS, but through the PdfSaveOptions class, since at the moment there is a problem with their processing.
Regarding the issue itself, we can offer two ways to add an image to the title with a short URL.
The first is to specify the name of the picture that he will put in the directory next to the library. Then CSS can be abbreviated as follows:
@page {
/* Page counter located at the bottom of the page */
@bottom-right
{
-aspose-content: "Page" currentPageNumber() " of " totalPagesNumber();
color: green;
}
/* Page title located at the top-center box */
@top-center
{
-aspose-content: url(name.svg);
vertical-align: bottom;
color: blue;
}
}
The second way is to create a custom MessageHandler and set the path to the image through a custom protocol. This approach can be used when the file system is not available. The implementation of this approach is shown in the following code snippet:
@page {
/* Page counter located at the bottom of the page */
@bottom-right
{
-aspose-content: "Page" currentPageNumber() " of " totalPagesNumber();
color: green;
}
/* Page title located at the top-center box */
@top-center
{
-aspose-content: url(header://name.svg);
vertical-align: bottom;
color: blue;
}
}
using var configuration = new Configuration();
var userAgent = configuration.GetService<Aspose.Html.Services.IUserAgentService>();
userAgent.UserStyleSheet = cssData;
configuration.GetService<INetworkService>().MessageHandlers.Insert(0, new TitleMessageHandler());
using var document = new HTMLDocument(htmlTemplate, ".", configuration);
var options = new PdfSaveOptions
{
PageSetup =
{
AnyPage = new Page
{
Size = new Size(width: Length.FromInches(20), height: Length.FromInches(10)),
Margin = new Margin(Unit.FromCentimeters(2), Unit.FromCentimeters(1), Unit.FromCentimeters(1), Unit.FromCentimeters(1))
},
},
};
Converter.ConvertHTML(document, options, "example.pdf");
public class TitleMessageHandler : MessageHandler
{
public TitleMessageHandler()
{
Filters.Add(new ProtocolMessageFilter("header"));
}
public override void Invoke(INetworkOperationContext context)
{
context.Response = new ResponseMessage(HttpStatusCode.OK)
{
Request = context.Request,
Content = new StringContent(
@"<svg xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" width=""100"" height=""100"" viewBox=""0 0 100 100"">
<rect y=""0"" x=""0"" width=""100"" height=""100"" fill=""green"" />
</svg>"),
Headers = { ContentType = { MediaType = "image/svg+xml" } }
};
}
}