Skip to main content

XRPdfContent.SourceUrl Property

Specifies the PDF document to be rendered in the report. Use a local file system path or URL to reference a document.

Namespace: DevExpress.XtraReports.UI

Assembly: DevExpress.XtraReports.v24.2.dll

NuGet Package: DevExpress.Reporting.Core

Declaration

[DefaultValue("")]
[SRCategory(ReportStringId.CatData)]
public string SourceUrl { get; set; }

Property Value

Type Default Description
String String.Empty

The URL or path to a PDF document that should be rendered in the report.

Remarks

When users save the report, the URL or path specified in the SourceUrl property persists in the report definition file. The PDF file should be available at the specified location when a report is printed or rendered in a Document Viewer.

You can use expressions to conditionally specify the SourceUrl property value or bind it to a report parameters or data source field. Add an item to the XRPdfContent.ExpressionBindings collection to set an expression.

Use the Source property instead of SourceUrl to include binary PDF content in a report.

Tip

The SourceUrl property value takes precedence over the Source value. If you specify both properties, the XRPdfContent control includes the content specified by SourceUrl. However, if the file specified in the SourceUrl property cannot be loaded, binary data from the Source property is used.

Specify a PDF File Path

You can assign an absolute or relative path to the SourceUrl property.

A relative path refers to the location relative to the base directory. The following table shows which folder is considered to be the base directory:

Application in Visual Studio (Design Time or Runtime) Deployed Application
The application project directory The application directory

Before deployment, you must configure the application to ensure that relative paths are resolved correctly. Follow the steps below that describe how to configure the application on different platforms.

Any Platform

For all resource files (image or document), do the following:

  • Set the Copy to Output Directory property to Copy Always.
  • Set the Build Action property to Content.

Desktop

To specify paths relative to another directory (not the application BaseDirectory), set the global DXResourceDirectory variable:

// Set the resource directory to the MyResources folder in the user profile.
string dataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "MyResources");
AppDomain.CurrentDomain.SetData("DXResourceDirectory", dataPath);

ASP.NET Web Forms & MVC

You have two options – either use the DirectoryAccessRule class or set the DXResourceDirectory global variable.

DirectoryAccessRule
  • An application includes Web Reporting controls.
    Implement the following code snippet in your application to allow access to a directory that contains resources (images or documents), since Reporting controls only allow access to URLs.

    using System;
    using DevExpress.Security.Resources;
    // ...
    public class Global_asax : System.Web.HttpApplication {
    
      protected void Application_Start(object sender, EventArgs e) {
          // Set access rules to data
          string dataPath = Server.MapPath("MyResources");
          AccessSettings.StaticResources.TrySetRules(DirectoryAccessRule.Allow(dataPath));
      }
      // ...
    }
    
  • An application prints or exports reports from code and does not include Web Reporting controls.
    There is no need to set directory access rules.

DXResourceDirectory
  • An application includes Web Reporting controls.
    There is no need to set this variable. Reporting controls resolve relative paths based on the application directory.
  • An application prints or exports reports from code and does not contain Web Reporting controls. Use the following code snippet:

    public class Global : System.Web.HttpApplication {
    
      protected void Application_Start(object sender, EventArgs e) {
          // Set the resource directory
          string contentPath = Server.MapPath("");
          AppDomain.CurrentDomain.SetData("DXResourceDirectory", contentPath);
      }
      // ...
    }
    

ASP.NET Core

You have two options – either use the DirectoryAccessRule class or set the DXResourceDirectory global variable.

DirectoryAccessRule
  • An application includes Web Reporting controls.
    Implement the following code snippet in your application to allow access to a directory that contains resources (images or documents), since Reporting controls only allow access to URLs.

    using DevExpress.Security.Resources;
    
    //...
    
    var app = builder.Build();
    
    // Set access rules to data
    string dataPath = System.IO.Path.Combine(contentPath, "Data");
    string dataPath2 = System.IO.Path.Combine(contentPath, "wwwroot\\Resources");
    AccessSettings.StaticResources.TrySetRules(DirectoryAccessRule.Allow(contentPath));
    AccessSettings.StaticResources.TrySetRules(DirectoryAccessRule.Allow(dataPath, dataPath2));
    
    app.Run();
    
  • An application prints or exports reports from code and does not include Web Reporting controls.
    There is no need to set directory access rules.

DXResourceDirectory
  • An application includes Web Reporting controls.
    There is no need to set this variable. Reporting controls resolve relative paths based on the application directory.
  • An application prints or exports reports from code and does not include Web Reporting controls. Use the following code snippet:

    var app = builder.Build();
    
    // Set the resource directory
    string contentPath = env.ContentRootPath;
    AppDomain.CurrentDomain.SetData("DXResourceDirectory", contentPath);
    
    app.Run();
    

Examples

Set a Constant Path to the PDF Document

The code below creates an XRPdfContent control, adds it to a report’s Detail band, and sets the control’s SourceUrl property to the path to the PDF file.

using System.IO;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Expressions;
// ...
// Create a report that has a Detail band and uses Landscape page orientation.
XtraReport report = new XtraReport(){
    Landscape = true,
    Bands = {
        new DetailBand() {
            Name = "DetailBand",
            HeightF = 25,
        }
    }
};
// Create an XRPdfContent class instance.
XRPdfContent pdfContent = new XRPdfContent();
// PDF content is loaded from the MasterDetail Report.pdf file.
pdfContent.SourceUrl = "MasterDetail Report.pdf";
// Add the XRPdfContent control to the Detail band.
report.Bands[BandKind.Detail].Controls.Add(pdfContent);

Bind the SourceUrl Property to a Report Parameter

The code below creates an XRPdfContent control and uses an expression to bind the control’s SourceUrl property to the report’s PdfParameter parameter.

using System.IO;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Expressions;
// ...
// Create a report that has a Detail band and uses Landscape page orientation.
// The "PdfParameter" report parameter specifies the PDF file location.
XtraReport report = new XtraReport(){
    Landscape = true,
    Parameters = {
        new DevExpress.XtraReports.Parameters.Parameter() {
            Name = "PdfParameter",
            Type = typeof(string),
            Value = "MasterDetailReport.pdf",
            Visible = true
        },
    },
    Bands = {
        new DetailBand() {
            Name = "DetailBand",
            HeightF = 25,
        }
    }
};
// Create an XRPdfContent class instance.
XRPdfContent pdfContent = new XRPdfContent();
// Create an expression that binds the XRPdfContent's SourceUrl property to the report's "PdfParameter" parameter.
pdfContent.ExpressionBindings.Add(new ExpressionBinding("SourceUrl", "?PdfParameter"));
// Add the XRPdfContent control to the Detail band.
report.Bands[BandKind.Detail].Controls.Add(pdfContent);
See Also