Image not getting loaded in odt file after exporting using Nreco - c#

I am using Nreco in mvc dotnet to export Html to Odt(Open office) file. But after getting exported image is not getting loaded in odt File. I have tried for Html to Pdf, which is working perfectly fine, but not in Html to Odt. Please look into the below code lines.
<div style="float:right; width:49%; text-align:right"><img
src="#(Url.Content("~/Content/img/logo.jpg"))" width="225"
height="44" alt="Logo" /></div>
Controller code:
HtmlToPdfConverter htmltoPdfObj = new HtmlToPdfConverter();
htmltoPdfObj.Orientation = PageOrientation.Default;
htmltoPdfObj.CustomWkHtmlArgs = "--margin-top 22 --header-spacing 10 --margin-bottom 20";
byte[] pdfContents = htmltoPdfObj.GeneratePdf(this.GetPDFScript() + htmlContractData + "</body></html>");
Response.Clear();
Response.Charset = "";
Response.ContentType = "application/vnd.oasis.opendocument.text";
string strFileName = "GenerateDocument" + ".odt";
Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
Response.Write(htmlContractData);
Response.End();
htmlContractData > plane cshtml text with above div Image.
Please help me out with this Issue troubling me since long

Related

How to itextsharp PDF open in new tab in chrome using asp.net c#

