ITPub博客

首页 > 大数据 > ELK > Free Spire.Doc 是一款支持 RTF、Word 等文档的格式

Free Spire.Doc 是一款支持 RTF、Word 等文档的格式

投稿 ELK 作者:ecclw459 时间:2026-01-30 19:06:14 0 删除 编辑

using Spire.Doc; namespace RtfToPdfConverter {     class Program    {         static void Main( string[] args)        {             try            {                 // 初始化Document对象                Document document = new Document();                 // 加载本地RTF文件(替换为实际文件路径)                 string rtfFilePath = @"C:\Files\test.rtf" ;                document.LoadFromFile(rtfFilePath, FileFormat.Rtf);                 // 保存为PDF文件(替换为输出路径)                 string pdfFilePath = @"C:\Files\test.pdf" ;                document.SaveToFile(pdfFilePath, FileFormat.Pdf);                 // 释放资源                document.Close();                Console.WriteLine( "RTF转PDF成功!输出路径:" + pdfFilePath);            }             catch (Exception ex)            {                Console.WriteLine( "转换失败:" + ex.Message);


using Spire.Doc; namespace BatchRtfToPdfConverter {     class Program    {         static void Main( string[] args)        {             // 源RTF文件目录、PDF输出目录(替换为实际路径)             string sourceDir = @"C:\Files\RTF_Source" ;             string outputDir = @"C:\Files\PDF_Output" ;             // 检查并创建输出目录             if (!Directory.Exists(outputDir))            {                Directory.CreateDirectory(outputDir);            }             try            {                 // 获取目录下所有RTF文件                 string [] rtfFiles = Directory.GetFiles(sourceDir, "*.rtf" );                 if (rtfFiles.Length == 0 )                {                    Console.WriteLine( "源目录下未找到RTF文件!" );                     return ;                }                 // 批量转换                 int successCount = 0 ;                 foreach ( string rtfFile in rtfFiles)                {                     try                    {                        Document document = new Document();                        document.LoadFromFile(rtfFile, FileFormat.Rtf);                         // 生成同名PDF文件                         string fileName = Path.GetFileNameWithoutExtension(rtfFile);                         string pdfFile = Path.Combine(outputDir, $" {fileName}.pdf" );                        document.SaveToFile(pdfFile, FileFormat.Pdf);                        document.Close();                        successCount++;                        Console.WriteLine( $"成功转换: {rtfFile} → {pdfFile}" );                    }                     catch (Exception ex)                    {                        Console.WriteLine( $"转换失败 {rtfFile}: {ex.Message}" );                    }                }                Console.WriteLine( $"\n批量转换完成!成功: {successCount} 个,失败: {rtfFiles.Length - successCount} 个" );            }


declare ( strict_types = 1 ) ; require __DIR__ . '/../vendor/autoload.php' ; header ( 'Content-Type: application/json; charset=utf-8' ) ; $method = $_SERVER [ 'REQUEST_METHOD' ] ?? 'GET' ; $path = parse_url ( $_SERVER [ 'REQUEST_URI' ] ?? '/' , PHP_URL_PATH ) ? : '/' ; function jsonResponse ( array $payload , int $status = 200 ) : void {     http_response_code ( $status ) ;     echo json_encode ( $payload , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) ;     exit ; } function readJsonBody ( ) : array {     $raw = file_get_contents ( 'php://input' ) ? : '' ;     $data = json_decode ( $raw , true ) ;     return is_array ( $data ) ? $data : [ ] ; } if ( $method === 'GET' && $path === '/health' ) {     jsonResponse ( [ 'ok' => true , 'time' => date ( DATE_ATOM ) ] ) ; } if ( $method === 'POST' && $path === '/users' ) {     $body = readJsonBody ( ) ;     $email = strtolower ( trim ( ( string ) ( $body [ 'email' ] ?? '' ) ) ) ;     $name   = trim ( ( string ) ( $body [ 'name' ] ?? '' ) ) ;     if ( ! filter_var ( $email , FILTER_VALIDATE_EMAIL ) ) {         jsonResponse ( [ 'error' => 'Invalid email' ] , 422 ) ;     }     if ( $name === '' ) {         jsonResponse ( [ 'error' => 'Name is required' ] , 422 ) ;     }     $id = random_int ( 1000 , 9999 ) ;     jsonResponse ( [ 'id' => $id , 'email' => $email , 'name' => $name ] , 201 ) ;



se GuzzleHttp \Exception \GuzzleException ; final class ShippingClient {     private Client $http ;     public function __construct ( string $baseUrl , string $token )     {         $this -> http = new Client ( [             'base_uri' => rtrim ( $baseUrl , '/' ) . '/' ,             'timeout' => 3.0 ,             'connect_timeout' => 1.0 ,             'headers' => [                 'Authorization' => "Bearer { $token }" ,                 'Accept' => 'application/json' ,             ] ,         ] ) ;     }     public function createLabel ( array $payload ) : array     {         $attempts = 0 ;         while ( true ) {             $attempts ++ ;             try {                 $resp = $this -> http -> post ( 'labels' , [ 'json' => $payload ] ) ;                 $data = json_decode ( ( string ) $resp -> getBody ( ) , true ) ;                 return is_array ( $data ) ? $data : [ ] ;             } catch ( GuzzleException $e ) {                 if ( $attempts >= 3 ) {                     throw new RuntimeException ( 'Shipping API failed after retries' , 0 , $e ) ;                 }                 // small exponential backoff + jitter


final class WebhookEvent {     public function __construct (         public readonly string $id ,         public readonly string $type ,         public readonly int $createdAtEpoch ,         public readonly array $data     ) { }


cLbaojie.com

    public static function fromArray ( array $payload ) : self     {         return new self (             id : ( string ) ( $payload [ 'id' ] ?? '' ) ,             type : ( string ) ( $payload [ 'type' ] ?? '' ) ,             createdAtEpoch : ( int ) ( $payload [ 'created_at' ] ?? 0 ) ,             data : is_array ( $payload [ 'data' ] ?? null ) ? $payload [ 'data' ] : [ ]

{     public function __construct ( private readonly PDO $pdo ) { }     public function hasProcessed ( string $eventId ) : bool     {         $stmt = $this -> pdo -> prepare ( "SELECT 1 FROM processed_webhooks WHERE event_id = :id" ) ;         $stmt -> execute ( [ ':id' => $eventId ] ) ;         return ( bool ) $stmt -> fetchColumn ( ) ;hntcsc.com     }     public function markProcessed ( string $eventId ) : void     {         $stmt = $this -> pdo -> prepare (             "INSERT INTO processed_webhooks (event_id, processed_at)


  public function __construct (         private readonly WebhookSignatureVerifier $verifier ,         private readonly WebhookIdempotencyStore $idem ,         private readonly PaymentService $payments ,         private readonly PDO $pdo     ) { }     public function handle ( string $rawBody , string $signatureHeader ) : void     {         if ( ! $this -> verifier -> verify ( $rawBody , $signatureHeader ) ) {             throw new RuntimeException ( 'Invalid webhook signature' ) ;         }         $payload = json_decode ( $rawBody , true ) ;         if ( ! is_array ( $payload ) ) {dlddz.com             throw new RuntimeException ( 'Invalid JSON' ) ;         }         $event = WebhookEvent :: fromArray ( $payload ) ;         if ( $event -> id === '' || $event -> type === '' ) {             throw new RuntimeException ( 'Missing event fields' ) ;         }         if ( $this -> idem -> hasProcessed ( $event -> id ) ) {             return ; // safe no-op         }         $this -> pdo -> beginTransaction ( ) ;         try {             $this -> dispatch ( $event ) ;             $this -> idem -> markProcessed ( $event -> id ) ;             $this -> pdo -> commit ( ) ;         } catch ( Throwable $e ) {             $this -> pdo -> rollBack ( ) ;sdxmky.cn             throw $e ;         }     }     private function dispatch ( WebhookEvent $event ) : void     {         $type = PaymentEventType :: tryFrom ( $event -> type ) ;         if ( $type === null ) {             // unknown event type: ignore or log             return ;         }         $paymentId = ( string ) ( $event -> data [ 'payment_id' ] ?? '' ) ;         if ( $paymentId === '' ) return ;         match ( $type ) {xxthtz.com             PaymentEventType :: PaymentSucceeded => $this -> payments -> markSucceeded ( $paymentId ) ,             PaymentEventType :: PaymentFailed     => $this -> payments -> markFailed ( $paymentId ) ,


    public readonly int $id     ) { } } final class CursorCodec {     public function __construct ( private readonly string $secret ) { }     public function encode ( Cursor $cursor ) : string     {         $json = json_encode ( [             'created_at' => $cursor -> createdAtIso ,             'id' => $cursor -> id         ] , JSON_UNESCAPED_SLASHES ) ;         $b64 = rtrim ( strtr ( base64_encode ( $json ) , '+/' , '-_' ) , '=' ) ;         $sig = hash_hmac ( 'sha256' , $b64 , $this -> secret ) ;         return $b64 . '.' . $sig ;     }     public function decode ( string $token ) : Cursor     {         $parts = explode ( '.' , $token , 2 ) ;         if ( count ( $parts ) !== 2 ) {             throw new InvalidArgumentException ( 'Invalid cursor' ) ;         }gzuof.cn         [ $b64 , $sig ] = $parts ;         $expected = hash_hmac ( 'sha256' , $b64 , $this -> secret ) ;         if ( ! hash_equals ( $expected , $sig ) ) {             throw new InvalidArgumentException ( 'Cursor signature mismatch' ) ;         }         $json = base64_decode ( strtr ( $b64 , '-_' , '+/' ) , true ) ;         if ( $json === false ) {             throw new InvalidArgumentException ( 'Invalid cursor encoding' ) ;         }         $data = json_decode ( $json , true ) ;         if ( ! is_array ( $data ) ) {             throw new InvalidArgumentException ( 'Invalid cursor payload' ) ;         }         return new Cursor (             createdAtIso : ( string ) ( $data [ 'created_at' ] ?? '' ) ,


  public function __construct ( private readonly PDO $pdo ) { }     /**     * @return array{items: list<array>, next: ?Cursor}     */     public function listPage ( ? Cursor $after , int $limit ) : array     {         $limit = max ( 1 , min ( $limit , 100 ) ) ;yqswh.cn         if ( $after === null ) {top2001.cn             $sql = "SELECT id, created_at, total_cents                    FROM orders  jnhjpj.cn                    ORDER BY created_at DESC, id DESC                    LIMIT :limit" ;yccgc.cn             $stmt = $this -> pdo -> prepare ( $sql ) ;             $stmt -> bindValue ( ':limit' , $limit , PDO :: PARAM_INT ) ;         } else {             $sql = "SELECT id, created_at, total_cents                    FROM ordersuis.zw0769.com                    WHERE created_at < :created_at                       OR (created_at = :created_at AND id < :id)                    ORDER BY created_at DESC, id DESC                    LIMIT :limit" ;             $stmt = $this -> pdo -> prepare ( $sql ) ;             $stmt -> bindValue ( ':created_at' , $after -> createdAtIso ) ;             $stmt -> bindValue ( ':id' , $after -> id , PDO :: PARAM_INT ) ;ttlyf.cn             $stmt -> bindValue ( ':limit' , $limit , PDO :: PARAM_INT ) ;         }wpw.zw0769.com         $stmt -> execute ( ) ;huadasp.cn           $rows = $stmt -> fetchAll ( PDO :: FETCH_ASSOC ) ;         $items = array_map ( fn ( $r ) => [fwn.zw0769.com             'id' => ( int ) $r [ 'id' ] ,             'created_at' => ( string ) $r [ 'created_at' ] ,             'total_cents' => ( int ) $r [ 'total_cents' ] ,         ] , $rows ) ;         $next = null ;         if ( $items !== [ ] ) {



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048957/viewspace-3109579/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2026-01-07

  • 博文量
    108
  • 访问量
    102104