Click here to Skip to main content
15,068,988 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i used iTextSharp.dll for conversion. it generates the pdf but it over write html document text.how can i solve this problem?

any answer will be highly appreciated.

Code:
C#
string filename = string.Empty;
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
                filename = textBox1.Text;

            }
        }

private void button2_Click(object sender, EventArgs e)
        {
            using (StreamReader reader = new StreamReader(filename))
            {
                String line = reader.ReadToEnd();
                Html2PDF(line);
            }


        }
        protected void Html2PDF(string PDFText)
        {
            //HttpContext context = HttpContext.Current;
            StringReader reader = new StringReader(PDFText);

            //Create PDF document 
            Document document = new Document(PageSize.A4);
            HTMLWorker parser = new HTMLWorker(document);

            string PDF_FileName = Path.GetFileNameWithoutExtension(filename) + ".pdf";
            PdfWriter.GetInstance(document, new FileStream(PDF_FileName, FileMode.Create));
            document.Open();

            try
            {
                parser.Parse(reader);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                document.Close();
                MessageBox.Show("PDF File Generated Successfully...");
            }

        }
Posted
Updated 22-Mar-17 0:16am
v2
Comments
   
Reason for my vote of 1
The code makes no sense.
--SA

You can add a PDF printer using something like CutePDF (http://www.cutepdf.com/[^]). This is free, and all you have to do is set up the printing.
   
Comments
   
Yes, good idea, I completely forgot about this approach even though there were many similar questions. My 5, of course.
--SA
Please see my comment: the code makes no sense. How can you ask about some problem if you are not even trying to do what you wanted to do: converting HTML to PDF. By some reason, you first read the file to a string. You could pass the StreamReader instance to HTMLWorker directly, but once you use the file content only, you don't touch this file anymore. If you screw up it, you do it somewhere else. No wonder: setting the file name and using for reading in response to the clicks to different buttons is extremely awkward and unsafe. What if you did not select a file before clicking a button for conversion? So, who knows what else could you screw up?

But then, you parse the string with HTMLWorker and… do nothing. You don't do anything to PDF document. Who knows what did you mean by that?

Please see this code sample:
http://blog.rubypdf.com/2007/10/10/using-htmlworker-to-parse-html-snippets-and-convert-to-pdf/[^].

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900