Click or drag to resize

PdfMetamorphosisHtmlSettingBaseUrl Property

The part of document’s URL preceding its name. Default value: Empty.

Namespace: SautinSoft
Assembly: PdfMetamorphosis (in PdfMetamorphosis.dll) Version: 2024.1.12
Syntax
public string BaseUrl { get; set; }

Property Value

String
Remarks
URL assigned to a page to convert "relative URLs" into "absolute URLs" by the addition of a document name or trailing slash /.

It’s very important to set this property when converting HTML document in memory in order to determine where all relative URLs are pointing to and convert them to absolute.

For example, let's convert HTML string to a PDF file.

Assuming we've specified:

p.HtmlOptions.BaseUrl = @"d:\my webs";

Now when the component meets 'src="images/asterisk.jpg"' it will try to load an image from: "d:\my webs\images\asterisk.jpg"
Example
Using the property BaseUrl in ASP.Net application using C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        Result.Text = "";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        // Activate your license here
        // SautinSoft.PdfMetamorphosis.SetLicense("1234567890");

        SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();

        // Let's set page numbers
        p.PageSettings.Numbering.Text = "Page {page} of {numpages}";

        // Set page header within HTML string
        p.PageSettings.Header.FromString("<table border=\"1\"><tr><td>We added this header using the property \"Header.Html\"</td></tr></table>", SautinSoft.PdfMetamorphosis.HeadersFooters.InputFormat.Html);

        // Add page footer from HTML file
        p.PageSettings.Footer.FromFile(Path.Combine(Server.MapPath(""), @"footer.htm"), SautinSoft.PdfMetamorphosis.HeadersFooters.InputFormat.Html);

        // Get content of the ASPX page as HTML document
        string htmlString = GetHtmlFromAspx(Path.Combine(Server.MapPath(""), @"Default.aspx"));

        p.HtmlSettings.BaseUrl = Server.MapPath("");        

        byte[] pdfBytes = p.HtmlToPdfConvertStringToByte(htmlString);

        //show PDF
        if (pdfBytes != null)
        {
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = "application/PDF";
            Response.BinaryWrite(pdfBytes);
            Response.Flush();
            Response.End();
        }
        else
        {
            Result.Text = "Converting failed!";
        }
    }
    public static string GetHtmlFromAspx(string url)
    {
        string contents = "";
        string urlpage = HttpContext.Current.Request.Url.AbsoluteUri;


        Stream StreamHttp = null;
        WebResponse resp = null;
        HttpWebRequest webrequest = null;

            webrequest = (HttpWebRequest)WebRequest.Create(urlpage);
            webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";
            resp = webrequest.GetResponse();
            StreamHttp = resp.GetResponseStream();
            StreamReader sr = new StreamReader(StreamHttp);
            contents = sr.ReadToEnd();
            return contents;

        }}
Using the property BaseUrl in ASP.Net application using VB.Net
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Net

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        ' Activate your license here
        ' SautinSoft.PdfMetamorphosis.SetLicense("1234567890")
        Dim p As New SautinSoft.PdfMetamorphosis()

        ' Let's set page numbers
        p.PageSettings.Numbering.Text = "Page {page} of {numpages}"

        ' Set page header within HTML string
        p.PageSettings.Header.FromString("<table border=""1""><tr><td>We added this header using the property ""Header.Html""</td></tr></table>", SautinSoft.PdfMetamorphosis.HeadersFooters.InputFormat.Html)

        ' Add page footer from HTML file
        p.PageSettings.Footer.FromFile(Path.Combine(Server.MapPath(""), "footer.htm"), SautinSoft.PdfMetamorphosis.HeadersFooters.InputFormat.Html)

        ' Get content of the ASPX page as HTML document
        Dim htmlString As String = GetHtmlFromAspx(Path.Combine(Server.MapPath(""), "Default.aspx"))

        p.HtmlSettings.BaseUrl = Server.MapPath("")

        Dim pdfBytes() As Byte = p.HtmlToPdfConvertStringToByte(htmlString)

        'show PDF
        If pdfBytes IsNot Nothing Then
            Response.Buffer = True
            Response.Clear()
            Response.ContentType = "application/PDF"
            Response.BinaryWrite(pdfBytes)
            Response.Flush()
            Response.End()
        Else

        End If
    End Sub
    Public Shared Function GetHtmlFromAspx(ByVal url As String) As String
        Dim contents As String = ""
        Dim urlpage As String = HttpContext.Current.Request.Url.AbsoluteUri


        Dim StreamHttp As Stream = Nothing
        Dim resp As WebResponse = Nothing
        Dim webrequest As HttpWebRequest = Nothing

            webrequest = CType(WebRequest.Create(urlpage), HttpWebRequest)
            webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)"
            resp = webrequest.GetResponse()
            StreamHttp = resp.GetResponseStream()
            Dim sr As New StreamReader(StreamHttp)
            contents = sr.ReadToEnd()
            Return contents

    End Function
End Class
See Also