Download a PDF file stored in a network location

In View page (.cshtml) Create a Link using anchor tag and define href field as:

href = ‘@Url.Action(“Download”, “YourControllerName”,new{filePath = “”})’

public FileResult Download(string filePath)
{
MemoryStream ms = new MemoryStream();

byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
string fileNAme = Path.GetFileName(filePath);

string tmpPath = Path.GetTempPath();
tmpPath = tmpPath + fileNAme;

PdfReader pdfReader = new PdfReader(fileBytes);

PdfReader.unethicalreading = true;
FileStream sourceFileStream = new FileStream(tmpPath, FileMode.Create, FileAccess.Write);
PdfStamper pdfStream = new PdfStamper(pdfReader, sourceFileStream);
string waterMark= "Confidential"
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(page);
PdfContentByte pdfData = pdfStream.GetUnderContent(page);
pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0.6F;
pdfData.SetGState(graphicsState);
pdfData.SetColorFill(BaseColor.GRAY);
pdfData.BeginText();
pdfData.ShowTextAligned(Element.ALIGN_CENTER, waterMark, pageRectangle.Width / 2, pageRectangle.Height / 2, 45);
pdfData.EndText();
}
pdfStream.FormFlattening = true;
pdfStream.Close();
pdfStream.Dispose();
sourceFileStream.Close();
sourceFileStream.Dispose();

byte[] byteArray = System.IO.File.ReadAllBytes(tmpPath);
System.IO.File.Delete(tmpPath);
ms.Write(byteArray, 0, byteArray.Length);
ms.Position = 0;

return new FileStreamResult(ms, "application/pdf");
}

To use above code you need iTextSharp dll. you can install it using Nuget as: Install-Package iTextSharp

References :
<ul>
	<li><a https://developers.itextpdf.com/itextsharp-net</a></li>
</ul>

2 thoughts on “Download a PDF file stored in a network location

  1. Out of curiosity, I see you’re using iTextSharp …?

    I am using iTextSharp myself, but it’s Affero GPL, and I’d love to have something at least slightly more permissive, if possible …
    Have you done some research into the alternatives here …?

    BTW, is it enough to provide a link in the actual PDF, or do you need to create a link also at the website that is generating the code …?

    If the latter, that would be better I think …

    Suggestions …?

    Like

    • As an alternative we can use PDFSharp. it is also published OPen Source and under the MIT License.
      You are right, Here also the link to download is created in website which on click download the pdf.

      Note – we can provide the browse button to give the pdf file path however here I am getting link from DB in backend and passing as a parameter to download function.

      I hope I answered your question to the best of my knowledge. if you have some more doubts/suggestions plz feel free to ask/suggest.
      Thank You.

      Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s