The part of document’s URL preceding its name.

Namespace:  SautinSoft
Assembly:  PdfMetamorphosis (in PdfMetamorphosis.dll) Version: 6.7.12.19

Syntax

      
 C#  Visual Basic 
public string BaseUrl { get; set; }
Public Property BaseUrl As String

Remarks

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

Default value: empty string

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"

Examples

CopyUsing the property BaseUrl in ASP.Net application
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)
    {
        SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();

        // After purchasing the license, please insert your serial number here to activate the component
        //p.Serial = "XXXXXXXXXXX";

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

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

        // Add page footer from HTML file
        p.PageSettings.Footer.FromHtmlFile(Path.Combine(Server.MapPath(""), @"footer.htm"));

        // 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 = "";

        if (url.Length > 6)
        {
            //open 'http://' file
            if ((url[0] == 'h' || url[0] == 'H') && (url[1] == 't' || url[1] == 'T') &&
                (url[2] == 't' || url[2] == 'T') && (url[3] == 'p' || url[3] == 'P') &&
                url[4] == ':' && url[5] == '/' && url[6] == '/')
            {

                Stream StreamHttp = null;
                WebResponse resp = null;
                HttpWebRequest webrequest = null;
                try
                {
                    webrequest = (HttpWebRequest)WebRequest.Create(url);
                    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;
                }
                catch
                {
                }
            }
            //local file
            else
            {
                try
                {
                    StreamReader sr = new StreamReader(url);
                    contents = sr.ReadToEnd();
                    sr.Close();

                }
                catch
                {
                }

            }
        }
        return contents;
    }
}
CopyUsing the property BaseUrl in ASP.Net application
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)
        Result.Text = ""
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim p As New SautinSoft.PdfMetamorphosis()

        ' After purchasing the license, please insert your serial number here to activate the component
        'p.Serial = "XXXXXXXXXXX";


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

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

        ' Add page footer from HTML file
        p.PageSettings.Footer.FromHtmlFile(Path.Combine(Server.MapPath(""), "footer.htm"))

        ' 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
            Result.Text = "Converting failed!"
        End If
    End Sub
    Public Shared Function GetHtmlFromAspx(ByVal url As String) As String
        Dim contents As String = ""

        If url.Length > 6 Then
            'open 'http://' file
            If (url.Chars(0) = "h"c OrElse url.Chars(0) = "H"c) AndAlso (url.Chars(1) = "t"c OrElse url.Chars(1) = "T"c) AndAlso (url.Chars(2) = "t"c OrElse url.Chars(2) = "T"c) AndAlso (url.Chars(3) = "p"c OrElse url.Chars(3) = "P"c) AndAlso url.Chars(4) = ":"c AndAlso url.Chars(5) = "/"c AndAlso url.Chars(6) = "/"c Then

                Dim StreamHttp As Stream = Nothing
                Dim resp As WebResponse = Nothing
                Dim webrequest As HttpWebRequest = Nothing
                Try
                    webrequest = CType(WebRequest.Create(url), HttpWebRequest)
                    resp = webrequest.GetResponse()
                    StreamHttp = resp.GetResponseStream()
                    Dim sr As New StreamReader(StreamHttp)
                    contents = sr.ReadToEnd()
                    Return contents
                Catch
                End Try
                'local file
            Else
                Try
                    Dim sr As New StreamReader(url)
                    contents = sr.ReadToEnd()
                    sr.Close()

                Catch
                End Try

            End If
        End If
        Return contents
    End Function
End Class

See Also