如何在node.js pdf工具包中的一行上写文字?

Atu*_*wal 4 pdfkit node.js

我正在使用pdfkit节点模块生成pdf.我的问题是我想在虚线上插入文本.这就是我在做的事情:

doc.moveDown(2)
        .moveTo(x+leftMargin, doc.y)
        .lineTo(doc.x, doc.y)
        .lineWidth(0.5)
        .dash(3,{space:3})
        .fillAndStroke(defBlackColor)
        .fill(defBlackColor)
        .fontSize(defFontSize)
    .text('Layover:'+' '+ obj.layover,x + leftMargin + xincr/2,doc.y);
Run Code Online (Sandbox Code Playgroud)

但它返回虚线下方的文本,如下所示:在此输入图像描述

我想得到:在此输入图像描述

我怎样才能实现它?

Yog*_*nan 6

我们可以使用.moveTo并将线分成两部分并在中间添加文本.

试试我在下面发布的代码,它对我有用:

doc.moveTo(200, 200)       // this is your starting position of the line, from the left side of the screen 200 and from top 200
   .lineTo(400, 200)       // this is the end point the line 
   .dash(5, { space: 10 }) // here we are formatting it to dash
   .text("text goes here", 410, 195) // the text and the position where the it should come
    doc.moveTo(500, 200)   //again we are giving a starting position for the text
   .lineTo(800, 200)       //end point
   .dash(5, {space: 10})   //adding dash
   .stroke() 
Run Code Online (Sandbox Code Playgroud)

收益: 在此输入图像描述