The word report document in the project will be converted to a PDF document and output to the web page.CodeIs to read the PDF file and write it to the page output stream:
Protected Void Page_load ( Object Sender, eventargs E)
{
Try
{
// Query the PDF path
String Pdfna = This . Request. querystring [ " PDF " ];
If (Pdfna = Null )
Throw New Exception ( " This page can not be accessed directly " );
String PDF = " Reports \\ " + Pdfna;
PDF = Server. mappath (PDF );
// Read all bytes
If (File. exists (PDF) = False )
Throw New Exception ( " This report is not created " );
Filestream FS = File. Open (PDF, filemode. Open );
Byte [] Buffer = New Byte [Fs. Length];
FS. Read (buffer, 0 , Buffer. Length );
FS. Close ();
// Write to response
Response. contenttype = " Application/PDF " ;
Response. addheader ( " Content-Disposition " , " Filename = " + Pdfna );
Response. addheader ( " Content-Length " , Buffer. length. tostring ());
Response. binarywrite (buffer );
}
Catch (Exception ex)
{
Response. Write (ex. Message );
}
Finally
{
Response. Flush ();
Response. Close ();
Response. End ();
}
}