Use C # To open a PDF file

Source: Internet
Author: User
Use C # To open a PDF file

Http://www.o2sol.com/public/webui/samples.shtml
Download XpdfRasterizer. dll and XpdfRasterizerNet. dll.
For example
Using XpdfRasterizerNet;

Protected System. Web. UI. WebControls. Button Button1;
Protected System. Web. UI. HtmlControls. HtmlInputFile File1;
Protected System. Web. UI. WebControls. Label Label1;
Protected System. Web. UI. WebControls. PlaceHolder PlaceHolder1;
Private void Page_Load (object sender, System. EventArgs e)
{
This. Label1.Text = "";
}

# Region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web Form Designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/// <Summary>
/// Required method for Designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void InitializeComponent ()
{
This. Button1.Click + = new System. EventHandler (this. button#click );
This. Load + = new System. EventHandler (this. Page_Load );

}
# Endregion

Private string GetFilePath ()
{
/// 4/19/2005 5:44:28
String time = System. DateTime. Now. ToString ();
Time = time. Replace ("/","");
Time = time. Replace ("","");
Time = time. Replace (":","");
// Time + = ". pdf ";
String path = Server. MapPath ("20172jpg. aspx ");
Path = path. Replace ("20172jpg. aspx", @ "Files \" + time );
Return path;
}
Private void button#click (object sender, System. EventArgs e)
{
// Upload
Int len = File1.PostedFile. FileName. Length;
String filetype = File1.PostedFile. FileName. Substring (len-3, 3 );
If (filetype. ToLower ()! = "Pdf ")
{
This. Label1.Text = "Please select pdf file ";
Return;
}
Else
This. Label1.Text = "";
String fileName = GetFilePath ();
String pdffile = fileName + ". pdf ";
If (File1.PostedFile! = Null)
{
Try
{
File1.PostedFile. SaveAs (pdffile );
}
Catch
{
Response. Write ("Upload fail ...");
}
}
// Convert to jpg

Double dpi = 120;
XpdfRasterizerNet. XpdfRasterizerClass rast = new XpdfRasterizerNet. XpdfRasterizerClass ();
Rast. loadFile (pdffile );
For (int page = 1; page <= rast. numPages; page ++)
{
String jpgfile = fileName + page. ToString () + ". bmp ";
Try
{
Rast. writePageBitmap (page, dpi, rast. imageRGB, rast. imageFileBMP, jpgfile );
System. Web. UI. WebControls. Image image = new System. Web. UI. WebControls. Image ();
Image. ImageUrl = jpgfile;
This. PlaceHolder1.Controls. Add (image );
}
Catch
{
Response. Write ("Convert fail ...:");
}
}
Rast. closeFile ();

}

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.