Wednesday 17 July 2013

Disable Right click in web page



If you have images (or anything else) on your website that want to protect then you can disable "right-click", which is how people get to the "Save As" option. This is not a perfect solution because anyone with advanced web experience can get around this, but it will stop most people from stealing anything you have a copyright on. You can use any (no need to use more than one) of the codes below to block "right-click" functionality, which will prevent most attempts to save your stuff.
Once you have selected the code from below that you want to use, follow these step…

1.    Paste the code you select from below into the box titled JavaScript for <head>


Disable Right-Click
<script type="text/javascript">
var message="Right-Click Disabled!";
function clickIE4(){
if (event.button==2){
alert(message); return false;
}
}
function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("alert(message);return false")
</script>


Disable Right-Click Without an Alert Message
<script type="text/javascript">
var message="";
function clickIE() {
if (document.all) {
(message);return false;
}
}
function clickNS(e) {
if (document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {
(message);
return false;
}
}
}
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS;
}else{
document.onmouseup=clickNS;
document.oncontextmenu=clickIE;
}
document.oncontextmenu=new Function("return false")
</script>

How to Disable Right-Click on Images Only
<script type="text/javascript">
var clickmessage="Right click disabled on images!"
function disableclick(e) {
if (document.all) {
if (event.button==2||event.button==3) {
if (event.srcElement.tagName=="IMG"){
alert(clickmessage);
return false;
}
}
}else if (document.layers) {
if (e.which == 3) {
alert(clickmessage);return false;
}
}else if (document.getElementById){
if (e.which==3&&e.target.tagName=="IMG"){
alert(clickmessage);
return false;
}
}
}
function associateimages(){
for(i=0;i<document.images.length;i++){
 document.images[i].onmousedown=disableclick;
}
}
if (document.all){
document.onmousedown=disableclick;
}else if (document.getElementById){
document.onmouseup=disableclick;
}else if (document.layers){
associateimages();
}
</script></document.images.length;i++){

Tuesday 16 July 2013

Print pdf document in C#

This technical tip shows how to print a PDF document in program. Spire.Pdf allows you to print PDF document for your requirement at runtime with.NET. By using  Spire.PDF for .NET ,which is suitable for Console platform, WinForm ,and Asp.net ,you can easily load Pdf into these programs and print out this PDF document with the help of printDialog object. The printing mode of this component support background printing mode without intervention and interaction. According to Spire.pdf, you can freely switch printer, select print page, and set relative properties while printing PDF files in C#

In this blog , I will take console platform as an example to share my tips about how to print a PDF document in C#. Here we go :

Procedures:

 Step1: For printing a PDF document, firstly I created a new project in VS .Then set its target framework to be .NET framework 2.0.

 Step2: I instantiated an Spire.Pdf. PdfDocument object and load PDF document by calling its method LoadFromFile:

            // Instantiate an object of PdfDocument
                PdfDocument doc = new PdfDocument();  
           
                string pdfFile = “sample.pdf”;

              //load PDF document
                doc.LoadFromFile(pdfFile);              

                //Default printing mode without interaction (default printer to print all pages of the document)

Step3:  After what I mentioned above, it’s time for calling the method of PdfDocumen.PrintDocument.Print(). By using default printer, printing this PDF document of all pages can be realized without interaction. Besides printing PDF , i am happy to tell you that this printing mode is also suitable for Web printing in C# :

                  C#

                 doc.PrintDocument.Print();

                //Interaction printing mode(page range,printer,and properties of printing can be freely to selected and set)



Step4 :  The final step to finish printing PDF was a little complicated in C#, because i need instantiate an object of printDialog and set its relative printing properties before calling its method.   Besides ,  at the same time,set Spire.Pdf.pdfDocument relative printing properties as well. Here,the key point to print PDF ,which is what I extremely want to emphasize, is the necessity of combination between printDialog object and PdfDocument.PrintDocument while in this process. Without printDialog object, to print PDF is in vain . At last i just can call method PdfDocument.PrintDocument.print to start print PDF document:

 
                PrintDialog dialogPrint = new PrintDialog();

                dialogPrint.AllowPrintToFile = true;

                dialogPrint.AllowSomePages = true;

                dialogPrint.PrinterSettings.MinimumPage = 1;

                dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;

                dialogPrint.PrinterSettings.FromPage = 1;

                dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;



The Entire Code Snippet:

Please take a look over the following code snippet for this approach:

C#:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing.Printing;

using Spire.Pdf;

namespace PdfPrint

{

class Program

{

[STAThread()]

static void Main(string[] args)

{

// Instantiate an object of PdfDocument

PdfDocument doc = new PdfDocument();

string pdfFile = “sample.pdf”;

//load PDF document

doc.LoadFromFile(pdfFile);

//Default printing mode without interaction (default printer to print all pages of the document)

doc.PrintDocument.Print();

//Interaction printing mode(page range,printer,and properties of printing can be freely to selected and set)

PrintDialog dialogPrint = new PrintDialog();

dialogPrint.AllowPrintToFile = true;

dialogPrint.AllowSomePages = true;

dialogPrint.PrinterSettings.MinimumPage = 1;

dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;

dialogPrint.PrinterSettings.FromPage = 1;

dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;

if (dialogPrint.ShowDialog() == DialogResult.OK)

{

//the startpage was selected to be printed

doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;

//the endpage was selected to be printed

doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;

//Choose printer to print pdf document

doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;

PrintDocument printDoc = doc.PrintDocument;

dialogPrint.Document = printDoc;

//Realize printing

printDoc.Print();

}
}
}
}