简体   繁体   中英

How to write a text on a line in node.js pdf kit?

I am using pdfkit node module to generate a pdf. My problem is that I want to insert a text on a dashed line. This is what I am doing:

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);

But it returns text just below the dashed line, like this: 在此输入图像描述

And I want to get: 在此输入图像描述

How can I achieve it?

We can use .moveTo and split the lines into two and add the text in middle.

Try the code I've posted below, it is working for me:

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() 

returns: 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM