【问题标题】:Setting a text style to underlined in PDFBox在 PDFBox 中将文本样式设置为带下划线
【发布时间】:2014-11-24 03:53:23
【问题描述】:

我正在尝试使用 PDFBox 将带下划线的文本添加到空白 pdf 页面,但我无法在网上找到任何示例。 stackoverflow 上的所有问题都指向提取带下划线的文本,而不是创建它。 PDFBox没有实现这个功能吗?查看 PDFBox 文档,似乎字体被预渲染为粗体、斜体和常规。

例如 Times New Roman Regular 表示为:

PDFont font = PDType1Font.TIMES_ROMAN. 

Times New Roman Bold 表示为:

PDFont font = PDType1Font.TIMES_BOLD

斜体表示为:

PDFont font = PDType1Font.TIMES_ITALIC

似乎没有带下划线的选项。反正有下划线文本,还是这不是一个功能?

【问题讨论】:

  • 你不能。画一条线。
  • @TilmanHausherr 就是这么做的。一旦我获得更多理解,我会发布我的答案。

标签: java pdf-generation pdfbox


【解决方案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 中减去 0.5 以将其移动到我们的文本位置下方,最终将其设置为看起来像带下划线的文本。

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

【讨论】:

    【解决方案2】:

    我使用下面的函数来为字符串加下划线。

    public class UnderlineText {
    PDFont font = PDType1Font.HELVETICA_BOLD;
    float fontSize = 10f;
    String str = "Hello";
    public static void main(String[] args) {
        new UnderlineText().generatePDF(20, 200);
    }
    
    public void generatePDF(int sX, int sY)
    {
        //create pdf and its contents for one page
        PDDocument document = new PDDocument();
        File file = new File("underlinePdfbox.pdf");
        PDPage page = new PDPage();
        PDPageContentStream contentStream;
    
        try {
            document.addPage(page);
    
            //create content stream
            contentStream = new PDPageContentStream(document, page);
    
            //being text for our page
            contentStream.beginText();
            contentStream.setFont( font, fontSize);
            contentStream.newLineAtOffset(sX, sY);
            contentStream.showText(str);
            contentStream.endText();
    
            //Draw Underline
            drawLine(contentStream, str, 1, sX, sY, -2);
    
    
            //close and save document
            contentStream.close();
            document.save(file);
            document.close();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
    
    public void drawLine(PDPageContentStream contentStream, String text, float lineWidth, float sx, float sy, float linePosition) throws IOException {
         //Calculate String width
        float stringWidth = fontSize * font.getStringWidth(str) / 1000;
        float lineEndPoint = sx + stringWidth;
    
        //begin to draw our line
        contentStream.setLineWidth(lineWidth);
        contentStream.moveTo(sx, sy + linePosition);
        contentStream.lineTo(lineEndPoint, sy + linePosition);
        contentStream.stroke();
    }
    }
    

    drawLine 是我创建的用于为特定字符串绘制线条的函数。您可以使用位置属性根据规范调整线条。

    位置字段中的减号 (-) 值在行下创建。您可以对上划线和笔划线使用正值。(例如,-2 表示下划线,10 表示上划线,2 表示上述代码的笔划线)

    你也可以管理线条的宽度。

    【讨论】:

      【解决方案3】:

      试试这个答案:

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

      希望对你有帮助

      【讨论】:

      猜你喜欢
      • 2015-09-21
      • 2012-10-10
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多