移动文件时保持RTF格式

时间:2017-07-13 15:43:37

标签: perl rtf

我已经能够对RTF文档进行标记,然后将其打印到另一个RTF文档中。我的问题是,是否可以保留第一个文档的原始格式(字体,字体颜色,背景颜色)。有些东西在文档中随机着色,因此保持格式很重要。

这里是tokenizer代码:

#!usr/bin/perl
use strict;
use warnings;
use RTF::Writer;
use Data::Dumper;
use RTF::Tokenizer;

die "usage: $0 input output\n" unless @ARGV == 2;

my $infile = shift;
my $outfile = shift;

my $tokenizer = RTF::Tokenizer->new();
$tokenizer->read_file($infile);

my ( $token_type, $argument, $parameter );

{
# reduce bogus warnings
no warnings 'uninitialized';


# get past the header
( $token_type, $argument, $parameter ) =
$tokenizer->get_token() until
($token_type eq 'control' and $argument eq 'par');
}

my @final;
while ($token_type ne 'eof'){
( $token_type, $argument, $parameter ) = $tokenizer->get_token();
push @final, $argument if $token_type eq 'text';
}

my $rtf = RTF::Writer->new_to_file($outfile);
my @sorted = sort {
my @fields_a = split / / , $a;
my @fields_b = split / /, $b;
chomp($a, $b);
$fields_a[0] cmp $fields_b[0];
} @final;

$rtf->prolog;
$rtf->print(\@sorted);
$rtf->close;

这就是我输入的内容

{\rtf1\ansi\deff0{\fonttbl{\f0 Times New Roman;}}
{\colortbl;\red255\green0\blue0;
\red0\green0\blue255;}
\cf1 145747.2545  
\cf0 134758.2545

我希望以相同的格式输出这些。我已经为它做了一个排序脚本

1 个答案:

答案 0 :(得分:1)

根据RTF::Writer的文档,RTF命令序列需要作为scalar references传递给print()方法。例如:

use strict;
use warnings;

use RTF::Writer;

my $rtf = RTF::Writer->new_to_handle(*STDOUT);

while (<DATA>) {
    $rtf->print(\$_);
}

$rtf->close;

__DATA__
{\rtf1\ansi\deff0{\fonttbl{\f0 Times New Roman;}}
{\colortbl;\red255\green0\blue0;
\red0\green0\blue255;}
\cf1 145747.2545
\cf0 134758.2545

我不熟悉RTF规范,因此我不知道这里是否需要换行。

{\rtf1\ansi\deff0{\fonttbl{\f0 Times New Roman;}}
{\colortbl;\red255\green0\blue0;
\red0\green0\blue255;}
\cf1 145747.2545
\cf0 134758.2545

如果您只是将标量传递给print()而不是标量引用,则看起来会执行一些转义:

\'7b\'5crtf1\'5cansi\'5cdeff0\'7b\'5cfonttbl\'7b\'5cf0 Times New Roman;\'7d\'7d
\line \'7b\'5ccolortbl;\'5cred255\'5cgreen0\'5cblue0;
\line \'5cred0\'5cgreen0\'5cblue255;\'7d
\line \'5ccf1 145747\'2e2545
\line \'5ccf0 134758\'2e2545
\line