public ActionResult Index(WordTemplateModel model) { //GLAAS.PdfPoc.POCUtil try { HttpPostedFileBase file = model.File; // Save File to Server. file.SaveAs(Server.MapPath(_uploadPath + file.FileName)); // Give user the mapping model = GenerateDataMapping(Server.MapPath(string.Format(@"{0}\{1}", _uploadPath, file.FileName)), model); //wordDoc(Server.MapPath(string.Format(@"{0}\{1}", _uploadPath, file.FileName)), Server.MapPath(string.Format(@"{0}\{1}", _uploadPath, _templateToDoc))); model.FileName = file.FileName; } catch (Exception ex) { } return(View(model)); }
private WordTemplateModel GenerateDataMapping(string TemplateFileLocation, WordTemplateModel model) { try { //OBJECT OF MISSING "NULL VALUE" Object oMissing = System.Reflection.Missing.Value; Object oTemplatePath = TemplateFileLocation; Application wordApp = new Application(); Document wordDoc = new Document(); wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing); //Dictionary<string, string> DataMapping = new Dictionary<string, string>(); List <ModelField> dataMapping = new List <ModelField>(); foreach (Microsoft.Office.Interop.Word.ContentControl cc in wordDoc.ContentControls) { //DataMapping[cc.Title] = ""; dataMapping.Add(new ModelField() { Key = cc.Title }); } model.DataMapping = dataMapping; object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; ((_Document)wordDoc).Close(ref doNotSaveChanges, ref oMissing, ref oMissing); wordDoc = null; ((_Application)wordApp).Quit(ref doNotSaveChanges, ref oMissing, ref oMissing); wordApp = null; } catch (Exception ex) { Console.WriteLine(ex.Message); } return(model); }
public ActionResult Generate(WordTemplateModel model) { try { string tempfilelocation = Server.MapPath(string.Format(@"{0}\{1}", _uploadPath, model.FileName)); string destfilelocation = Server.MapPath(string.Format(@"{0}\{1}", _uploadPath, _templateToDoc)); wordDoc(tempfilelocation, destfilelocation, model.DataMapping); convertToPdf(destfilelocation); //Dictionary<int, string> DataDictionaryReplacementValues = new Dictionary<int, string>(); ////YourName //DataDictionaryReplacementValues[0] = "Mohammad Murtaza Zaidi"; //// bool Single //DataDictionaryReplacementValues[0] = "true"; //var pdfReader = new PdfReader(Server.MapPath(_uploadPath + model.FileName)); //var output = new MemoryStream(); //var stamper = new PdfStamper(pdfReader, output); //foreach (var field in model.Fields) //{ // stamper.AcroFields.SetField(field.Key, field.Value); //} //stamper.FormFlattening = true; //stamper.Close(); //pdfReader.Close(); var output = new MemoryStream(); var path = destfilelocation.Replace(".docx", ".pdf"); using (var fsSource = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { byte[] bytes = new byte[fsSource.Length]; int numBytesToRead = (int)fsSource.Length; int numBytesRead = 0; while (numBytesToRead > 0) { // Read may return anything from 0 to numBytesToRead. int n = fsSource.Read(bytes, numBytesRead, numBytesToRead); // Break when the end of the file is reached. if (n == 0) { break; } numBytesRead += n; numBytesToRead -= n; } numBytesToRead = bytes.Length; output.Write(bytes, 0, numBytesToRead); } Response.AddHeader("Content-Disposition", "attachment; filename=GeneratedFrom-" + (model.FileName.ToLower().EndsWith(".pdf") ? model.FileName : model.FileName + ".pdf")); Response.ContentType = "application/pdf"; Response.BinaryWrite(output.ToArray()); Response.End(); } catch (Exception ex) { } return(RedirectToAction("Index")); }
private WordTemplateModel GenerateDataMappingOxml(string TemplateFileLocation, WordTemplateModel model) { try { using (WordprocessingDocument document = WordprocessingDocument.Open(TemplateFileLocation, true)) { // Change the document type to Document document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document); // Get the MainPart of the document MainDocumentPart mainPart = document.MainDocumentPart; // Get the Document Settings Part DocumentSettingsPart documentSettingPart1 = mainPart.DocumentSettingsPart; OpenXmlElement[] Enumerate = mainPart.ContentControls().ToArray(); List <ModelField> dataMapping = new List <ModelField>(); for (int i = 0; i < Enumerate.Count(); i++) { OpenXmlElement cc = Enumerate[i]; SdtProperties props = cc.Elements <SdtProperties>().FirstOrDefault(); Tag tag = props.Elements <Tag>().FirstOrDefault(); SdtAlias alias = props.Elements <SdtAlias>().FirstOrDefault(); string title = ((DocumentFormat.OpenXml.Wordprocessing.StringType)(alias)).Val; string tagName = tag.Val; dataMapping.Add(new ModelField() { Key = title }); } model.DataMapping = dataMapping; } } catch (Exception ex) { throw; } return(model); }
public ActionResult Generate(WordTemplateModel model) { try { string tempfilelocation = Server.MapPath(string.Format(@"{0}\{1}", _uploadPath, model.FileName)); string destfilelocation = Server.MapPath(string.Format(@"{0}\{1}", _uploadPath, _templateToDoc)); string fileIN = Server.MapPath(string.Format(@"{0}\{1}", _uploadPath, "test123.docx")); string fileOUT = fileIN.Replace(".docx", ".pdf"); var path = model.DocumentType == 0 ? destfilelocation.Replace(".docx", ".pdf") : destfilelocation; wordDocOpenXml(tempfilelocation, destfilelocation, model.DataMapping); if (model.DocumentType == 0) { AsposeToPdf(destfilelocation, path); } //ConvertToPdf(fileIN, fileOUT); //wordDoc(tempfilelocation, destfilelocation, model.DataMapping); //convertToPdf(destfilelocation); var output = new MemoryStream(); using (var fsSource = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { byte[] bytes = new byte[fsSource.Length]; int numBytesToRead = (int)fsSource.Length; int numBytesRead = 0; while (numBytesToRead > 0) { // Read may return anything from 0 to numBytesToRead. int n = fsSource.Read(bytes, numBytesRead, numBytesToRead); // Break when the end of the file is reached. if (n == 0) { break; } numBytesRead += n; numBytesToRead -= n; } numBytesToRead = bytes.Length; output.Write(bytes, 0, numBytesToRead); } Response.AddHeader("Content-Disposition", string.Format("attachment; filename=GeneratedFrom-{0}", model.DocumentType == 0 ? "GeneratedDocument.pdf" : "GeneratedDocument.docx")); Response.ContentType = "application/pdf"; Response.BinaryWrite(output.ToArray()); Response.End(); return(View("Index", model)); } catch (Exception ex) { // return new ContentResult() { Content = ex.Message }; model.Error = ex.ToString(); return(View("Index", model)); } //return RedirectToAction("Index"); }