【发布时间】:2017-04-25 13:01:12
【问题描述】:
如何使用 mvc c# 将 jpg/png/txt 或任何文件格式转换为 pdf。 代码如下:
public ActionResult SaveProfileDocument(string code)
{
bool isSavedSuccessfully = true;
string fName = "";
string _documentname = String.Empty;
try
{
foreach (string fileName in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileName];
//Save file content goes here
fName = file.FileName;
if (file != null && file.ContentLength > 0)
{
var originalDirectory = new DirectoryInfo(string.Format("{0}Documents\\Profile\\" + code, Server.MapPath(@"\")));
string pathString = System.IO.Path.Combine(originalDirectory.ToString());
var fileName1 = Path.GetFileName(file.FileName);
bool isExists = System.IO.Directory.Exists(pathString);
if (!isExists)
System.IO.Directory.CreateDirectory(pathString);
_documentname=fName;
var path = string.Format("{0}\\{1}", pathString, file.FileName);
if (System.IO.File.Exists(path)) {
_documentname=Guid.NewGuid()+"_"+file.FileName;
var path2 = string.Format("{0}\\{1}", pathString,_documentname );
file.SaveAs(path2);
}
else {
file.SaveAs(path);
}
}
}
}
catch (Exception ex)
{
isSavedSuccessfully = false;
}
if (isSavedSuccessfully)
{
return Json(new { Message = fName, documentname = _documentname });
}
else
{
return Json(new { Message = "Error in saving file", documentname=""});
}
}
在上面的代码中,我正在保存文件。但是 这里我需要转换文件然后保存。
所以对于转换我需要一个单独的类或方法在这里只调用那个方法。
问题是在那个时候上传文件需要转换pdf任何文件来转换pdf。并保存在文件夹或其他任何地方。
【问题讨论】:
-
我不知道你的问题是什么。你特别困在什么地方?
-
我需要使用c#将jpg格式文件转换为pdf
-
您不会将 JPG 文件转换为 PDF。 JPG 是一种图像格式。 PDF 是一种文档格式。您可以创建 PDF,然后将 JPG 插入其中。您可能需要一个能够生成 PDF 的库。我建议您研究并找到一个适用于 .NET 的工具并阅读其文档。
-
我需要将任何格式转换为 pdf,它可能是 jpg 或 png 或 txt 或 xlx 或 jpeg 等等
-
为什么我不能转换??我的要求是如果假设选择 pdf 那么如果有人选择任何其他格式然后将其转换为 pdf 就可以了
标签: c# asp.net asp.net-mvc class asp.net-mvc-5