in

simplusoft.net

another .NET open source community

iulia

June 2008 - Posts

  • Search content in pdf files stored into Sql Server


    If you have some .pdf files stored into Sql Server and you want to search content in them, the steps are:

    1. Suppose you allready have a table into sql server that have a column "FileContent" of type varbinary

    (MAX). Check this. Add a column "Extension" of type nvarchar(50) where you have to store the exact

    extension for your file; of course, in our particular case the value will be ".pdf". This column will be

    used when you will create an full text search index on the column "FileContent"

    2. Enable creation of full text index search on your Sql database


     EXEC sp_fulltext_database 'enable'
     go

     sp_fulltext_service 'load_os_resources',1
     go

     sp_fulltext_service 'verify_signature', 0
     go


    3. Create the full text catalog
     create fulltext catalog CatalogTest
     go


    4. Create the full text index on the column "Filecontent"


     CREATE FULLTEXT INDEX ON CatalogTest.tblDocs(FileContent) KEY INDEX PK_extra_Docs;
     go

    where "tblDocs" is the table that have the column "FileContent" and "Extension"

    5. Of course, now you will try to test the full text search index, suposing that you allready have some

    .pdf files inserted into database. But will not work. In order to can search in your pdf files, you have

    to install on the Sql Server the Adobe PDF Filter. You can take Adobe PDF Filter from here AdobePdfFilter.zip

    6. Now you can check to search by using the query sample below

    declare @SearchTags nvarchar(1000)


    set @SearchTags = 'this is a text to search in pdf content'


    select *
    from tblDocs
    where
     (@SearchTags = ''
      or (@SearchTags <> '' 
        and CONTAINS(FileContent, @SearchTags))
     )

  • Print local reports .rdlc from report viewer


    If you want to print a local .rdlc report, you can't use the property "ShowPrintButton" for the reportviewer control. This property event it is set to true, will work only when the report is processed remote (ProcessingMode=remote), case when you have to install Reporting Services where to deploy your report.

    But, if you don't want to install Reporting Services, but you want to have the facility to print your report, a solution is to save locally as .pdf your report and after that to open directely the file to your user. So, the user can just print the opened file using print button from Adobe Reader.

    In my example, I have write a method wich reveive as parameter an report viewer object, and the page where the report viewer is containing:

      public static void PrintLocalRdlc(Microsoft.Reporting.WebForms.ReportViewer rwRpt, Page page)
        {
            DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/Upload/"));
            foreach (FileInfo fi in di.GetFiles())
            {
                if (fi.Name.IndexOf("_"
                                    + HttpContext.Current.Session.SessionID
                                    + "_")
                    != -1)
                {
                    fi.Delete();
                }
            }

            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;

            byte[] pdfContent
                = rwRpt.LocalReport.Render
                ("PDF", null, out mimeType, out encoding, out fileNameExtension,
                out streams, out warnings);

            string realUrl = "~/Upload/Report_"
                                + HttpContext.Current.Session.SessionID
                                + "_"
                                + Guid.NewGuid().ToString()
                                + ".pdf";
            string pdfPath
                = HttpContext.Current.Server.MapPath(realUrl);
            System.IO.FileStream pdfFile = new System.IO.FileStream(pdfPath, System.IO.FileMode.Create);
            pdfFile.Write(pdfContent, 0, pdfContent.Length);
            pdfFile.Close();

            string windowOpen = "printWin = window.open('" + page.ResolveUrl(realUrl) + "', '_blank','height=800,left=100,top=100,width=1200,toolbar=no,titlebar=0,status=0,menubar=yes,location=no,scrollb ars=1' " + ");printWin.focus();self.print();";
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), windowOpen, true);
        }


    The example can be downloaded from PrintLocalRdlc.zip

  • Catch Web Browser Close Event - IE and Firefox

    Sometime you need to catch the event of browser close. There are some situations when you
    need to create some web application reports and you need to compute the totalm time spend by user
    to visit each page, case when you need the exact moment when the user close the web page.
    Or, you need to implement an engine for clear all the session variables used for a certain page.
    To catch the close browser event we have to use different code in order to implement this in Internet Explorer and Firefox.
    The html event is the same for both cases: onbeforeunload.But the javascript is different depending on the client browser; i have put the scripts only for the Internet Explorer and for the Mozilla Firefox.


     if (Request.Browser.Browser.IndexOf("IE") != -1)
            {
                this.ClientScript.RegisterStartupScript(
                     this.GetType()
                    , Guid.NewGuid().ToString()
                    , "document.body.onbeforeunload = function()"
                        + "{alert('window close - internet explorer');}\n", true);
            }
            else
                if (Request.Browser.Browser.ToLower().IndexOf("firefox") != -1)
                {
                    HtmlGenericControl body = (HtmlGenericControl)this.FindControl("hostBody");
                    if (body != null)
                        body.Attributes.Add("onbeforeunload", "alert('window close - firefox');");
                }


    The problem is that we can not use the same method RegisterStartupScript when the client browser is Firefox, because this method does not have the expected execution as it is for Internet Explorer.
    So, we are forced for Firefox to add attributes to the html body; for this we have to find programatically the html body, which in my example it is called "hostBody":

    <body id="hostBody" runat="server">


    The example can be downloaded from CatchBrowserCloseEvent.zip
     

More Posts
Simplusoft.net is a non-profit community dedicated to all .NET developers
Powered by Community Server (Non-Commercial Edition), by Telligent Systems