How to convert MS-WORD file into HTML in ASP.NET MVC? - asp.net-mvc

I am doing a project in which i have to to read a word document and perform some operation on file content. I have read the file successfully using
Open XML, but that's not enough. It read the file but does not retain the format and indentation. So the solution i found is that first read the file and convert it into HTML. Another thing i want all file content in "String" because i have to perform further operation on file contents.
So is there any way to read the MS-WORD file and convert into HTML and store in "string"?
Here i am showing the code of reading MS-WORD file.
[HttpPost]
public ActionResult OpenFile(HttpPostedFileBase file)
{
Rules R = new Rules();
string FileText = "";
if (file != null && file.ContentLength > 0)
{
string filename = Path.GetFileName(file.FileName);
R.name = filename;
string path = Path.Combine(Server.MapPath("~/Uploads"), filename);
file.SaveAs(path);
string filepath = path;
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filepath, true))
{
Body body = wordDoc.MainDocumentPart.Document.Body;
foreach (var paragraph in body.Elements<Paragraph>())
{
FileText += " <br> ";
}
}
R.Content += FileText;
}
return View(R);
}

Related

Wrong Mimetype for .RTF in web request

When a .rtf file is upload through an action in ASP.NET MVC, the mimetype for seems to be wrong (application/msword instead of application/rtf):
foreach (string _file in Request.Files)
{
var fileContent = Request.Files[_file];
if (fileContent != null && fileContent.ContentLength > 0)
{
var stream = fileContent.InputStream;
Attachment attachment = new Attachment
{
File = stream.ToByteArray(),
MimeType = fileContent.ContentType,
NomeFile = fileContent.FileName,
Description = description
};
}
}
fileContent is HttpPostedFileBase type.

Want exact file format on download which we had upload for signature on document sign

I am following these steps.
I am uploading the files of TEXT / PNG / DOCX / PPTX for the DocuSign using
c# API.
when I download that file after the signature of the document I got PDF format of that signature document except for uploaded file format.
Is there any alternate way of code or API how I got the same extension file on the download after signature done.
Envelope ID:- 3a6b752a-92f1-4751-b06a-ffb2c236f65d , 93409bdc-edcf-4e4b-8c6f-3c76c6305dcf , 5a32979b-3739-4585-a554-b725e1d65504
I am using below code in asp.net.
// get Lists the envelope documents.
public static EnvelopeDocumentsResult ListEnvelopeDocuments(string accountId, string envelopeId)
{
EnvelopesApi envelopesApi = new EnvelopesApi();
EnvelopeDocumentsResult docsList = envelopesApi.ListDocuments(accountId,
envelopeId);
return docsList;
}
// download file using envelopeId and that I get .pdf format
public static string DownloadSignedDocument(string envelopeId, string tempFileName, string companyCode, string apiUserName, string apiPassword, string integratorkey, string restApi)
{
ApiClient apiClient = new ApiClient(restApi);
Configuration.Default.ApiClient = apiClient;
// call the Login() API which sets the user's baseUrl and returns their accountId
string accountId = LoginApi(apiUserName, apiPassword, integratorkey);
// pass accountid and envelop id to get perticuler document for the download
EnvelopeDocumentsResult documentList = ListEnvelopeDocuments(accountId,
envelopeId);
string SignedPDFPath = string.Empty;
EnvelopesApi envelopesApi = new EnvelopesApi();
foreach (EnvelopeDocument document in documentList.EnvelopeDocuments)
{
MemoryStream docStream =
(MemoryStream)envelopesApi.GetDocument(accountId,
envelopeId, documentList.EnvelopeDocuments[0].DocumentId);
string documentName = document.Name != "Summary" ? document.Name :
"Summary";
SignedPDFPath =
systemConfiguration.ConfigurationManager.AppSettings["uploadFolder"] +
"/" + companyCode + "/" + "DocuSign/" + envelopeId;
if (!Directory.Exists(SignedPDFPath))
{
Directory.CreateDirectory(SignedPDFPath);
}
string[] tempFileExtention = tempFileName.Split('.');
string[] removeFileExtention = documentName.Split('.');
// need to give hardcoded ".pdf" extention because **document.Name**
//gives pdf file formate
SignedPDFPath = SignedPDFPath + "/" + removeFileExtention[0] + ".pdf"
;
// Server.MapPath("~/Uploadfiles/" + recipient.EnvelopeID + "/" +
// recipient.EnvelopeID + "_" + documentName + ".pdf");
using (FileStream fileStream = File.Create(SignedPDFPath))
{
docStream.Seek(0, SeekOrigin.Begin);
docStream.CopyTo(fileStream);
}
break;
}
return SignedPDFPath;
}
No.
All files are converted to PDF upon upload to DocuSign. If desired, you could use external tools to convert that PDF into another file format, but you would be breaking the 'tamper evident' digital seal on the document.

FileContentResult and international characters in the file name

I am using this code to return the file to the client
return new FileContentResult(Encoding.UTF8.GetBytes(sb.ToString()), MimeType)
{
FileDownloadName = String.Format("{0}.csv", fileName)
};
it works file when the file name is in ascii format, but it will return the name of the Action when the file name contains international chars
For example if filename is Report 新しいレポート the downloaded file is the name of the Action without any extension.
Update
This will happen in Chrome and ie, in Firefox the file is downloaded but some chars are changed
Firefox: Report2%0d%0a ��しいレポート_2016-03-09_09-20-35.c%0d%0a sv
For whom who have the same problem:
var browser = System.Web.HttpContext.Current.Request.Browser.Browser;
if (browser == "Chrome" || browser == "IE" || browser == "InternetExplorer")
{
fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);
}
return new FileContentResult(Encoding.UTF8.GetBytes(sb.ToString()), MimeType)
{
FileDownloadName = String.Format("{0}.csv", fileName)
};

