public HttpResponseMessage GetFileAttachment(string folder, string attachmentFileName) { var fullPath = Path.Combine(Config.Configuration.WorkingDirectory.Replace("/", "\\"), "Editor", folder); var pdfDocumentPath = string.Format("{0}\\document.pdf", fullPath); attachmentFileName = ReplaceLastUnderscore(attachmentFileName); Document pdfDocument = new Document(pdfDocumentPath); EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles; var index = 0; for (int i = 1; i <= pdfDocument.EmbeddedFiles.Count; i++) { if (pdfDocument.EmbeddedFiles[i].Name == attachmentFileName) { index = i; } } var attachment = embeddedFiles[index]; HttpResponseMessage httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK); httpResponseMessage.Content = new StreamContent(attachment.Contents); httpResponseMessage.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); httpResponseMessage.Content.Headers.ContentDisposition.FileName = attachment.Name; httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); return(httpResponseMessage); }
public IHttpActionResult RemoveFileAttachment(RemoveAttachmentModel removeAttachmentModel) { var httpRequest = HttpContext.Current.Request; var fullPath = Path.Combine(Config.Configuration.WorkingDirectory.Replace("/", "\\"), "Editor", removeAttachmentModel.documentId); var pdfDocumentPath = string.Format("{0}\\document.pdf", fullPath); // Open document try { using (Document pdfDocument = new Document(pdfDocumentPath)) { EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles; pdfDocument.EmbeddedFiles.Delete(removeAttachmentModel.attachmentFileName); } var model = new FileAttachmentsModel { d = "Success", Path = HttpContext.Current.Request.Form["documentId"] }; return(Ok(model)); } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult GetFileAttachments(string folder) { var fullPath = Path.Combine(Config.Configuration.WorkingDirectory.Replace("/", "\\"), "Editor", folder); var pdfDocumentPath = string.Format("{0}\\document.pdf", fullPath); string outAttach = ""; // Open document using (Document pdfDocument = new Document(pdfDocumentPath)) { // Get embedded files collection EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles; // Loop through the collection to get all the attachments foreach (FileSpecification fileSpecification in embeddedFiles) { outAttach = outAttach + "," + fileSpecification.Name + "," + fileSpecification.Description; } if (outAttach.Length > 1) { outAttach = outAttach.Substring(1); } } var model = new FileAttachmentsModel { d = outAttach }; return(Ok(model)); }
public static string GetFileAttachments() { string outAttach = ""; // Open document using (Document pdfDocument = new Document(HttpContext.Current.Server.MapPath("Convert/output.pdf"))) { // Get embedded files collection EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles; // Loop through the collection to get all the attachments foreach (FileSpecification fileSpecification in embeddedFiles) { string[] filename = fileSpecification.Name.Split('\\'); outAttach = outAttach + "," + filename[filename.Length - 1] + "," + fileSpecification.Description; // Get the attachment and write to file or stream byte[] fileContent = new byte[fileSpecification.Contents.Length]; fileSpecification.Contents.Read(fileContent, 0, fileContent.Length); FileStream fileStream = new FileStream(HttpContext.Current.Server.MapPath("Attachments/" + filename[filename.Length - 1]), FileMode.Create); fileStream.Write(fileContent, 0, fileContent.Length); fileStream.Close(); } if (outAttach.Length > 1) { outAttach = outAttach.Substring(1); } } return(outAttach); }
public static void Run() { // ExStart:ExtractFilesFromPortfolio // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_TechnicalArticles(); // Load source PDF Portfolio Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(dataDir + "PDFPortfolio.pdf"); // Get collection of embedded files EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles; // Itterate through individual file of Portfolio foreach (FileSpecification fileSpecification in embeddedFiles) { // Get the attachment and write to file or stream byte[] fileContent = new byte[fileSpecification.Contents.Length]; fileSpecification.Contents.Read(fileContent, 0, fileContent.Length); string filename = Path.GetFileName(fileSpecification.Name); // Save the extracted file to some location FileStream fileStream = new FileStream(dataDir + "_out" + filename, FileMode.Create); fileStream.Write(fileContent, 0, fileContent.Length); // Close the stream object fileStream.Close(); } // ExEnd:ExtractFilesFromPortfolio }
public static void Run() { // ExStart:GetAlltheAttachments // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments(); // Open document Document pdfDocument = new Document(dataDir + "GetAlltheAttachments.pdf"); // Get embedded files collection EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles; // Get count of the embedded files Console.WriteLine("Total files : {0}", embeddedFiles.Count); int count = 1; // Loop through the collection to get all the attachments foreach (FileSpecification fileSpecification in embeddedFiles) { Console.WriteLine("Name: {0}", fileSpecification.Name); Console.WriteLine("Description: {0}", fileSpecification.Description); Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType); // Check if parameter object contains the parameters if (fileSpecification.Params != null) { Console.WriteLine("CheckSum: {0}", fileSpecification.Params.CheckSum); Console.WriteLine("Creation Date: {0}", fileSpecification.Params.CreationDate); Console.WriteLine("Modification Date: {0}", fileSpecification.Params.ModDate); Console.WriteLine("Size: {0}", fileSpecification.Params.Size); } // Get the attachment and write to file or stream byte[] fileContent = new byte[fileSpecification.Contents.Length]; fileSpecification.Contents.Read(fileContent, 0, fileContent.Length); FileStream fileStream = new FileStream(dataDir + count + "_out_" + ".txt", FileMode.Create); fileStream.Write(fileContent, 0, fileContent.Length); fileStream.Close(); count += 1; } // ExEnd:GetAlltheAttachments }
public static void Main(string[] args) { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); //open document Document pdfDocument = new Document(dataDir + "input.pdf"); //get embedded files collection EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles; //get count of the embedded files Console.WriteLine("Total files : {0}", embeddedFiles.Count); int count = 1; //loop through the collection to get all the attachments foreach (FileSpecification fileSpecification in embeddedFiles) { Console.WriteLine("Name: {0}", fileSpecification.Name); Console.WriteLine("Description: {0}", fileSpecification.Description); Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType); //check if parameter object contains the parameters if (fileSpecification.Params != null) { Console.WriteLine("CheckSum: {0}", fileSpecification.Params.CheckSum); Console.WriteLine("Creation Date: {0}", fileSpecification.Params.CreationDate); Console.WriteLine("Modification Date: {0}", fileSpecification.Params.ModDate); Console.WriteLine("Size: {0}", fileSpecification.Params.Size); } //get the attachment and write to file or stream byte[] fileContent = new byte[fileSpecification.Contents.Length]; fileSpecification.Contents.Read(fileContent, 0, fileContent.Length); FileStream fileStream = new FileStream(dataDir + count + ".txt", FileMode.Create); fileStream.Write(fileContent, 0, fileContent.Length); fileStream.Close(); count += 1; } }