1

I have a scenario in which user is giving input file name and on back end I am searching that file if that is present on server or not , If the file is present i am loading it on browser else giving error , The problem is that the end user can download that PDF file as well as print that file , I want to stop that both scenarios.

I tried couple of online tutorials and libraries like spire.pdf and I also tried that scenario by converting my PDF in to images and than showing images to the client but that is not the good solution .

   Public Action File(string filename)
   {
      if(user is authorize)
      { 
          Search file on server
          if(File Is present) 
           { 
            return file. // user can see , download and print right now , i want to prevent downloading and printing scenarios            
           }

       } 
   }

I just want to stop user to download and print , how can i get the desire result ?

Reply not
  • 311
  • 1
  • 3
  • 12

2 Answers2

3

You cannot full prevent the pdf download (right click - download), you have a lot of choices...

1) You can open the document in borwser as default option, then the document will be displayed in browser page and not downloaded as first option. Return your document inline, the document will be opened in borwser and not downloaded

Public Action File(string filename)
{
  if(user is authorize)
  { 
      Search file on server
      if(File Is present) 
      { 
        var file = "filename.pdf";

        // Response...
        System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
        {
          FileName = file,
          Inline = true 
              // false = prompt the user for downloading;  
              // true = browser to try to show the file inline
        };
        Response.Headers.Add("Content-Disposition", cd.ToString());
        Response.Headers.Add("X-Content-Type-Options", "nosniff");

        return File(System.IO.File.ReadAllBytes(file), "application/pdf");         
      }

   } 
}

2) when you create your PDF file you can set permission FLAGS, remove print permission

3) Watermark, when your user download a PDF personalize the pdf before download writing in each page the Name, Surname, Email address of the user. this is an excellent deterrent. here an example: https://code.msdn.microsoft.com/windowsdesktop/Add-a-text-watermark-on-e56799cf

     PdfDocument doc = new PdfDocument(); 
      doc.LoadFromFile(@"..\..\Sample1.pdf");
     foreach (PdfPageBase page in doc.Pages) 
                { 
                    PdfTilingBrush brush 
                       = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3)); 
                    brush.Graphics.SetTransparency(0.3f); 
                    brush.Graphics.Save(); 
                    brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2); 
                    brush.Graphics.RotateTransform(-45); 
                    brush.Graphics.DrawString("MSDN Samples", 
                        new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, 0, 0, 
                        new PdfStringFormat(PdfTextAlignment.Center)); 
                    brush.Graphics.Restore(); 
                    brush.Graphics.SetTransparency(1); 
                    page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize)); 
                }
     doc.SaveToFile("TextWaterMark.pdf"); 
danilonet
  • 1,757
  • 16
  • 33
  • The document is currently opening on browser , and have that options of printing and downloading on top of it , my question is that how can i send a request to server to water mark the pages ? when user click the download option(that is present on browser by default) – Reply not Aug 20 '19 at 07:29
  • write the code like the example supplied in your method and instead of SaveToFile, return the stream in the response. – danilonet Aug 20 '19 at 07:38
0

Yes you can but you will need to:

  • Create Images for each page
  • Present those to the user on the web via your own interface (html, flash etc)

A print screen will allow someone to recreate the low res image you present, and in this case, you could add a watermark to the image.

  • The same I've tried , that i need to make image of every frame and than show them to the user using my own template ? – Reply not Aug 20 '19 at 07:13