Friday, July 25, 2014

iTextSharp - Make the PDF file secure

When you use iTextSharp, which is a tool used to generate PDF, using C#, you can make the PDF secure. I mean by secure, you can modify the security properities of the PDF like printing, allow modification, allow copying...etc










You have just to write the following fragment: 

            // define the document
            Document document = new Document(PageSize.A4);
            document.SetMargins(0, 0, 20, 10);

            // create memory stream to save the file
            MemoryStream ms = new MemoryStream();
            PdfWriter p = PdfWriter.GetInstance(document, ms);

            // set security properties
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            p.SetEncryption(null, encoding.GetBytes("12"), PdfWriter.AllowPrinting, 
            PdfWriter.STRENGTH40BITS);

            // open the document
            document.Open();

            // generate the document content
            document.Add(PDF_FinanceCertificate(item, p, document));

            // close the final PDF document
            document.Close();

            byte[] pdfFile = ms.ToArray();

            return pdfFile;


Note that, because we implement security issue in the PDF, the generation operation will take more time more than usual.


Thursday, January 9, 2014

How To Add Twitter (Tweet Button) To Web Page

To add twitter button to tweet your page URL just do the following:

* add this line to HTML to show tweet button:

<a href="https://twitter.com/share" class="twitter-share-button" data-url="YOUR PAGE URL" data-text="PAGE TITLE OR DESCRIPTION OF THE TWEET" data-lang="en" data-size="small" data-count="none" data-hashtags="HASHTAG YOU WANT TO TWEET FOR">TWEET</a>


* add the following script in master page or your page:

<script>!function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = p + '://platform.twitter.com/widgets.js'; fjs.parentNode.insertBefore(js, fjs); } }(document, 'script', 'twitter-wjs');</script>

The result will be like this:


Notes:
- URL should be published, this means you can't share localhost or internal URL if you create javascript to get the current page URL.

- You can create javascript to get the current page title.

- You can remove the section data-hashtags if you want.

- You can change the value of data-lang to your specific language like ar for Arabic language.

- You can change the value of data-size to big to show bigger icon.



Sunday, September 22, 2013

CLR To Return Full Gregorian Date By Passed Um Alqura Month And Year

In this article I will show how to create new CLR to be registered in SQL database.
This CLR includes function to convert passed Um Alqura month and year, then returns full Gregorian date.
(I mean by full, the first day of the Um Alqura date with month and year passed converted to Gregorian)


By creating new SQL server project, in visual studio 2012:


Right click on project, add new SQL CLR C# user-defined function:



The beginning of the month: 
Just add the following code:

public partial class UserDefinedFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static DateTime ConvertToGregorianStartDate (int Year, int Month)
    {
        System.Globalization.UmAlQuraCalendar UMQ_Calender = new System.Globalization.UmAlQuraCalendar();
        return UMQ_Calender.ToDateTime(Year, Month, 1, 0,0,0,0);
    }
}


The end of the month:
Just add the following code:

[Microsoft.SqlServer.Server.SqlFunction]
    public static DateTime ConvertToGregorianEndDate(int Year, int Month)
    {
        System.Globalization.UmAlQuraCalendar UMQ_Calender = new System.Globalization.UmAlQuraCalendar();
        DateTime firstDayOfTheMonth = UMQ_Calender.ToDateTime(Year, Month, 1, 0, 0, 0, 0);
        return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
    }

Build the project and publish it to your DB.

if you execute the function created, you will get the following result:



Friday, September 20, 2013

Change Label From Persian Gulf To Arabian Gulf In Google Maps

By default, Google select Persian Gulf to be displayed as label on the gulf:

 To change it to Arabian Gulf, just add the following query string to Google map API URL:
region=SA

https://maps.googleapis.com/maps/api/js?sensor=false&language=SA&region=SA



Dealing With Google Maps - ASP.NET - Part (3)

In this article, I will show you how to store and retrieve data from SQL Server DB to Google map and vise versa.

Save data from Google map to SQL table:
Create table to store the following information:
  - Latitude
  - Logitude
  - Description of the location


our interface should be like the following:



After click SAVE button, information in text boxes should be saved to our table.




Retrieve data from SQL table to Google map:

Retrieve the data from SQL table by your own way, and define three public properties and assign value to them, those properties will called in HTML.

public string Longitude{get; set;}
public string Latitude{get; set;}
public string Desc{get; set;}

To change marker position to be like values stored in table, just change location properties from:
myCoordinates = new google.maps.LatLng( 24, 47);
to
myCoordinates = new google.maps.LatLng('<%= Latitude %>', '<%= Logitude %>');

now, we want to display location description if user click to the marker.
First change draggable property of the marker from true to false, to be read only.
Then, add the following to script:

// define new info window and assign location description
var infoWindow = new google.maps.InfoWindow({
            content: '<%= Desc %>'
        });

// add click listener to marker
google.maps.event.addListener(marker, 'click', function () {
            infoWindow.open(map, marker);
        });

and if you run the page, and click to marker, you will get the following result:


Enjoy ..

Monday, August 5, 2013

Dealing With Google Maps - ASP.NET - Part (2)

After showing how to load Google map in your asp.net page in this article, we will move to dealing with map marker and its properties.

Marker used to specify location using coordinates, called Longitude and Latitude.

to load marker in your map, just add the following script:

function initialize() {

        // initial location of marker
        var myCoordinates;
        var marker;

        var mapOptions = {

            // 24, 47 is saudi arabia coordinates
            center: new google.maps.LatLng(24, 47),
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById("googleMap"), mapOptions);

        // initial location of marker
        myCoordinates = new google.maps.LatLng(24, 47);

        // set marker properties
        marker = new google.maps.Marker({
            position: myCoordinates,
            map: map,
            draggable: true
        });
    }

    google.maps.event.addDomListener(window, 'load', initialize); 

if you play this page, you will get draggable marker in your map:


if you want marker read-only, just change the property of marker:
draggable : false

Now, we want to show coordinates( longitude and latitude ) in text boxes. Just add the following texts inside html body:

<input id="txtLat" type="text" runat="server" />
<input id="txtLong" type="text" runat="server" /> 

and add the following listener to your script (inside initialize() method):

google.maps.event.addListener(marker, 'drag', function () {

   document.getElementById("<% =txtLat.ClientID %>").value = marker.position.lat().toString();

   document.getElementById("<% =txtLong.ClientID %>").value = marker.position.lng().toString();

});

then play your page, and try to move marker, you will get the following:


there is a lot of events of marker, one of them 'dragend', which will called after marker dragged, 'click' .. etc.


Wednesday, July 31, 2013

Dealing With Google Maps - ASP.NET - Part (1)

In this article, I'll show you how to load Google map in your page. Then, we will move to dealing with markers, how to customize it and apply data which retrieved from SQL database to Google map.

First Step:
create new web application, using visual studio, then register the following reference to web page, or to your master page.

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"/>

Second Step:
just copy the following script and put it in your page in order to display Google map.

<script>
function initialize() { var mapOptions = {
     
 // 28, 47 is saudi arabia coordinates 
       center: new google.maps.LatLng(28, 47), 
       zoom: 5, mapTypeId: google.maps.MapTypeId.ROADMAP }; 
       var map = new google.maps.Map(document.getElementById("googleMap"), mapOptions); 
 }

// this listener will call the method initialize() when page load
google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

Third Step:
inside your page body, set the following div

<div id="googleMap" style="width:500px;height:380px;"></div>

then run the page, you will see the map in your page.


if you want to change map language to Arabic or another language, just pass Language code via query string like the following:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&Language=SA"/>