Hello
i tried to convert byte (which is store in table )into pdf file(download pdf file)
here is my code in mvc
pdf is genrated but not loaded
what is wrong in code
public FileResult DownloadPDF(string Id)
{
AttachFileSC mAttachFileSC = null;
DataSet mDataSet = null;
MemoryStream pdfStream = new MemoryStream();
string FileName = string.Empty;
try
{
mAttachFileSC = new AttachFileSC();
FileManagerBLL mFileManagerBLL = new FMS.Models.BLL.FileManagerBLL();
mDataSet = mFileManagerBLL.DownloadPDF(Id, UserObj.UserId);
foreach (DataRow mDataRow in mDataSet.Tables[0].Rows)
{
mAttachFileSC.FileName = mDataRow["FileName"].ToString();
mAttachFileSC.FileSize = mDataRow["FileSize"].ToString();
mAttachFileSC.FileExt = mDataRow["FileExt"].ToString();
mAttachFileSC.FileByte = (byte[])mDataSet.Tables[0].Rows[0]["FileByte"];
}
FileName = (mAttachFileSC.FileName).Replace(mAttachFileSC.FileExt, ".pdf");
pdfStream.Write(mAttachFileSC.FileByte, 0, mAttachFileSC.FileByte.Length);
pdfStream.Position = 0;
}
catch (Exception ex)
{
throw ex;
}
return File(pdfStream, System.Net.Mime.MediaTypeNames.Application.Octet, FileName);
}
{
AttachFileSC mAttachFileSC = null;
DataSet mDataSet = null;
MemoryStream pdfStream = new MemoryStream();
string FileName = string.Empty;
try
{
mAttachFileSC = new AttachFileSC();
FileManagerBLL mFileManagerBLL = new FMS.Models.BLL.FileManagerBLL();
mDataSet = mFileManagerBLL.DownloadPDF(Id, UserObj.UserId);
foreach (DataRow mDataRow in mDataSet.Tables[0].Rows)
{
mAttachFileSC.FileName = mDataRow["FileName"].ToString();
mAttachFileSC.FileSize = mDataRow["FileSize"].ToString();
mAttachFileSC.FileExt = mDataRow["FileExt"].ToString();
mAttachFileSC.FileByte = (byte[])mDataSet.Tables[0].Rows[0]["FileByte"];
}
FileName = (mAttachFileSC.FileName).Replace(mAttachFileSC.FileExt, ".pdf");
pdfStream.Write(mAttachFileSC.FileByte, 0, mAttachFileSC.FileByte.Length);
pdfStream.Position = 0;
}
catch (Exception ex)
{
throw ex;
}
return File(pdfStream, System.Net.Mime.MediaTypeNames.Application.Octet, FileName);
}

Salman BegRank #284 · 8.8K reputationPosted Oct 31, 2019, 6:16 PM EDT
Rahul PrasadRank #2,557 · 546 reputationPosted Oct 31, 2019, 4:27 PM EDT
Jaimin ShethiyaRank #177 · 13.7K reputationPosted Oct 31, 2019, 8:00 PM EDT
Can you please check with the below code.
Thanks
Rupesh KahaneRank #136 · 16.4K reputationPosted Oct 31, 2019, 8:09 PM EDT
Mageshwaran RRank #411 · 6.1K reputationPosted Nov 7, 2019, 3:47 AM EST
Marvin ReidRank #1,963 · 746 reputationPosted Mar 21, 2023, 1:08 PM EDT
You can take your byte data and convert it into a PDF using the DocumentConverter class. Here is an example you can build on:
string b64Str = "Your base64 string add here"; byte[] bytes = Convert.FromBase64String(b64Str); using (MemoryStream ms = new MemoryStream(bytes)) { var LDfile = DocumentFactory.LoadFromStream(ms, new LoadDocumentOptions()); var documentConverter = new DocumentConverter(); var jobData = new DocumentConverterJobData { DocumentFormat = DocumentFormat.Pdf, Document = LDfile, OutputDocumentStream = ms }; var job = documentConverter.Jobs.CreateJob(jobData); documentConverter.Jobs.RunJob(job); if (job.Status == DocumentConverterJobStatus.Success) { } File.WriteAllBytes("test.pdf", ms.ToArray()); }https://www.leadtools.com/help/sdk/tutorials/dotnet-convert-files-with-the-document-converter.html