IzzyTwinkly
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.PrinterSetti ngs.Printe rName = "Acrobat PDFWriter";
printDocument.PrinterSetti ngs.PrintT oFile = true;
printDocument.PrinterSetti ngs.PrintF ileName = @"C:\b";
printDocument.Print();
}
catch (Exception err)
{
MessageBox.Show(err.Messag e);
}
}
private void printDocument_PrintPage(ob ject sender, System.Drawing.Printing.Pr intPageEve ntArgs e)
{
objSr = new StreamReader(@"C:\a.html") ;
Font printFont = new Font("Arial", 10);
.....
}
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.PrinterSetti
printDocument.PrinterSetti
printDocument.PrinterSetti
printDocument.Print();
}
catch (Exception err)
{
MessageBox.Show(err.Messag
}
}
private void printDocument_PrintPage(ob
{
objSr = new StreamReader(@"C:\a.html")
Font printFont = new Font("Arial", 10);
.....
}
Did you try opening file 'a' in the Reader?
ASKER
Hi mohan sekar,
Yes, I opened it in Acrobat reader. However, only unreadable characters are displayed in there.
Yes, I opened it in Acrobat reader. However, only unreadable characters are displayed in there.
What if you specify @"C:\b.pdf";?
ASKER
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.
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.
ASKER
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?
ASKER
private void printDocument_PrintPage(ob ject sender, System.Drawing.Printing.Pr intPageEve ntArgs 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.Grap hics);
while (count < linePerPage && ((line = objSr.ReadLine()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(e.Grap hics));
e.Graphics.DrawString(
line,
printFont,
Brushes.Black,
leftMargin,
yPos,
new StringFormat());
count++;
}
objSr.Close();
}
{
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.Grap
while (count < linePerPage && ((line = objSr.ReadLine()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(e.Grap
e.Graphics.DrawString(
line,
printFont,
Brushes.Black,
leftMargin,
yPos,
new StringFormat());
count++;
}
objSr.Close();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.