Convert pdf to image stream and display jquery to the page. Convert jquery

Source: Internet
Author: User

Convert pdf to image stream and display jquery to the page. Convert jquery

First, you need to learn how to use pdf. js before displaying pdf files. Why not directly transfer images? This is probably because images cannot be stored on the server. You can only transfer the pdf path to the image stream. Directly go to the demo code: Here I will reference O2S. ComponentModel. DataAnnotations. dll (http://pan.baidu.com/s/1nuecq, password hep9)

Using System;
Using System. Collections. Generic;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. IO;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using O2S. Components. PDFRender4NET;

Namespace WebApplication1
{
Public partial class WebForm1: System. Web. UI. Page
{
Public enum Definition
{

One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10

}

/// <Summary>

/// How to convert a PDF document to an image

/// </Summary>

/// <Param name = "pdfInputPath"> pdf file path </param>

/// <Param name = "imageOutputPath"> image output path </param>

/// <Param name = "imageName"> name of the generated image </param>

/// <Param name = "startPageNum"> switch from the page of the PDF document </param>

/// <Param name = "endPageNum"> the conversion starts from the page number of the PDF document. </param>

/// <Param name = "imageFormat"> set the image format </param>

/// <Param name = "definition"> set the image definition. The larger the number, the clearer the image. </param>

Public static List <byte []> convert00002image (string pdfInputPath, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
{

PDFFile pdfFile = PDFFile. Open (pdfInputPath );

// Validate pageNum

If (startPageNum <= 0)
{

StartPageNum = 1;

}

If (endPageNum> pdfFile. PageCount)
{

EndPageNum = pdfFile. PageCount;

}

If (startPageNum> endPageNum)
{

Int tempPageNum = startPageNum;

StartPageNum = endPageNum;

EndPageNum = startPageNum;

}

// Create a byte array to store the stream Array
List <byte []> byteList = new List <byte []> ();
// Cyclically convert the entire pdf file into an image stream
For (int I = startPageNum; I <= endPageNum; I ++)
{
// Bitmap generated by pdf converts the image stream
Bitmap pageImage = pdfFile. GetPageImage (I-1, 56 * (int) definition );
MemoryStream MS = new MemoryStream ();
PageImage. Save (MS, imageFormat );
PageImage. Dispose ();
Byte [] byteImg = ms. ToArray ();
ByteList. Add (byteImg );
}
PdfFile. Dispose ();
Return byteList;
}


// The above is to convert the pdf file to the image stream, and the image is not stored locally. The following is only an extension


// Stream the image in binary format
Public byte [] SaveImage (String path)
{
FileStream fs = new FileStream (path, FileMode. Open, FileAccess. Read); // Save the image as a file stream
BinaryReader br = new BinaryReader (fs );
Byte [] imgBytesIn = br. ReadBytes (int) fs. Length); // read the stream into the byte array
Return imgBytesIn;
}

// The image represented by the real binary stream
Public void ShowImgByByte (byte [] imgBytesIn)
{
Response. ContentType = "image/jpg"; // garbled characters will appear if this parameter is not found
Response. BinaryWrite (imgBytesIn );

}

}
}

 

With how the image stream is displayed on the page, you can directly use the html image control to directly display the code.

/// <Summary>
/// Convert the pdf path to an image stream
/// </Summary>
/// <Returns> </returns>
Public string PdfPathToImage (string path)
{
// Obtain the image stream Array
IList <byte []> imgBytesIn = effectoimghelper. convert%2image (path, 1,100, ImageFormat. Jpeg, effectoimghelper. Definition. Three );
// Spell json data
StringBuilder strSb = new StringBuilder ();
StrSb. Append ("[");
Int32 _ index = 1;
// Convert the image stream to Base64 through Traversal
Foreach (byte [] s in imgBytesIn)
{
If (imgBytesIn. Count = _ index)
{
StrSb. Append ("{");
StrSb. Append ("\" value \ ": \" "+ Convert. ToBase64String (s) + "\"");
StrSb. Append ("}");
}
Else
{
StrSb. Append ("{");
StrSb. Append ("\" value \ ": \" "+ Convert. ToBase64String (s) + "\"");
StrSb. Append ("},");
}
_ Index ++;
}
StrSb. Append ("]");
String strValue = strSb. ToString ();
Return JsonConvert. SerializeObject (strValue );
}

// Public method for converting pdf path to image
Function PdfPathToImage (path ){
$. Ajax ({
Type: "post ",
Url: "/Evaluation/PdfPathToImage ",
Data: {"path": path },
Async: true,
DataType: 'json ',
Success: function (data ){
// Get base64jsons data and convert it to an object
Var p = eval (data );
// If the object is not empty, the html image control is spliced cyclically.
If (p ){
Var _ html = "";
For (var item in p ){
_ Html + = ' ';
}
$ ("# Masterslider"). append (_ html );
}
}
});
}

Continue to learn. If you need optimization, please submit your suggestion. If you do not want to enter, you will leave!

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.