Online document preview: technical details of document generation

Source: Internet
Author: User
Tags image converter
Online document preview Study Series
General idea
Technical details of document generation
Use Baidu Reader

The previous "online document preview: overall idea" was welcomed by many friends. For this reason, I will continue to explain some technical details of the two steps of online document preview. I will introduce C # And Windows as an example. Remember, this article describes how to convert a document into a pdf file and then convert it into another format. We will not discuss how to display the converted flash file (for the moment, let's assume FlexPaper is used for demonstration ). The source code and Software downloading of the operations mentioned in this article are included later in this article. Some software must be purchased and authorized for use.Four basic operations1. Call a Windows Printer to print documents PrintDocument docToPrint = new PrintDocument ();
DocToPrint. DocumentName = GetPath (sourcePath );
// Start printing
DocToPrint. Print ();

 

2. Call the command line to execute the CMD commandCode

/// <Summary>
/// Run the command
/// </Summary>
/// <Param name = "strShellCommand"> command string </param>
/// <Returns> command running time </returns>
Private static double RunShell (string strShellCommand)
{
Double spanMilliseconds = 0;
DateTime beginTime = DateTime. Now;

Process cmd = new Process ();
Cmd. StartInfo. FileName = "cmd.exe ";
Cmd. StartInfo. UseShellExecute = false;
Cmd. StartInfo. CreateNoWindow = true;
Cmd. StartInfo. Arguments = String. Format (@ "/c {0}", strShellCommand );
Cmd. Start ();
Cmd. WaitForExit ();

DateTime endTime = DateTime. Now;
TimeSpan timeSpan = endTime-beginTime;
SpanMilliseconds = timeSpan. TotalMilliseconds;

Return spanMilliseconds;
}

 

 3. Check whether the file has been generated.

Code

/// <Summary>
/// Check whether the conversion is successful (whether the file is generated)
/// </Summary>
/// <Param name = "sourcePath"> check the file address. </param>
/// <Param name = "targetPath"> address to be copied (consistent with sourcePath if real replication is not required) </param>
/// <Param name = "timeout"> maximum waiting time </param>
/// <Returns> </returns>
Private static bool IsParseSuccess (string sourcePath, string targetPath, int timeout)
{
Bool isSuccess = false;

If (timeout <= 0)
Timeout = 30;

Int I = 0;
While (! RenameFile (sourcePath, targetPath ))
{
Thread. Sleep (1000 );
I ++;
If (I = timeout)
Break;
}
If (I <timeout)
IsSuccess = true;

Return isSuccess;
}

/// <Summary>
/// Rename the file (used to check whether the file is generated successfully)
/// </Summary>
/// <Param name = "sourePath"> source address </param>
/// <Param name = "targetPath"> target address </param>
/// <Returns> </returns>
Private static bool RenameFile (string sourePath, string targetPath)
{
Bool isOpen = false;

// If the address is the same, move it directly to check whether the file has been generated; otherwise, copy the file (the target file may be faulty because it exists)
If (sourePath. Equals (targetPath ))
{
Try
{
// Move the file
File. Move (sourePath, targetPath );
IsOpen = true;
}
Catch (Exception e)
{

}
}
Else
{
Bool isCopySuccess = false;
Try
{
// Copy an object
File. Copy (sourePath, targetPath, true );
IsCopySuccess = true;
}
Catch (Exception e)
{
IsCopySuccess = false;
}
If (isCopySuccess)
{
// If the copy is successful, delete the source file
File. Delete (sourePath );
}
}

Return isOpen;
}

 

 4. Killing and executing functions

Code

/// <Summary>
/// Close the process based on the process name
/// </Summary>
/// <Param name = "processName"> </param>
Private static void KillPrecess (string processName)
{
Foreach (Process p in Process. GetProcesses ())
{
If (p. ProcessName = processName)
{
P. Kill ();
}
}
}

 

Document to pdfThe document we introduced to convert to pdf requires that it is more common, that is, you do not need to write a specific program according to the specific document, and not only need to support Office documents, it is better to support other documents such as txt and html. 1. Use pdffacloud (see the source code method: parsepdfwithpdffacloud)Note: The software must be registered to use the principle:. call the system default printer (pdffacloud) to print the document; B. copy the pdf file from the temporary directory of the printer to the target path. pdffacloud settings:. open the printer and set "pdffacloud Pro" as the default printer; B. open "Printer Preferences" of "pdffacloud Pro", and register the printer on the "Licensing" tab; c. open "Printer Preferences" of "pdffacloud Pro" and combine the program to set the following two figures;

 

Advantage:. this method can be used to convert printed files into pdf files. Disadvantages:. the file printing window is often displayed on the server; B. some corrupted files or incorrectly formatted documents will abort the generation process; c. if you encounter a document with a virus macro, it will cause damage to the server. Note:. during the printing process, a large number of temporary files are generated in the user's directory. You need to clear the junk files in time, or migrate the corresponding directory to a location with a large disk space. B. windows Server needs to install the Server version pdffacloud; 2. Use FlashPaper (see the source code method: ParsePDFWithFlashPrinter)Principle: Call the command line "flashprinter c: \ document.doc-o c: \ document.pdf" for printing. Advantages:. this method can be used to convert printed files to pdf files. relatively simple:. due to program problems, printing may sometimes fail, and hundreds of processes run simultaneously to drag the system down; 3. Other methods (source code not included)Use other Virtual printers, such as: Virtual PDF Printer uses jcom or other methods to call the Office Component for conversion. For details, see: convert office documents (word, excel, powerpoint) use JodConverter to call OpenOffice conversion for pdf conversion. For details, see JodConverter to convert Office to PDF format. Pdf to flash (see source code: ParseSWF)Principle: Use swftools's limit 2swf, call the command line limit 2swf.exe-T 9-p 1-3-s limit agedir = D: \ xpdf-3.02pl5 \ xpdf-chinese-simplified D: \ document.pdf-o D: \ document.swf "for conversion; FAQ: 1, some documents after conversion garbled, console prompt: cannot find the "Adobe-GB1" font solution: In the iis2swf command with Language Pack, For details, see: FUSE (parameter: -T 9), so the compatibility is the best. Pdf to image conversion (see source code: ParseCoverImage)Principle: use the "VeryPDF PDF To Image converter.pdf software, and use the command line To convert 2img.exe-r 25-f 1-l 1-I c: \ 1.pdf-o c: \ 11.jpg; Pdf to text (see the source code method: ParseText)Xpdf is a good tool to read the text in documents to generate summaries or index documents. Principle: Use the “xpdf”software to call the command line to convert totext.exe c: \ 1.pdf c: \ 1.txt. FAQ: To enable xpdf to support Chinese characters, you also need to download the Chinese Language Pack and make some configuration, for detailed configuration in xpdf, refer to: Examples

Feedback on Article quality, you can comment through the quick channel:

 

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.