Trouble exporting to excel from mvc action

I created a simple action to download some content as excel file:
public FileResult ExportToExcel()
{
string filename = "list.xlsx";
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
List<string[]> list = new List<string[]>();
list.Add(new[] { "col1", "col2", "cols3" });
list.Add(new[] { "col4", "col5", "cols6" });
list.Add(new[] { "col7", "col8", "cols9" });
StringWriter sw = new StringWriter();
sw.WriteLine("ID,Date,Description");
foreach (string[] item in list)
{
sw.WriteLine("{0},{1},{2}", item[0], item[1], item[2]);
}
byte[] fileContents = Encoding.UTF8.GetBytes(sw.ToString());
return this.File(fileContents, contentType, filename);
}
I have 2 issues with it:
1. The file is downloaded but I cannot open it and am getting a warning:
Excel cannot open the file ... because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
When I use old excel format:
string filename = "List.xls";
string contentType = "application/vnd.ms-excel";
I am able to open the file but after 3 different warnings about file being corrupted etc.
Btw I compared saving and tried to write file as pdf
string filename = "List.pdf";
string contentType = "application/pdf";
And I still couldn't open the file - it said format is not valid etc.
2. The contents appear in the file in the second example however the commas are not recognised as column separators and all data in a row is written as one column.
What separator to use for excel format or how to write data to file to have it in a table excel format?
Ideal solution for me would be just return exported view (strongly typed) but I didn't find out how to do it so far.
--- EDIT: Working solution ---
public FileResult ExportToExcel()
{
string filename = "List.xlsx";
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
List<string[]> titles = new List<string[]>() { new[] { "a", "be", "ce" } };
List<string[]> list = new List<string[]>
{
new[] { "col1", "col2", "cols3" },
new[] { "col4", "col5", "cols6" },
new[] { "col7", "col8", "cols9" },
new[] { "col10", "col11", "cols12" }
};
XLWorkbook wb = new XLWorkbook();
XLTables xt = new XLTables();
var ws = wb.Worksheets.Add("List");
ws.Cell(1, 1).InsertData(titles);
ws.Cell(2, 1).InsertData(list);
ws.Columns().AdjustToContents();
var stream = new MemoryStream();
wb.SaveAs(stream);
stream.Seek(0, SeekOrigin.Begin);
wb.Dispose();
return this.File(stream, contentType, filename);
}
The reason why it is not being correctly rendered is because you cannot just return the mime type and expect the framework to figure out the rest.
I would go with a nuget package called closedXML which will allow you to create an excel file in memory and stream it back to the client.
it comes with a full documentation (here) for more information.
Using this package you can do something like
XLWorkbook wb = new XLWorkbook();
XLTables xt = new XLTables();
var ws = wb.Worksheets.Add("Sheet 1");
var firstCell = ws.Cell(1, 1);
var lastCell = ws.Cell(3, list.Count);
var table = ws.Range(firstCell.Address, lastCell.Address).AsTable();
table.Cell(2, 1).InsertData(list);
table.CreateTable();
ws.Columns().AdjustToContents();
using(var stream = new MemoryStream())
{
wb.SaveAs(stream);
stream.Seek(0, SeekOrigin.Begin);
wb.Dispose();
return File(stream , contentType, filename);
}

Error in converting HTML with images to PDF using itextsharp

In my application first am allowing the user to create html document using CKEDITOR where user can can create html document and can insert image, form fields etc. the generated HTML document is than converted into PDF.
If HTML document contains plain text than PDF file gets created successfully but if user inserts image in it than gives error.
code for creating PDF document.
public ActionResult CreateFile(FormCollection data)
{
var filename = data["filename"];
var htmlContent = data["content"];
string sFilePath = Server.MapPath(_createdPDF + filename + ".html");
htmlContent = htmlContent.Trim();
if (!System.IO.File.Exists(sFilePath))
{
using (FileStream fs = new FileStream(sFilePath, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.Write(htmlContent);
}
}
createPDF(sFilePath);
}
return View();
}
private MemoryStream createPDF(string sFilePath)
{
string filename = Path.GetFileNameWithoutExtension(sFilePath);
string name = Server.MapPath(_createdPDF + filename + ".pdf");
MemoryStream ms = new MemoryStream();
TextReader tr = new StringReader(sFilePath);
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
string urldir = Request.Url.GetLeftPart(UriPartial.Path);
urldir = urldir.Substring(0, urldir.LastIndexOf("/") + 1);
Response.Write(urldir);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(name, FileMode.Create));
document.Open();
string htmlText = "";
StreamReader sr;
sr = System.IO.File.OpenText(sFilePath);
htmlText = sr.ReadToEnd();
sr.Close();
WebClient wc = new WebClient();
Response.Write(htmlText);
var props = new Dictionary<string, Object>();
props["img_baseurl"] = #"C:\Documents and Settings\shubham\My Documents\visdatemplatemanger\visdatemplatemanger\";
List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null,props);
for (int k = 0; k < htmlarraylist.Count; k++)
{
document.Add((IElement)htmlarraylist[k]);
}
document.Close();
System.IO.File.Delete(sFilePath);
UploadURL(name);
return ms;
}
The error that i get if image is included in HTML document is:
Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\PDFimages\rectangle-shape.png'.
iTextSharp will try to resolve relative images for HTTP-based documents but ones served from the filesystem you need to either provide absolute paths or provide a base for it to search from.
//Image search base, path will be concatenated directly so make sure it contains a trailing slash
var props = new Dictionary<string, Object>();
props["img_baseurl"] = #"c:\images\";
//Include the props from above
htmlarraylist = HTMLWorker.ParseToList(sr, null, props);

Resources