/// <summary> /// Saves the content of the fileUpload control to the database and file system /// as the given type. The file object is returned on success, else null. /// </summary> public static File SavePDF(FileUpload fileUpload, FileType fileType) { if (fileUpload.HasFile) { if (IsPostedFilePDF(fileUpload.PostedFile)) { File file = new File(); file.FileName = fileUpload.FileName; file.FileType = fileType; using (var ts = new TransactionScope()) { bool saveSuccess = FileBLL.MakePersistent(file); // Save is a success if the persistence worked and all validation rules are satisfied if (saveSuccess) { fileUpload.SaveAs(FilePath + file.ID.ToString()); ts.CommitTransaction(); //On success we commit return(file); } } } } return(null); }
public static File SavePDFWithWatermark(FileUpload fileUpload, FileType fileType) { File file = new File(); if (fileUpload.HasFile) { if (IsPostedFilePDF(fileUpload.PostedFile)) { file.FileName = fileUpload.FileName; file.FileType = fileType; bool saveSuccess = FileBLL.MakePersistent(file); // Save is a success if the persistence worked and all validation rules are satisfied if (!saveSuccess) { return(null); } } else { return(null); } } PdfReader reader = new PdfReader(fileUpload.FileContent); int n = reader.NumberOfPages; Document document = new Document(reader.GetPageSizeWithRotation(1)); PdfWriter writer = PdfWriter.GetInstance(document, new System.IO.FileStream(FilePath + file.ID.ToString(), System.IO.FileMode.Create)); document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage page; int rotation; for (int i = 1; i <= n; i++) { document.SetPageSize(reader.GetPageSizeWithRotation(i)); document.NewPage(); page = writer.GetImportedPage(reader, i); rotation = reader.GetPageRotation(i); if (rotation == 90 || rotation == 270) { cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); } else { cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); } BaseFont bf = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.BeginText(); cb.SetFontAndSize(bf, 26f); cb.SetColorFill(Color.RED); cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "CONFIDENTIAL", reader.GetPageSizeWithRotation(i).Width / 2f, reader.GetPageSizeWithRotation(i).Height - 26f, 0); //cb.ShowText(currentApplication.Files[f].FileType.FileTypeName); cb.EndText(); } document.Close(); return(file); }