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>

3 comments

  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

    1. 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

      1. Interesting point you are proposing (indirectly), which is that once the PDF file has been generated, the client code no longer needs to link to the iTextSharp library itself, at which point you have not created a “derivative work”, and the GPL doesn’t kick in, at which point the Affero restrictions no longer applies, assuming the code generating the PDF and the code allowing the user to download the PDF is not “linked together” in any ways …

        Which just so happens to create some very interesting scenarios for me personally … 😉

        Have you got any experience with PDF sharp …?

        Is it any good?
        Is the API easily understood?
        Features of PDF sharp versus iTextSharp?
        Does it handle HTML, and can it handle CSS to style the content?

        Thx mate 🙂

        Like

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.