In vb.net I need to print the contents showing in a browser control to printer.
I have used Gecko web-browser control in winform application and there is no direct way to print the page's contents.
Either way to print direct using InnerHtml or converting that html to pdf and then printing the pdf document.
currently I am using a third party library `ItextSharpe' but it gives errors.
public byte[] GetPDF(string pHTML) {
byte[] bPDF = null;
MemoryStream ms = new MemoryStream();
TextReader txtReader = new StringReader(pHTML);
// 1: create object of a itextsharp document class
Document doc = new Document(PageSize.A4, 25, 25, 25, 25);
// 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);
// 3: we create a worker parse the document
HTMLWorker htmlWorker = new HTMLWorker(doc);
// 4: we open document and start the worker on the document
doc.Open();
htmlWorker.StartDocument();
// 5: parse the html into the document
htmlWorker.Parse(txtReader);
// 6: close the document and the worker
htmlWorker.EndDocument();
htmlWorker.Close();
doc.Close();
bPDF = ms.ToArray();
return bPDF;
}
Byte[] bytes;
bytes = GetPDF(browse.Document.Body.InnerHtml);
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
System.IO.File.WriteAllBytes(testFile, bytes);
but throws errors while parsing.
Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.
I have seen different examples over web but this is totally different, the examples or duplicate answer is about to export panels or grids but this is dynamic HTML and i need to convert it to PDF or print directly the client area.
Related
I am converting sharepoint aspx page content to pdf using the below code but while parsing the htmlstring its throwing this error: "Invalid nested tag div found, expected closing tag script" How can I handle this kind of invalid tag or tag closing issue thru the C# code, so that I can pass the valid htmlString to the method.
//Create a byte array that will eventually hold our final PDF
Byte[] bytes;
//Boilerplate iTextSharp setup here
//Create a stream that we can write to, in this case a MemoryStream
using (var ms = new MemoryStream()) {
//Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
using (var doc = new Document()) {
//Create a writer that's bound to our PDF abstraction and our stream
using (var writer = PdfWriter.GetInstance(doc, ms)) {
//Open the document for writing
doc.Open();
//Our sample HTML
var example_html = GetHtmlSource(HttpContext.Current.Request.Url.ToString());
using (var srHtml = new StringReader(example_html)) {
//Parse the HTML
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml); **In this line throwing exception**
}
}
doc.Close();
}
}
bytes = ms.ToArray();
I am using the following code for generating a PDF file.
It is working good, but now i want to generate 4 PDF's at the same time.
I tried by again initiating Document & repeating the whole code for generating 2nd PDF report, But it generates only 1 PDF.
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWrite object, writing the output to a MemoryStream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
document.Open();
string contents = System.IO.File.ReadAllText(Server.MapPath("~/Reports/Original.html"));
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
foreach (var htmlElement in parsedHtmlElements)
document.Add(htmlElement as IElement);
document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename=Receipt-{0}.pdf", "Report"));
Response.BinaryWrite(output.ToArray());
return View();
How to generate multiple PDF's?
You are outputting the bytes as a response, so you would never be able of generating 2 different files in one response. Only one response per request.
If you want the user to download 2 different PDFs at the same time you could call the controller using javascript from the view.
I want to convert HTML into PDF in windows form. I have come across many fabulous articles online such as these:
http://www.aspsnippets.com/Categories/iTextSharp.aspx
http://www.aspsnippets.com/Articles/Export-HTML-DIV-contents-to-PDF-using-iTextSharp-in-ASPNet.aspx
http://www.aspsnippets.com/Articles/Export-ASP.Net-GridView-to-PDF-with-Custom-Columns-Widths-using-iTextSharp.aspx
But they do not quite address my problem.
Is it possible that I can just pass a string of HTML and convert it into PDF in Winform?
For example :
string html = "<table><tr><td>Arbaaz</td><tr></table>"
Can I just pass this string to some iText method to create PDF?
Got the answer I was looking for here ..
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.HTMLWorker hw =
new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(htmlText));
document.Close();
https://stackoverflow.com/a/18378661/2064292
How can I generate a pdf in winRT apps? I'm using iTextSharp to generate pdfs in windows store apps, but winRT does not have filestream, filemode or filedirectory. help
Here is my code:
iTextSharp.text.pdf.PdfWriter writer =
iTextSharp.text.pdf.PdfWriter.GetInstance(
doc, new System.IO.FileStream(System.IO.Directory.GetCurrentDirectory() +
"\\ScienceReport.pdf", System.IO.FileMode.Create
)
);
I can generate a PDF file in winrt
string path = #"ExportPDF.pdf";
var storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
// Create empty PDF file.
var file = await storageFolder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting);
if (file != null)
{
await FileIO.WriteTextAsync(file, string.Empty);
}
// Open to PDF file for read/write.
StorageFile sampleFile = await storageFolder.GetFileAsync(path);
var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
// Create an instance of the document class which represents the PDF document itself.
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
// Create an instance to the PDF file by creating an instance of the PDF
// Writer class using the document and the filestrem in the constructor.
PdfWriter writer = PdfWriter.GetInstance(document, stream.AsStream());
// Add meta information to the document
document.AddAuthor("Jigs");
document.AddCreator("Sample application");
document.AddKeywords("PDF App");
document.AddSubject("Document subject - Describing the steps creating a PDF document");
document.AddTitle("The document title - PDF creation");
// Open the document to enable you to write to the document
document.Open();
// Add a simple and wellknown phrase to the document in a flow layout manner
document.Add(new Paragraph("Hello!"));
// Close the document
document.Close();
// Close the writer instance
writer.Close();
I'm trying to convert HTML to PDF with iTextSharp.
Here's my code:
Document doc = new Document(PageSize.A4);
StringReader reader = new StringReader(responseHtml);
FileStream pdfStream = new FileStream("C:\\temp\\foo.pdf", FileMode.OpenOrCreate);
PdfWriter writer = PdfWriter.GetInstance(doc, pdfStream);
doc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, reader);
doc.Close();
The issue is that the PDF file stops after 2 pages.
The rest of the content just doesn't make it to the PDF file.
When I change the PageSize to A1, I get the whole content, 'cause it fits on two A1 pages.
How do I get it to create more than two pages?