Actionscript 3到printjob的边距

时间:2015-02-24 11:31:26

标签: actionscript-3 flash printing

我正在处理我需要打印到收据打印机的内容。 我想在收据上显示两个边距,所以我可以在左侧显示名称,在右侧显示价格。 像这样:

Cola       2$
Pepsi      2$
Fanta      2$

我有两个文本字段,一个用于价格,另一个用于名称。

我可以这样做吗?

Liste.text = liste.text + result.name.format.leftMargin + result.name.format.rightMargin + "\n";

我无法弄清楚如何做到这一点......

以下是代码:

var pj:PrintJob = new PrintJob();
        var started:Boolean = pj.start2(null, false);

    if(started){
        var s:Sprite = new Sprite(); 
        var top:TextField = new TextField(); 
        top.autoSize = TextFieldAutoSize.CENTER;
        var list:TextField = new TextField();

        top.text ="Receipt"; 
        list.text ="\n\n" + liste.text;

        s.addChild(top);
        s.addChild(list);

        pj.addPage(s);
        pj.send();
        };
        pj = null;

2 个答案:

答案 0 :(得分:0)

为新行创建带有两个TextField(名称,价格)的Sprite,并为每个etries添加到s(List myObj {name,price})

function createLine(name: String, price: String): Sprite
{
    var name: TextField = new TextField();
    var price: TextField = new TextField();

    //your config for TextFields - italic, bold, font size, face etc;

    name.text = value;
    price.text = value;
    price.x = 80; //for example;


    var line: Sprite = new Sprite();
    line.addChild(name);
    line.addChild(price);

    return line;        
}


//...
if(started){
    var s:Sprite = new Sprite(); 
    var top:TextField = new TextField(); 
    top.autoSize = TextFieldAutoSize.CENTER;


    s.addChild(top);

    var y: Number = 0;
    foreach (var obj: Tobj in myObj)
    {
       var line: Sprite = createLine(obj.name, obj.price);
       line.y = y;
       y+=line.height;    //or other method           
       s.addChild(line);
    }        

    pj.addPage(s);
    pj.send();
    };

此解决方案可以配置分页(检查y),您可以修改收据的每个字段。

答案 1 :(得分:0)

正如@Michal说的那样,你只需要在一个新行中添加每个产品的名称和价格,并在那里使用他们的位置设置“边距”。

举个例子:

var products:Array = [
    ['product1', '2$'],
    ['product2', '3$'],
    ['product3_product3_product3', '4$']
]

// margin between the first line and the top of your page
var top_margin:int = 25;        

// max width of a product name
var name_max_width:int = 100;   

// height of a product line
var line_height:int = 20;   

// margin between product name and its price
var name_price_margin:int = 10; 

var page:Sprite = new Sprite();
var txt_top:TextField = new TextField();
    txt_top.autoSize = TextFieldAutoSize.CENTER;
    txt_top.text = 'Receipt';

page.addChild(txt_top);

for(var i:int = 0; i < products.length; i++){

    // get the product
    var product:Array = products[i];

    // create a new sprite for every product
    var product_line:Sprite = new Sprite();
        product_line.y = top_margin + i * line_height;      

    // product name text field
    var txt_name:TextField = new TextField();
        txt_name.text = product[0];
        txt_name.width = name_max_width;        

    // product price text field
    var txt_price:TextField = new TextField();
        txt_price.text = product[1];
        txt_price.x = txt_name.width + name_price_margin;

    product_line.addChild(txt_name);
    product_line.addChild(txt_price);
    page.addChild(product_line);

}

var print_job:PrintJob = new PrintJob();
if (print_job.start()) {
    print_job.addPage(page);
    print_job.send();
}
print_job = null;

这段代码会给你这样的东西(我使用的是pdf打印机):

enter image description here

当然,这只是一个向您展示如何做您正在寻找的方式的示例,您必须根据您的具体需求进行改进和调整。

希望可以提供帮助。