Skip to content

Instantly share code, notes, and snippets.

@flaviotsf
Created May 24, 2012 17:40
Show Gist options
  • Select an option

  • Save flaviotsf/2782997 to your computer and use it in GitHub Desktop.

Select an option

Save flaviotsf/2782997 to your computer and use it in GitHub Desktop.
iTextSharp PDF Downloads
private void SendFile(HttpContext context, ProductDownload productDownload)
{
try {
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ContentType = ContentType(productDownload.FileInfo.Extension);
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + productDownload.FileName.Replace(" ", "_"));
if ((productDownload.FileInfo.Extension.ToLower() != ".pdf")) {
context.Response.TransmitFile(productDownload.FilePath);
context.Response.Flush();
SaveDownloadLog(productDownload, DownloadStartedAt);
return;
}
var backgroundColor = new BaseColor(0, 0, 0);
var stampEnabled = Convert.ToBoolean(ConfigurationManager.AppSettings("UseStamping"));
if ((stampEnabled)) {
try {
var reader = new PdfReader(productDownload.FilePath);
if ((reader.IsOpenedWithFullPermissions)) {
using (MemoryStream memoryStream = new MemoryStream()) {
var pdfStamper = new PdfStamper(reader, memoryStream);
var firstPage = (reader.NumberOfPages > 2 ? 2 : 1);
for (var i = firstPage; i <= reader.NumberOfPages; i++) {
var mediaBox = reader.GetPageSizeWithRotation(i);
var cropBox = reader.GetCropBox(i);
var dif = Math.Abs(cropBox.Height - mediaBox.Height);
var pdfPageContents = pdfStamper.GetOverContent(i);
pdfPageContents.BeginText();
// Start working with text.
var myFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName, false);
pdfPageContents.SetColorFill(backgroundColor);
pdfPageContents.SetFontAndSize(myFont, 6);
pdfPageContents.SetRGBColorFill(204, 0, 0);
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("Copyright American Society for Training & Development, {0}. No unauthorized use, distribution, or reproduction is permitted.", DateTime.Now.Year.ToString()), 50, mediaBox.GetBottom(20 + dif / 2), 0);
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("Permissions authorized through Copyright Clearing Center (www.astd.org/licensing). Order Number: {0} Order Date: {1}", productDownload.OrderNumber, productDownload.OrderDate.ToString("MM/dd/yyyy")), 50, mediaBox.GetBottom(10 + dif / 2), 0);
pdfPageContents.EndText();
// Done working with text
pdfStamper.FormFlattening = true;
}
pdfStamper.Close();
reader.Close();
dynamic pdf = memoryStream.ToArray();
context.Response.BinaryWrite(pdf);
}
SaveDownloadLog(productDownload, DownloadStartedAt);
return;
}
} catch (BadPasswordException ex) {
} catch (BadPdfFormatException ex) {
}
}
//Some error with the PDF file made it fall here.
context.Response.TransmitFile(productDownload.FilePath);
context.Response.Flush();
SaveDownloadLog(productDownload, DownloadStartedAt);
return;
} catch (HttpException ex) {
throw new UserCanceledDownloadException();
}
}
@flaviotsf

Copy link
Copy Markdown
Author

I'm starting the stamp on page 2, so it doesn't affect the cover of the document.
If it's a one page document, it will be added there.

@flaviotsf

Copy link
Copy Markdown
Author

made some changes to ensure the PDF can be opened for edit, if not - we send the original file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment