Skip to content

View PDF as part of the MVC page

I am trying to view a PDF document in my MVC web page, but I cant make it to work.

I would like the PDF to be displayed as a part of the other stuff on the page (header, footer etc.). Currently I have a solution where the PDF is shown, but on the entire page.-

Has anybody done this, if yes then how?


Why don’t you try using iframe like this :

<iframe src="even file stream action url"></iframe>

I suggest to use object tag if it’s possible, use iframe just for testing.

If you want to render PDF as part of the page as you just did

src='<% Html.RenderAction(“GetPDF”); %>’

Then this is your option

If you need complete control over PDF content using CSS or whatsoever, like Google books and so on, then you need tools that help you to convert each requested page of PDF to Plain Text, HTML or even image. tools like PDFsharp. Search Google For Tools

If you want display PDF as part of the page then this is what you have to do

ASPX: src="<%= Url.Action("GetPDF") %>"
Razor: src="@Url.Action("GetPDF")"

And final answer could be

<object data="<%= Url.Action("GetPDF") %>" type="application/pdf" width="300" height="200">
    alt : <a href="data/test.pdf">test.pdf</a>

And in the case that you want to return PDF as Stream then you need

public FileStreamResult GetPDF()
    FileStream fs = new FileStream("c:\\PeterPDF2.pdf", FileMode.Open, FileAccess.Read);
    return File(fs, "application/pdf");

Client-Side Validation for the CheckBoxes Inside a GridView

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
      <asp:BoundField HeaderText="n" DataField="sno"> 
       <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
       <asp:TemplateField HeaderText="Select">
               <asp:CheckBox ID="chkBxSelect" runat="server" />
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
 <asp:Button ID="btnPost" runat="server" Text="Post" 
             OnClientClick="javascript:return TestCheckBox();"
             OnClick="btnPost_Click" />

javascript code:
<script type="text/javascript">
   var TargetBaseControl = null;
   window.onload = function()
         //get target base control.
         TargetBaseControl = 
           document.getElementById('<%= this.GridView1.ClientID %>');
         TargetBaseControl = null;
   function TestCheckBox()
      if(TargetBaseControl == null) return false;
      //get target child control.
      var TargetChildControl = "chkBxSelect";
      //get all the control of the type INPUT in the base control.
      var Inputs = TargetBaseControl.getElementsByTagName("input"); 
      for(var n = 0; n < Inputs.length; ++n)
         if(Inputs[n].type == 'checkbox' && 
            Inputs[n].id.indexOf(TargetChildControl,0) >= 0 && 
          return true;        
      alert('Select at least one checkbox!');
      return false;


How to get list of all domain controllers in C#

List all computers in active directory

If you have a very big domain, or have setup limits on your domain how how many items can be returned pr search you might have to use paging.

public static List<string> GetComputers()
    List<string> ComputerNames = new List<string>();

    DirectoryEntry entry = new DirectoryEntry("LDAP://");
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = ("(objectClass=computer)");
    mySearcher.SizeLimit = int.MaxValue;
    mySearcher.PageSize = int.MaxValue;

    foreach(SearchResult resEnt in mySearcher.FindAll())
    	string ComputerName = resEnt.GetDirectoryEntry().Name;
    	if (ComputerName.StartsWith("CN="))
    		ComputerName = ComputerName.Remove(0,"CN=".Length);


        return ComputerNames;

--- -- ---- ------------------------------------------------------------------ ------ - -- -                          -- -             ---------------------

Get computers and domains in Active Directory

I want to recover a complete list of computers and the domains that they are in from Active Directory using the functionailty built in to .NET 2.0.  I have determined how to get ALL computers in the default domain using the following code:

// Identify the ‘default’ AD/LDAP Server
DirectoryEntry defaultServer = new DirectoryEntry(“LDAP://rootDSE”);
string strLdapServer = (string)defaultServer.Properties[“defaultNamingContext”].Value;
DirectoryEntry mySearchRoot = new DirectoryEntry(“LDAP://” + strLdapServer);

// Create a ‘DirectoryEntry’ object to search.
DirectorySearcher myDirectorySearcher = new DirectorySearcher(mySearchRoot);
myDirectorySearcher.Filter = (“(objectClass=computer)”);

// Iterate through (any) results
foreach(SearchResult resEnt in myDirectorySearcher.FindAll())
// Get the ‘DirectoryEntry’ that corresponds to ‘mySearchResult’.
DirectoryEntry myDirectoryEntry = resEnt.GetDirectoryEntry();
string strComputer = myDirectoryEntry.Name.ToString();

…and I have also determined code to recover a list of domains defined within AD as :

List<string> domains = new List<string>()
DirectoryEntry en = new DirectoryEntry(“LDAP://”);

// Search for objectCategory type “Domain”
DirectorySearcher srch = new DirectorySearcher(“objectCategory=Domain”);
SearchResultCollection coll = srch.FindAll();

// Enumerate over each returned domain.
foreach (SearchResult rs in coll)
ResultPropertyCollection resultPropColl = rs.Properties;
foreach (object domainName in resultPropColl[“name”])

…now what I want to do is to use the name of the domain returned as above to recover the name of any computers within this domain.  I did try using the following code:

string ldapString = “LDAP://” + domainName + “/CN=Computers,DC=” + domainName + “,DC=com”;
DirectoryEntry domain = new DirectoryEntry(ldapString);
foreach (DirectoryEntry child in domain.Children)
string computerName = child.Name;
if (computerName != “”)
computers.Add(new string[] { computerName, domainName });

But this fails to connect to the AD server.  Ultimately I need some code which, given the domain name, is able to query the AD server for that domain and recover the list of computers on that domain but I seem to be having problems determining the correct LDAP string.

Chapter 52: Directory Services –

Enumerate Global Catalogs in the Current Forest

public static ArrayList EnumerateDomains()
    ArrayList alGCs = new ArrayList();
    Forest currentForest = Forest.GetCurrentForest();
    foreach (GlobalCatalog gc in currentForest.GlobalCatalogs)
    return alGCs;

Enumerate Domain Controllers in a Domain

public static ArrayList EnumerateDomainControllers()
    ArrayList alDcs = new ArrayList();
    Domain domain = Domain.GetCurrentDomain();
    foreach (DomainController dc in domain.DomainControllers)
    return alDcs;