PDF conversion of key Library technologies to HTML

Source: Internet
Author: User

Aspose.pdf the use of methods, the official online introduction of all have, but are relatively simple, mainly to play an example role.

In combination with my own actual work, I have done a good job of sorting out the relevant methods, and will make a brief explanation of the key points.

Before converting to HTML, you must define the FilePath and folder, remember to remember.

classpdftools{/// <summary>    ///source File/// </summary>     Public stringfilepath {Get;Set; } /// <summary>    ///     /// </summary>    Private stringfilename {Get {            string_filename =Path.getfilename (filepath); _filename= _filename. Substring (0, _filename. IndexOf ('.')); return_filename; }    }    /// <summary>    ///Final file target folder/// </summary>     Public stringfolder {Get;Set; }  PublicPdftools () {} PublicPdftools (string_filepath,string_folder) {filepath=_filepath; Folder=_folder; Folder= folder. Replace ("/",@"\"); if(folder. EndsWith (@"\") ==false) Folder= Folder +@"\"; }    /// <summary>    ///convert PDF files to a full HTML///filepath is a PDF source file, you must complete the file path///folder is the final save directory, and the generated files are in this directory/// </summary>    /// <returns></returns>     Public BOOLPdf2html_nosplit () {//determine if a PDF file exists        if(file.exists (filepath) = =false)            return false; Folder= folder. Replace ("/",@"\"); if(folder. EndsWith (@"\") ==false) Folder= Folder +@"\"; if(directory.exists (folder) = =true) Directory.delete (folder,true);        Directory.CreateDirectory (folder); Document Doc=NewDocument (filepath); Htmlsaveoptions HSO=Newhtmlsaveoptions (); Hso. Splitintopages=false; Hso. Fixedlayout=true; Hso.compresssvggraphicsifany=true;

I prefer to have all the resources stored in a folder, so I need to rewrite the following three function HSO. Customresourcesavingstrategy=NewHtmlsaveoptions.resourcesavingstrategy (resourcestrategy); Hso. Customcsssavingstrategy=NewHtmlsaveoptions.csssavingstrategy (cssstrategy); Hso. Customstrategyofcssurlcreation=NewHtmlsaveoptions.cssurlmakingstrategy (cssurlstrategy); Hso. Fontsavingmode=HtmlSaveOptions.FontSavingModes.AlwaysSaveAsTTF; stringOutpath = folder + filename +". html"; Try{doc. Save (Outpath, HSO); return true; } Catch{directory.delete (folder,true); return false; } }/// <summary> ///generate home page thumbnails based on PDF files/// </summary> /// <param name= "_input" >PDF Source file full path</param> /// <param name= "_output" >Picture Save Path</param> Public voidCreatethumb (string_input,string_output) { if(File.exists (_output) = =true) File.delete (_output); using(Document doc =NewDocument (_input))using(FileStream Imgstream =NewFileStream (_output, FileMode.Create)) { //Picture Display qualityAspose.Pdf.Devices.Resolution Resolution =NewAspose.Pdf.Devices.Resolution ( -); Aspose.Pdf.Devices.PngDevice Device=NewAspose.Pdf.Devices.PngDevice (resolution); Device. Process (Doc. pages[1], Imgstream); Imgstream. Close (); } } /// <summary> ///extract body from PDF file/// </summary> /// <param name= "_input" >pdf file directory </param> /// <returns></returns> Public stringGettxtfrompdf (string_input) {Document doc=NewDocument (_input); StringBuilder SB=NewStringBuilder (); for(inti =1; I <= Doc. Pages.count; i++) {Aspose.Pdf.Text.TextAbsorber ab=NewAspose.Pdf.Text.TextAbsorber (); Ab. Textsearchoptions.limittopagebounds=true; Try{doc. Pages[i]. Accept (AB); Sb. Append (AB. Text); } Catch { return NULL; }} SB= sb. Replace ("\ r \ n"," "). Replace ("   "," "); stringOutput =sb. ToString (); while(Output. IndexOf (" ") >=0) Output= output. Replace (" "," "); returnoutput; }/// <summary> ///Rename the font, picture, and if you don't use the function, the page may appear to be abnormal/// </summary> /// <param name= "resource" ></param> /// <returns></returns> Private stringResourcestrategy (saveoptions.resourcesavinginfo Resource) {stringResourceName =""; if(Resource. ResourceType = =SaveOptions.NodeLevelResourceType.Image) resourcename= Guid.NewGuid (). ToString () +path.getextension (Resource. Supposedfilename); Elseresourcename=Resource. Supposedfilename; if(!directory.exists (folder)) Directory.CreateDirectory (folder); stringoutfile = folder +resourcename; if(File.exists (resourcename) = =true) returnresourcename; System.IO.BinaryReader Reader=NewBinaryReader (Resource. Contentstream); System.IO.File.WriteAllBytes (outfile, reader. Readbytes ((int) resource. Contentstream.length)); returnresourcename; }/// <summary> ///to generate a page-referenced CSS file/// </summary> /// <param name= "resource" ></param> Private voidCssstrategy (htmlsaveoptions.csssavinginfo Resource) {stringPath = folder+ filename +@". CSS"; BinaryReader Reader=NewBinaryReader (Resource. Contentstream); File.writeallbytes (path, reader. Readbytes ((int) resource. Contentstream.length)); } /// <summary> ///overriding the CSS address of a page reference/// </summary> /// <param name= "resource" ></param> /// <returns></returns> Private stringCssurlstrategy (htmlsaveoptions.cssurlrequestinfo Resource) {returnFileName +". CSS"; }}

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.