Code /// <Summary>
/// Download button
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected Void Button2_click ( Object Sender, eventargs E)
{
Using (VAR imagestream = New Filestream (mappath ( " ~ /Views " ) + " /Printlinks " , Filemode. Open, fileaccess. Read, fileshare. readwrite ))
{
Byte [] Bytes = This . Streamtobytes (imagestream );
This . Download (bytes, " PDF " );
}
}
/// <Summary>
/// Generate a PDF copy on the server
/// </Summary>
Private Void Createpdf ()
{
VaR bytes = Getdata ();
Int Width = Bytes. toimage (). width;
Int Height = Bytes. toimage (). height;
Document document = New Document ( New Rectangle (width, height ), 0 , 0 , 0 , 0 );
Using (VAR stream = New Filestream (mappath ( " ~ /Views " ) + " /Printlinks " , Filemode. Create, fileaccess. Write, fileshare. None ))
{
Using writer. getinstance (document, stream );
Document. open ();
Using (Stream imagestream = New Memorystream (bytes ))
{
VaR Image = Itextsharp. Text. image. getinstance (imagestream );
Document. Add (image );
}
Document. Close ();
}
}
/// <Summary>
/// Download to local
/// </Summary>
/// <Param name = "bytes"> </param>
/// <Param name = "filetype"> </param>
Public Void Download ( Byte [] Bytes, String Filetype)
{
Response. Buffer = True ;
// Page. response. Clear (); // Clear all buffer content
Page. response. contenttype = " Application/octet-stream " ;
Page. response. addheader ( " Content-Disposition " , " Attachment; filename = " + Httputility. urlencode ( " Print. " + Filetype ));
Byte [] File = Bytes;
Response. binarywrite (File );
Response. Flush ();
Response. End ();
}
Use a third-party dll: itextsharp.
The image is converted to byte [] in the cache. If it is on a local disk
VaR imagestream = newfilestream ("test.jpg", filemode. Open, fileaccess. Read, fileshare. readwrite)
In this way.
Call createpdf at page Initialization