2014-09-29 462 views
3

我试图使用PDFBox将下划线文本添加到空白pdf页面,但我一直未能在网上找到任何示例。所有关于stackoverflow的问题都指向提取带下划线的文本,但不是创建它。 PDFBox中没有实现此功能?查看PDFBox文档,似乎字体预先呈现为粗体,斜体和常规。设置PDFBox中带下划线的文本样式

PDFont font = PDType1Font.TIMES_ROMAN. 

宋体粗体表示为:

例如,Times New Roman字体定期记

PDFont font = PDType1Font.TIMES_BOLD 

斜体表示为:

PDFont font = PDType1Font.TIMES_ITALIC 

有似乎没有加下划线的选项。无论如何要强调文字,还是这不是一个功能?

+2

你不行。只需画一条线。 – 2014-09-29 18:58:38

+0

@TilmanHausherr就是这样。一旦我获得了更多的理解,我会发布我的答案。 – antihero989 2014-09-29 19:36:18

回答

1

我不知道这是否是一个更好的不管选择与否,但我跟着Tilman Hausherr画了一条与我的文字相比较的线。举例来说,我有以下几点:

public processPDF(int xOne, int yOne, int xTwo, int yTwo) 
{ 
    //create pdf and its contents for one page 
    PDDocument document = new PDDocument(); 
    File file = new File("hello.pdf"); 
    PDPage page = new PDPage(); 
    PDFont font = PDType1Font.HELVETICA_BOLD; 
    PDPageContentStream contentStream; 

    try { 
     //create content stream 
     contentStream = new PDPageContentStream(document, page); 

     //being to create our text for our page 
     contentStream.beginText(); 
     contentStream.setFont(font, largeTitle); 

     //position of text 
     contentStream.moveTextPositionByAmount(xOne, yOne, xTwo, yTwo); 
     contentStream.drawString("Hello"); 
     contentStream.endText(); 

     //begin to draw our line 
     contentStream.drawLine(xOne, yOne - .5, xTwo, yYwo - .5); 

     //close and save document 
     document.save(file); 
     document.close(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
} 

在那里我们的参数XONE,YONE,xTwo和yTwo是我们的文本的位置。该行让我们从yOne和yTwo减去.5,将它移动到文本位置下方,最终将其设置为带下划线的文本。

可能有更好的办法,但这是我走的路线。

0

尝试这样的回答:

highlight text using pdfbox when it's location in the pdf is known

使用PDAnnotationTextMarkup这种方法,它有四个值

/** 
* The types of annotation. 
*/ 
public static final String SUB_TYPE_HIGHLIGHT = "Highlight"; 
/** 
* The types of annotation. 
*/ 
public static final String SUB_TYPE_UNDERLINE = "Underline"; 
/** 
* The types of annotation. 
*/ 
public static final String SUB_TYPE_SQUIGGLY = "Squiggly"; 
/** 
* The types of annotation. 
*/ 
public static final String SUB_TYPE_STRIKEOUT = "StrikeOut"; 

希望它可以帮助

+0

本周我会尝试这种方法,因为我正在处理其他项目。但感谢您的意见!试用后我会接受这个答案。 – antihero989 2014-10-13 16:34:51

+0

看看这个四分点http://stackoverflow.com/questions/18781923/quadbounds-order-for-pdfannotation-markup-using-itext-or-pdfbox – serge 2014-10-13 22:00:41