如何使用PDFBox进行文本大纲?

发布于 2025-01-24 22:39:10 字数 2829 浏览 3 评论 0原文

我的目标是能够使用PDFBox管理文本大纲。

我一直在尝试用黑色轮廓绘制灰色文字。

像这样:

“

我一直在寻找几天,我似乎找不到解决方案。

我尝试使用填充和中风功能而没有任何成功。

有人知道如何与PDFBox所寻找的结果吗?

在这里,一个简单的代码来说明我要做什么。

在这种情况下,我将非抚摸颜色(填充)设置为灰色,并将抚摸颜色(轮廓)设置为黑色。

但是,当我尝试为文本或中风命令的轮廓指定线宽时,我会收到错误消息,说不允许文本。

我真的很感谢任何帮助。

package text_outline;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

/**
 *
 * @author User
 */
public class Text_Outline {

    public static void main(String[] args) {

    // Create PDF Document
    PDDocument MAIN_DOC = new PDDocument();
    
    try {
        // Create page
        PDPage PAGE = new PDPage();
        
        // Create PDPageContentStream for the page
        PDPageContentStream contentStream = new PDPageContentStream(MAIN_DOC, PAGE);
        
        // Begin text
        contentStream.beginText(); 
        contentStream.newLineAtOffset(25, 700);
        
        // Select font
        File fontFile = new File("C:\\WINDOWS\\FONTS\\ARIALBD.TTF");
        PDFont TTFFnt= PDType0Font.load(MAIN_DOC, fontFile);
        contentStream.setFont(TTFFnt, 100);
        
        // Set non stroking color to grey
        contentStream.setNonStrokingColor(179, 179, 179);
        
        // Set stroking color to black
        contentStream.setStrokingColor(Color.BLACK);
                
        // Set text content to HELLO and show
        contentStream.showText("HELLO");
        
        /* setLineWidth not allowed with texts. I get this error :
        Exception in thread "main" java.lang.IllegalStateException: Error: setLineWidth is not allowed within a text block.   */
        contentStream.setLineWidth(10);
        
        /* Stroke not allowed with texts. I get this error :
        Exception in thread "main" java.lang.IllegalStateException: Error: stroke is not allowed within a text block.        */
        contentStream.stroke();
                
        // End text
        contentStream.endText();
        // Close PDPageContentStream
        contentStream.close();
        // Add page to document
        MAIN_DOC.addPage(PAGE);
        
        //Saving the document
        MAIN_DOC.save("c:\\temp\\Text_Outline.pdf");
                        
        //Closing the document
        MAIN_DOC.close();
            
            
        } catch (IOException ex) {
            Logger.getLogger(Text_Outline.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}

My goal is to be able to manage text outlines with PDFBox.

I have been trying to draw a grey text with a black outline.

Like so :

Like so

I've been looking for a few days and I can't seem to find a solution.

I've tried playing with the fill and stroke functions without any success.

Does anyone know how to get the result I'm looking for with PDFBox ?

Here a simple code to illustrate what I'm trying to do.

In this case I set the non stroking color (fill) to grey and the stroking color (outline) to black.

However, when I try to specify a line width for the outline of the text or the stroke command, I get error messages saying it's not allowed with texts.

I would really appreciate any help.

package text_outline;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

/**
 *
 * @author User
 */
public class Text_Outline {

    public static void main(String[] args) {

    // Create PDF Document
    PDDocument MAIN_DOC = new PDDocument();
    
    try {
        // Create page
        PDPage PAGE = new PDPage();
        
        // Create PDPageContentStream for the page
        PDPageContentStream contentStream = new PDPageContentStream(MAIN_DOC, PAGE);
        
        // Begin text
        contentStream.beginText(); 
        contentStream.newLineAtOffset(25, 700);
        
        // Select font
        File fontFile = new File("C:\\WINDOWS\\FONTS\\ARIALBD.TTF");
        PDFont TTFFnt= PDType0Font.load(MAIN_DOC, fontFile);
        contentStream.setFont(TTFFnt, 100);
        
        // Set non stroking color to grey
        contentStream.setNonStrokingColor(179, 179, 179);
        
        // Set stroking color to black
        contentStream.setStrokingColor(Color.BLACK);
                
        // Set text content to HELLO and show
        contentStream.showText("HELLO");
        
        /* setLineWidth not allowed with texts. I get this error :
        Exception in thread "main" java.lang.IllegalStateException: Error: setLineWidth is not allowed within a text block.   */
        contentStream.setLineWidth(10);
        
        /* Stroke not allowed with texts. I get this error :
        Exception in thread "main" java.lang.IllegalStateException: Error: stroke is not allowed within a text block.        */
        contentStream.stroke();
                
        // End text
        contentStream.endText();
        // Close PDPageContentStream
        contentStream.close();
        // Add page to document
        MAIN_DOC.addPage(PAGE);
        
        //Saving the document
        MAIN_DOC.save("c:\\temp\\Text_Outline.pdf");
                        
        //Closing the document
        MAIN_DOC.close();
            
            
        } catch (IOException ex) {
            Logger.getLogger(Text_Outline.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情换悲伤 2025-01-31 22:39:10

默认情况下,文本以渲染模式为“填充”。您需要“填充&am​​p; stroke”,因此必须添加

contentStream.setRenderingMode(RenderingMode.FILL_STROKE);

到文本对象中。

By default text is drawn in rendering mode "fill". You want "fill & stroke", so you have to add

contentStream.setRenderingMode(RenderingMode.FILL_STROKE);

to your text object.

淡淡の花香 2025-01-31 22:39:10

如果有人需要,这是使用PDFBox的文本大纲的工作示例。

package text_outline;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;

public class Text_Outline {

    public static void main(String[] args) {

    // Create PDF Document
    PDDocument MAIN_DOC = new PDDocument();
    
    try {
        // Create page
        PDPage PAGE = new PDPage();
        
        // Create PDPageContentStream for the page
        PDPageContentStream contentStream = new PDPageContentStream(MAIN_DOC, PAGE);
        
        // Set contentStream Rendering Mode to Fill & Stroke
        contentStream.setRenderingMode(RenderingMode.FILL_STROKE);
        
        // Set outline width (Must be before beginText)
        contentStream.setLineWidth(3);
        
        // Set non stroking color to grey
        contentStream.setNonStrokingColor(179, 179, 179);
        
        // Set stroking color to black
        contentStream.setStrokingColor(Color.BLACK);

        // Begin text
        contentStream.beginText(); 
        
        // Set text position
        contentStream.newLineAtOffset(25, 700);
        
        // Select font
        File fontFile = new File("C:\\WINDOWS\\FONTS\\ARIALBD.TTF");
        PDFont TTFFnt= PDType0Font.load(MAIN_DOC, fontFile);
        contentStream.setFont(TTFFnt, 100);
                               
        // Set text content to HELLO and show
        contentStream.showText("HELLO");
                        
        // End text
        contentStream.endText();
        
        // Close PDPageContentStream
        contentStream.close();
        
        // Add page to document
        MAIN_DOC.addPage(PAGE);
        
        //Saving the document
        MAIN_DOC.save("c:\\temp\\Text_Outline.pdf");
                        
        //Closing the document
        MAIN_DOC.close();
            
            
        } catch (IOException ex) {
            Logger.getLogger(Text_Outline.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }
    
}

为此:

“在此处输入图像描述”

If anyone needs it, here is a working example for text outlines with PDFBox.

package text_outline;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;

public class Text_Outline {

    public static void main(String[] args) {

    // Create PDF Document
    PDDocument MAIN_DOC = new PDDocument();
    
    try {
        // Create page
        PDPage PAGE = new PDPage();
        
        // Create PDPageContentStream for the page
        PDPageContentStream contentStream = new PDPageContentStream(MAIN_DOC, PAGE);
        
        // Set contentStream Rendering Mode to Fill & Stroke
        contentStream.setRenderingMode(RenderingMode.FILL_STROKE);
        
        // Set outline width (Must be before beginText)
        contentStream.setLineWidth(3);
        
        // Set non stroking color to grey
        contentStream.setNonStrokingColor(179, 179, 179);
        
        // Set stroking color to black
        contentStream.setStrokingColor(Color.BLACK);

        // Begin text
        contentStream.beginText(); 
        
        // Set text position
        contentStream.newLineAtOffset(25, 700);
        
        // Select font
        File fontFile = new File("C:\\WINDOWS\\FONTS\\ARIALBD.TTF");
        PDFont TTFFnt= PDType0Font.load(MAIN_DOC, fontFile);
        contentStream.setFont(TTFFnt, 100);
                               
        // Set text content to HELLO and show
        contentStream.showText("HELLO");
                        
        // End text
        contentStream.endText();
        
        // Close PDPageContentStream
        contentStream.close();
        
        // Add page to document
        MAIN_DOC.addPage(PAGE);
        
        //Saving the document
        MAIN_DOC.save("c:\\temp\\Text_Outline.pdf");
                        
        //Closing the document
        MAIN_DOC.close();
            
            
        } catch (IOException ex) {
            Logger.getLogger(Text_Outline.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }
    
}

For this result :

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文