I m using this code but its not working . Pdf File open in same page
this is my code
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.Charset = string.Empty;
Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
Response.AddHeader("Content-Disposition",
"inline; filename=ChallanReceive.pdf");
Response.OutputStream.Write(PDFData.GetBuffer(), 0, PDFData.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.End();
Just use a hyperlink to the pdf file and set the target to "_blank."
<a href="YourURL" target="_blank">
This will solve you the problem.

How to display pdf file in a seperate tab in a browser?

I have tried a code to open a pdf file in a web browser. It give to allows me to open the file through pdf reader no in the browser. Almost all the codes i found over the internet are also same as this code. But this code doesn't work as i expected.
Help me to figure out the problem in here. I'm using a link button in the aspx.
Here is my code
aspx code
<asp:LinkButton ID="pdfViewLOP" runat="server" Style="margin-left: 10px" OnClick="pdfViewLOP_Click" >View PDF</asp:LinkButton>
aspx.cs
Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "viewPDF.aspx"));
Code of the new page which pdf should be displayed
string name=Session["name"].ToString();
int refNo = Convert.ToInt32(name);
string FilePath = Server.MapPath("~/filesPDF/" + refNo + ".pdf");
WebClient User = new WebClient();
Byte[] buffer = User.DownloadData(FilePath);
if (buffer != null)
{
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
How if you directly create a hyperlink with target _blank and path of file as a href of hyperlink.
So the result hyperlink generated inside the html page is
View PDF

How to avoid downloading files to the download folder and download to other specific folder only in c#

I am trying to convert the gridview to excel and downloading the excel sheet to my computer's specific folder in my c# application.
The problem is: The file is getting downloaded at both the places.The destination folder and also the download folder.
the code for this is:
private void ExportToExcel(GridView GrdView, string fname)
{
Response.Clear();
Response.AddHeader("content-disposition", "inline;filename=" + fname + ".xls");
Response.Charset = "";
Response.ContentType = "application/OCTET-STREAM";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
string renderedGridView = stringWrite.ToString();
File.WriteAllText(#"C:\\Users\abc\\Desktop\" + fname + ".xls", renderedGridView);
Response.Write(stringWrite.ToString());
Response.End();
}
How to avoid the files getting downloaded to the download folder?
Please help!
Thank you
The answer would be to pick one: either render the control to a string and output to Response OR WriteAllText. If you want to use the WriteAllText to save the file to a determined location, then perform the action and pop up a notification to the user that the file was saved.

How to open PDF file in a new tab or window instead of downloading it (using asp.net)?

This is the code for downloading the file.
System.IO.FileStream fs = new System.IO.FileStream(Path+"\\"+fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] ar = new byte[(int)fs.Length];
fs.Read(ar, 0, (int)fs.Length);
fs.Close();
Response.AddHeader("content-disposition", "attachment;filename=" + AccNo+".pdf");
Response.ContentType = "application/octectstream";
Response.BinaryWrite(ar);
Response.End();
When this code is executed, it will ask user to open or save the file. Instead of this I need to open a new tab or window and display the file. How can I achieve this?
NOTE:
File won't necessary be located in the website folder. It might be located in an other folder.
Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
Response.BinaryWrite(fileContent);
And
<asp:LinkButton OnClientClick="openInNewTab();" OnClick="CodeBehindMethod".../>
In javaScript:
<script type="text/javascript">
function openInNewTab() {
window.document.forms[0].target = '_blank';
setTimeout(function () { window.document.forms[0].target = ''; }, 0);
}
</script>
Take care to reset target, otherwise all other calls like Response.Redirect will open in a new tab, which might be not what you want.
Instead of loading a stream into a byte array and writing it to the response stream, you should have a look at HttpResponse.TransmitFile
Response.ContentType = "Application/pdf";
Response.TransmitFile(pathtofile);
If you want the PDF to open in a new window you would have to open the downloading page in a new window, for example like this:
View PDF
this may help
Response.Write("<script>");
Response.Write("window.open('../Inventory/pages/printableads.pdf', '_newtab');");
Response.Write("</script>");
You have to create either another page or generic handler with the code to generate your pdf. Then that event gets triggered and the person is redirected to that page.
Here I am using iTextSharp dll for generating PDF file.
I want to open PDF file instead of downloading it.
So I am using below code which is working fine for me.
Now pdf file is opening in browser ,now dowloading
Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
Paragraph Text = new Paragraph("Hi , This is Test Content");
pdfDoc.Add(Text);
pdfWriter.CloseStream = false;
pdfDoc.Close();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.End();
If you want to download file,
add below line, after this Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
you can return a FileResult from your MVC action.
*********************MVC action************
public FileResult OpenPDF(parameters)
{
//code to fetch your pdf byte array
return File(pdfBytes, "application/pdf");
}
**************js**************
Use formpost to post your data to action
var inputTag = '<input name="paramName" type="text" value="' + payloadString + '">';
var form = document.createElement("form");
jQuery(form).attr("id", "pdf-form").attr("name", "pdf-form").attr("class", "pdf-form").attr("target", "_blank");
jQuery(form).attr("action", "/Controller/OpenPDF").attr("method", "post").attr("enctype", "multipart/form-data");
jQuery(form).append(inputTag);
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
return false;
You need to create a form to post your data, append it your dom, post your data and remove the form your document body.
However, form post wouldn't post data to new tab only on EDGE browser. But a get request works as it's just opening new tab with a url containing query string for your action parameters.
Use this code. This works like a champ.
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = outputPdfFile;
process.Start();

ASP.NET: Write a table (and checkboxes) to a word document

I am writing an ASP.NET Application in c# and I'm trying to write a table to a word document.
I use currently the following code to write to a word doc:
string strContent = "This content goes to the word document";
string attach = "attachment; filename=wordtest.doc";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/msword";
HttpContext.Current.Response.AddHeader("content-disposition", attach);
HttpContext.Current.Response.Write(strContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
Now, my question is if anyone knows how I can write a table to a word document?
And second question is if anyone knows how to write checkboxes to a word doc (like you copy it from a website and paste it in word)
Thanks in advance!
Killerwes
Word can recognise HTML. So you can simply write any HTML to MS WORD with Response.Write(); Method. Here is code sample.
string strBody = "<html>" +
"<body>" +
"<div>Your name is: <b>" + txtName.Text + "</b></div>" +
"<table width="100%" style="background-color:#cfcfcf;"><tr><td>1st Cell body data</td><td>2nd cell body data</td></tr></table>" +
"Ms Word document generated successfully." +
"</body>" +
"</html>";
string fileName = "MsWordSample.doc";
// You can add whatever you want to add as the HTML and it will be generated as Ms Word docs
Response.AppendHeader("Content-Type", "application/msword");
Response.AppendHeader ("Content-disposition", "attachment; filename="+ fileName);
Response.Write(strBody);

Categories