如何使用PDFBox进行文本大纲?
我的目标是能够使用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 :
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,文本以渲染模式为“填充”。您需要“填充& stroke”,因此必须添加
到文本对象中。
By default text is drawn in rendering mode "fill". You want "fill & stroke", so you have to add
to your text object.
如果有人需要,这是使用PDFBox的文本大纲的工作示例。
为此:
If anyone needs it, here is a working example for text outlines with PDFBox.
For this result :