print with PDF Writer

IzzyTwinkly
IzzyTwinkly used Ask the Experts™
on
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);
            .....
         }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Did you try opening file 'a' in the Reader?

Author

Commented:
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";?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
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.

Author

Commented:
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?

Author

Commented:
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();
        }
PDFWriter is ancient - it was part of the default Acrobat installation until Acrobat 5. And there is a good reason why it's no longer distributed with Acrobat: It produces low quality PDF. For anything more modern, you need to use the "Adobe PDF" printer, and Adobe actually documented how to specify an output filename:

http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/Distiller_WinIntro.61.11.html

All you need to do is to set a registry key before you print.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial