During the project development process, sometimes it is necessary to call third-party programs to implement certain functions of this system, such as the swftools plugin that needs to be used in this article. So how do you use this plugin in the program, and how does the plugin convert PDF files into SWF files? Next, I will give a brief introduction.
In the .NET platform, C# provides an operational access process to local and remote, enabling the start and stop of system processes. This class is, let’s first understand this class.
1. Analytical Class
Using the Process class in C# can provide local and remote access processes, enabling the start and stop of system processes, and this class can manage system processes. Some common methods in this class: Start(), Kill(), WaitForExit() and other methods; StartInfo, FileName, CreateNoWindow and other properties.
() Method: Start (or reuse) the process resource specified by the StartInfo property of this Process component and associate it with the component. true if process resources are started, and false if new process resources are not started (for example, if existing processes are reused).
Let me introduce the implementation code of this method in detail:
/// <devdoc> /// <para> /// <see cref=''/> If the process resource is reused instead of being started, the reused process is associated with the <see cref =''/> part. /// </para> /// </devdoc> [ResourceExposure()] [ResourceConsumption(, )] public bool Start() { Close(); ProcessStartInfo startInfo = StartInfo; if ( == 0) throw new InvalidOperationException(()); if () { #if !FEATURE_PAL return StartWithShellExecuteEx(startInfo); #else throw new InvalidOperationException((SR.net_perm_invalid_val, "", true)); #endif // !FEATURE_PAL } else { return StartWithCreateProcess(startInfo); } }
() Method: Stop the associated process immediately. Kill forces the process to be terminated, and the Kill method will be executed asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine whether the process has exited.
Let me introduce the implementation code of this method in detail:
[ResourceExposure()] [ResourceConsumption()] public void Kill() { SafeProcessHandle handle = null; try { handle = GetProcessHandle(NativeMethods.PROCESS_TERMINATE); if (!(handle, -1)) throw new Win32Exception(); } finally { ReleaseProcessHandle(handle); } }
SafeProcessHandle GetProcessHandle(int access) { return GetProcessHandle(access, true); } /// <devdoc> /// Gets a short-term handle to the process with a given access permission. /// If the handle is stored in the current process object, use it. ///Note that the handle we store in the current process object will have all the access rights we need. /// </devdoc> /// <internalonly/> [ResourceExposure()] [ResourceConsumption(, )] SafeProcessHandle GetProcessHandle(int access, bool throwIfExited) { (, "GetProcessHandle(access = 0x" + ("X8", ) + ", throwIfExited = " + throwIfExited + ")"); #if DEBUG if () { StackFrame calledFrom = new StackTrace(true).GetFrame(0); (" called from " + () + ", line " + ()); } #endif if (haveProcessHandle) { if (throwIfExited) { //Because hasProcessHandle is true, we know we have process handles // At least SYNCHRONIZE access is required when opening, so we can wait for it // zero timeout to see if the process has exited. ProcessWaitHandle waitHandle = null; try { waitHandle = new ProcessWaitHandle(m_processHandle); if ((0, false)) { if (haveProcessId) throw new InvalidOperationException((, ())); else throw new InvalidOperationException(()); } } finally { if( waitHandle != null) { (); } } } return m_processHandle; } else { EnsureState( | ); SafeProcessHandle handle = ; #if !FEATURE_PAL handle = (processId, access, throwIfExited); #else IntPtr pseudohandle = (); // Get a real handle if (! (new HandleRef(this, pseudohandle), new HandleRef(this, pseudohandle), new HandleRef(this, pseudohandle), out handle, 0, false, NativeMethods.DUPLICATE_SAME_ACCESS | NativeMethods.DUPLICATE_CLOSE_SOURCE)) { throw new Win32Exception(); } #endif // !FEATURE_PAL if (throwIfExited && (access & NativeMethods.PROCESS_QUERY_INFORMATION) != 0) { if ((handle, out exitCode) && exitCode != NativeMethods.STILL_ACTIVE) { throw new InvalidOperationException((, ())); } } return handle; } }
() Method: Instructs the <see cref =''/> component to wait for the specified number of milliseconds to cause the associated process to exit.
Let me introduce the implementation code of this method in detail:
public bool WaitForExit(int milliseconds) { SafeProcessHandle handle = null; bool exited; ProcessWaitHandle processWaitHandle = null; try { handle = GetProcessHandle(, false); if () { exited = true; } else { processWaitHandle = new ProcessWaitHandle(handle); if( (milliseconds, false)) { exited = true; signaled = true; } else { exited = false; signaled = false; } } } finally { if( processWaitHandle != null) { (); } // If we have a hard timeout, we cannot wait for the streams if( output != null && milliseconds == -1) { (); } if( error != null && milliseconds == -1) { (); } ReleaseProcessHandle(handle); } if (exited && watchForExit) { RaiseOnExited(); } return exited; }
internal ProcessWaitHandle( SafeProcessHandle processHandle): base() { SafeWaitHandle waitHandle = null; bool succeeded = ( new HandleRef(this, ()), processHandle, new HandleRef(this, ()), out waitHandle, 0, false, NativeMethods.DUPLICATE_SAME_ACCESS); if (!succeeded) { (Marshal.GetHRForLastWin32Error()); } = waitHandle; }
Properties: Gets or sets the properties of the Start method to be passed to Process. StartInfo represents a set of parameters used to start a process. When Start is called, StartInfo is used to specify the process to start. The only StartInfo member that must be set is the FileName property.
Let me introduce the implementation code of this method in detail:
[Browsable(false), DesignerSerializationVisibility(), MonitoringDescription()] public ProcessStartInfo StartInfo { get { if (startInfo == null) { startInfo = new ProcessStartInfo(this); } return startInfo; } [ResourceExposure()] set { if (value == null) { throw new ArgumentNullException("value"); } startInfo = value; } }
Properties: Gets or sets a value indicating whether to start the process in a new window.
Let me introduce the implementation code of this method in detail:
[ DefaultValue(false), MonitoringDescription(), NotifyParentProperty(true) ] public bool CreateNoWindow { get { return createNoWindow; } set { createNoWindow = value; } }
The above briefly introduces three common methods and two common properties of this class. In actual development projects, there is no need to have a comprehensive understanding of the underlying implementation of each attribute method and attribute. However, it is recommended that when learning this class, appropriately understand the method implementation of some classes, which will help us master the class well.
2. How to convert PDF files into SWF files
In the project, if you need to convert PDF files to SWF files, you can introduce the Swftools plugin in the project. The main functions of this plugin are: PDF to SWF converter. Generate one frame per page. Enables you to have fully formatted text in Flash Movie, including tables, formulas, graphics, and more. It is based on Derek B. Noonburg's xpdf PDF parser.
A brief introduction to the common parameters of this plug-in:
-h , –help
-V, –version
-o , –output Direct output to . If contains '13568621' (), then each page specifies the output swf file name
-P , –password password Use password for deciphering the pdf. Specify the password to open the pdf
-z, –zlib �
-i , –ignore
The above are several commonly used parameters. For details, please refer to the detailed list of parameters:/。
A brief introduction to the classes and plug-ins that implement this operation, and next provides a specific operation method to implement this function:
/// <summary> /// Convert PDF to SWF /// </summary> /// <param name="pdfPathParameter">Original video file address, such as /a/b/</param> /// <param name="swfPathParameter">The generated FLV file address, such as /a/b/</param> /// <param name="beginpage">Conversion start page</param> /// <param name="endpage">Conversion end page</param> /// <param name="photoQuality">Photo Quality</param> /// <returns></returns> public static bool PdfConversionSwf(string pdfPathParameter, string swfPathParameter, int beginpage, int endpage, int photoQuality) { if ((pdfPathParameter)) { throw new ArgumentNullException(pdfPathParameter); } if ((swfPathParameter)) { throw new ArgumentNullException(swfPathParameter); } if (endpage < beginpage) { throw new ArgumentException("The number of start pages is greater than the number of end pages"); } if (photoQuality <= 0) { throw new ArgumentException("Photo quality error"); } var exe = ("~/tools/"); var pdfPath = (pdfPathParameter); var swfPath = (swfPathParameter); Process p = null; try { if (!(exe) || !(pdfPath)) { return false; } if ((swfPath)) { (swfPath); } var sb = new StringBuilder(); (" \"" + pdfPath + "\""); (" -o \"" + swfPath + "\""); (" -s flashversion=9"); (" -s disablelinks"); if (endpage > GetPageCount(pdfPath)) { endpage = GetPageCount(pdfPath); } (" -p " + "\"" + beginpage + "" + "-" + endpage + "\""); //SWF picture quality (" -j " + photoQuality); var command = (); //Process provides access to local and remote processes, enabling the start and stop of system processes. p = new Process { StartInfo = { FileName = exe, Arguments = command, WorkingDirectory = ("~/Bin/"), UseShellExecute = false, RedirectStandardError = true, CreateNoWindow = false } }; //Start the thread (); //Start asynchronous reading (); //Waiting for completion (); //Start synchronous reading //(); if (!(swfPath)) return false; return true; } catch (IOException ioex) { throw new IOException(); } catch (Exception ex) { throw new Exception(); } finally { if (p != null) { //Close the process (); //Release resources (); } } }
3. Summary
In this article, we introduce how to operate external programs and threads in C#, and introduce the underlying implementation code of some common methods of this class. If you need to have a detailed understanding of this class, you can carefully learn based on the relevant annotations and articles of the underlying source code of MSDN and .NET. While introducing the classes that implement operations, the Swftools plug-in was also explained and listed the relevant parameters. If there are high requirements in the project, you can refactor them according to the official API documentation.
In project development, no function can complete all functions. When encoding functions, you can only consider the universality of the method as much as possible. After understanding the basic principles and usage methods of a certain class and a certain plug-in, you can add new functions according to the corresponding API.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.