我有一个场景,其中用户提供输入文件名,并且在后端我正在搜索该文件是否存在于服务器上,如果该文件存在我将其加载到浏览器中,否则会出现错误,问题是最终用户可以下载该 PDF 文件以及打印该文件,我想停止这两种情况。
我尝试了几个在线教程和库,如 spire.pdf,我还尝试了将我的 PDF 转换为图像而不是向客户端显示图像的方案,但这不是好的解决方案。
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
}
}
}
我只是想阻止用户下载和打印,我怎样才能得到想要的结果?
最佳答案
您不能完全阻止 pdf 下载(右键单击 - 下载), 你有很多选择...
1) 您可以默认在浏览器中打开文档,然后文档将显示在浏览器页面中,而不是作为第一个选项下载。内联返回您的文档,文档将在浏览器中打开而不是下载
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) 当您创建 PDF 文件时,您可以设置权限 FLAGS,
3) 水印,当您的用户下载 PDF 时,在下载之前个性化 pdf,在每个页面上写下用户的姓名、姓氏、电子邮件地址。 这是极好的威慑力。 这里有一个例子: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");
关于c# - 我可以阻止客户端下载在浏览器上可见的 pdf 文件吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57568346/