2016-03-07 45 views
3

我正在使用pdfkit節點模塊來生成pdf。我的問題是我想要在虛線上插入文本。這是我在做什麼:如何在node.js 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); 

但它僅返回虛線下面的文字,像這樣:enter image description here

而且我想:enter image description here

我怎樣才能實現呢?

回答

5

我們可以使用.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() 

回報: enter image description here