My iTextSharp Code is like below I am using XSLT for getting HTML design:
int pageCount = GetNoofPages(outputString);
StringReader sr = new StringReader(outputString.Replace("###Noofpages###", pageCount.ToString()));
byte[] content = null;
string savedfile = string.Empty;
using (MemoryStream myMemoryStream = new MemoryStream())
{
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A4, 40f, 10f, 40f, 36);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
StyleSheet style = new StyleSheet();
htmlparser.SetStyleSheet(style);
PdfWriter myPDFWriter = PdfWriter.GetInstance(pdfDoc, myMemoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
HTMLWorker htmlparser1 = new HTMLWorker(pdfDoc);
StyleSheet style1 = new StyleSheet();
htmlparser1.SetStyleSheet(style1);
pdfDoc.Close();
content = AddPageNumbers(myMemoryStream.ToArray());
}
return content;
My output is:
Leg 1: 4311 route
// Now rest of the page is blank
.
.
.
.
.
// Now it will continue on next page
Route details:
// remaining instructions
Please help me.
Related
This my code, why do it produces the error?
sMailBody=MailstrBody.ToString();
StringBuilder sample = new StringBuilder();
sample = MailstrBody;
string passno = dsMail1.Tables[0].Rows[0]["PASSNO"].ToString();
HttpResponse Response = HttpContext.Current.Response;
EmailContentDAL example = new EmailContentDAL();
example.Pdf(sample, Response, passno);
public void Pdf(StringBuilder sample, HttpResponse currentResponse, string passno)
{
StringReader sr = new StringReader(sample.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc,
currentResponse.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
currentResponse.Clear();
currentResponse.ContentType = "application/pdf";
currentResponse.AddHeader("Content-Disposition", "attachment; filename=GetPass_" + passno + ".pdf");
currentResponse.Buffer = true;
currentResponse.Cache.SetCacheability(HttpCacheability.NoCache);
currentResponse.Write(pdfDoc);
currentResponse.End();
}
The above code produces null reference error at htmlparser.Parse(sr), but at StringReader sr = new StringReader(sample.ToString()), sample.toString() shows a value.
I'm calling ExportToPDF2 method in a for loop to produce pdf documents.
The problem is the loop stops after the Respons.End().
How can I get this resolved, and is there a better way than using this technique?
private int ExportToPDF2()
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
return 0;
}
It strange to me that you are saving the output of same page (Page.RenderControl(hw);) multiple times!
Anyway, It's not possible to send multiple outputStream from Server, you can try
Response.Write(somefile);
Response.End();
textBox1.Text = "1111"; // it wont work because you can't have multiple outputstream
The best workaround you can have is to make a zip file then do Response.Write(zipFile)
But if you still insist on having them separately(multiple downloads) better you save each file on the server first by:
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, new FileStream(Server.MapPath("~") + pdfName + ".pdf");
}
then use this trick https://stackoverflow.com/a/30682695/336511 to support multiple download. note that this will work on modern browsers.
var links = [
'https://s3.amazonaws.com/Minecraft.Download/launcher/Minecraft.exe',
'https://s3.amazonaws.com/Minecraft.Download/launcher/Minecraft.dmg',
'https://s3.amazonaws.com/Minecraft.Download/launcher/Minecraft.jar'
];
function downloadAll(urls) {
var link = document.createElement('a');
link.setAttribute('download', null);
link.style.display = 'none';
document.body.appendChild(link);
for (var i = 0; i < urls.length; i++) {
link.setAttribute('href', urls[i]);
link.click();
}
document.body.removeChild(link);
}
<button onclick="downloadAll(window.links)">Test me!</button>
I am trying to export a div to pdf with style using code:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + Fullname.Text + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
var cssText = System.IO.File.ReadAllText(Server.MapPath("/css/bootstrap.css"));
var memoryStream = new MemoryStream();
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
pfd.RenderControl(htmlTextWriter);
StringReader stringReader = new StringReader(stringWriter.ToString().Replace("<br>", "<br/>").Replace("<td>", "<td/>").Replace("<tr>", "<tr/>").Replace("<td>", "<td/>"));
Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
PdfWriter writer = PdfWriter.GetInstance(Doc, Response.OutputStream);
Doc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, Doc, stringReader);
Doc.Close();
Response.Write(Doc);
Response.End();
How i can attach css file to pdf to keep same appearance in HTML?
Try below code,
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
byte[] pdf; // result will be here
var cssText = File.ReadAllText(MapPath("~/css/style.css"));
var html = File.ReadAllText(MapPath("~/css/index.html"));
using (var memoryStream = new MemoryStream())
{
var document = new Document(PageSize.A4, 20, 20, 30, 30);
var writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
{
using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, htmlMemoryStream, cssMemoryStream);
}
}
document.Close();
pdf = memoryStream.ToArray();
}
I am converting html into pdf. It is working fine. but when i try to save html into any location it gives me error Network path not found.
I am having some line of code
#region Trying to convert pdf
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
StringReader sr = new StringReader(htmlStringToConvert);
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
#endregion
I am getting error at htmlparser.Parse(sr).
Thanks in advance
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DivMaster.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 20, 20, 10, 20);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
Add DivMaster (div) in design page and Add ur HTML code inside that DivMaster.
Note: Use % or px throughout HTML design.
my code is:
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
GridView2.AllowPaging = false;
GridView2.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new iTextSharp.text.Document(PageSize.A2);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("contentdisposition","attachment;filename=CustomerReport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}*/
please help me to resolve this. In this i am getting font size too small error.Error in htmlparser.parse()