Link to home
Start Free TrialLog in
Avatar of IzzyTwinkly
IzzyTwinklyFlag for United States of America

asked on

print with PDF Writer

Hi,

I am using the following code to convert a html file(a.html) to b pdf file(b.pdf) using Acroat PDF Writer.  
Please note that I used 'b' for the pdf file name instead of 'b.pdf' since I thought that 'Acrobat PDF Writer' will automatically convert the file to PDF format.

When I run the following code, I see 2 problems.
1. 'b.pdf' file is created.  However, the file 'a'(with no type.  when I open its property window 'type of file' is jsut 'file') is created as well.
2. When I open 'b.pdf' file, I don't see any content from a.html.  It just displays unreadable characters on 'b.pdf'.  No chart or report from 'a.html' is displayed in 'b.pdf'.

What should I do to resolve these two problems?

StreamReader objSr;
        private void button1_Click(object sender, EventArgs e)
        {            
                try
                {
                    printDocument.PrinterSettings.PrinterName = "Acrobat PDFWriter";
                    printDocument.PrinterSettings.PrintToFile = true;
                    printDocument.PrinterSettings.PrintFileName = @"C:\b";
                    printDocument.Print();
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
        }

        private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            objSr = new StreamReader(@"C:\a.html");
            Font printFont = new Font("Arial", 10);
            .....
         }
Avatar of mohan_sekar
mohan_sekar
Flag of United States of America image

Did you try opening file 'a' in the Reader?
Avatar of IzzyTwinkly

ASKER

Hi mohan sekar,
Yes, I opened it in Acrobat reader.  However, only unreadable characters are displayed in there.
What if you specify @"C:\b.pdf";?
mohan sekar..oh..you meant 'a'.  sorry...I misunderstood you.

When I tried to open 'a.html', it says "Adobe Reader could not open 'a.html' because it is either not a supported file type or because the file has been damaged(for example, it was sent as an email attachment and wasn't correctly decoded.)  

However, if I just use 'Acrobat PDFWriter' to print to file and open that file, it displays everything correctly.
and if I specify pdf, the file name becomes b.pdf.pdf and does the same thing.
How are you creating the graphics image that you want to print? Can you post the complete printDocument_PrintPage() event?
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            objSr = new StreamReader(@"C:\a.html");

            Font printFont = new Font("Arial", 10);
            float linePerPage = 0;
            float yPos = 0;
            int count = 0;

            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;

            string line = null;
            linePerPage = e.MarginBounds.Height /
                           printFont.GetHeight(e.Graphics);


            while (count < linePerPage && ((line = objSr.ReadLine()) != null))
            {
                yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
                e.Graphics.DrawString(
                    line,
                    printFont,
                    Brushes.Black,
                    leftMargin,
                    yPos,
                    new StringFormat());
                count++;
            }
                       objSr.Close();
        }
ASKER CERTIFIED SOLUTION
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial