Asp.net JPG to PDF and download

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.