oomoster 2021-06-20 18:16 采纳率: 100%
浏览 115
已采纳

JavaScript 在PDF内提取的换行文本,输出的时候如果转为不换行?

    大佬们好,咨询一个JavaScript 在Acrobat PDF 内取值的问题,利用正则表达式(/(SN[\:\=\s][ ]*(\w+)(\d{6,10})(\s)(\n)(\w+)(\d{6,10}))/g)取以下2个数据,如何输出的时候按 SN: 002JM034523/FX13231413 这种中间加"/"的方式显示,而不是和原数据那样换行?

SN: 002JM034523
FX13231413
 

完整的代码如下:

// This is a combination of strict and relaxed versions of ISBN number format
var reISBN=/(SN[\:\=\s][ ]*(\w+)(\d{6,10})(\s)(\n)(\w+)(\d{6,10}))/g;

var strExt = "_Ex_SN2.pdf";
var strIntro = "SN numbers extracted from document: ";
var strFinal = "Total number of SN numbers extracted: " ;

ExtractFromDocument(reISBN,strExt,strIntro,strFinal);

function ExtractFromDocument(reMatch, strFileExt, strMessage1, strMessage2)
{
var chWord, numWords;

// construct filename for output document
var filename = this.path.replace(/\.pdf$/, strFileExt);

// create a report document
try {
    var ReportDoc = new Report();
    var Out = new Object(); // array where we will collect all our emails before outputing them
    
    ReportDoc.writeText(strMessage1 + this.path);
    ReportDoc.divide(1);      // draw a horizontal divider
    ReportDoc.writeText(" "); // write a blank line to output
    
    for (var i = 0; i < this.numPages; i++)
    {
        numWords = this.getPageNumWords(i);
        var PageText = "";
        for (var j = 0; j < numWords; j++) {
            var word = this.getPageNthWord(i,j,false);
            PageText += word;
            }
    
        var strMatches = PageText.match(reMatch);
        if (strMatches == null) continue;
        // now output matches into report document
        for (j = 0; j < strMatches.length; j++) {
            Out[strMatches[j]] = true; // store email as a property name
            }
    }
    
    var nTotal = 0;
    for (var prop in Out) 
    {
        ReportDoc.writeText(prop);
        nTotal++;
    }
    
    ReportDoc.writeText(" "); // output extra blank line
    ReportDoc.divide(1); // draw a horizontal divider
    ReportDoc.writeText(strMessage2 + nTotal);
    
    // save report to a document
    ReportDoc.save(
        {
        cDIPath: filename
        });

}
catch(e)
{
app.alert("Processing error: "+e)
}
    
} // end of the function
  • 写回答

2条回答 默认 最新

  • 关注

            ReportDoc.writeText(prop.replace(/\n/g,"/"));

    换行替换成 / 啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的