Tags » Mvc

Download a PDF file stored in a network location

Create a Link: using anchor tag href = “‘@Url.Action(“Download”, “YourControllerName”)'”;

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. 60 more words
MVC

Men's Soccer to Play Bradley

The men’s soccer team will travel to Peoria to take on Bradley University. The Panthers have a record of (2-5-5, 0-2-1 SL). The Braves have a record of (7-4-1, 2-1-1 MVC).  16 more words

EIU

My Reading Habits

Those who have known me for a long time know that I love to read. When I was in middle school and high school, I zipped through novels almost every week. 159 more words

MVC

Enterprise level Application architecture using MVC5 - Best practices

  1. First of all, it is important to note that in software design process, there is nothing like solid right and wrong. As long as an approach works for your project and fits well, it is…
  2. 840 more words
DotNET

Handy note on Configuration and InitializationStrategy in .Net EntityFramework

The default Entity framework conventions can be overridden through two methods
1.Data Annotations
2.Fluent API.
Entities and properties are configured through EntityTypeConfiguration. The ModelBuilder class provides Enity (),which return EntityTypeConfiguration object which is  used in configuration.  180 more words

MVC

In Built Delegates in C#

Func<T,TResult>, Action and predicate delegates are inbuilts present in the system namespace.

Func delegate can take one or more parameters and returns a value. The parameters can be zero to 16 and the last parameter represents a return value. 90 more words

MVC

One stop for all basic Entity framework Migration commands

This post thows up a glimpse on the basic commads syntax and usage serving as a quick reference when ever you are struck out for pretty basic thing. 185 more words

MVC