PDFBOX使用粗体和普通文本的相同流 [英] PDFBOX Same Stream with bold and normal text

查看:892
本文介绍了PDFBOX使用粗体和普通文本的相同流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用PDFBox我还是完全不理解它,但是我已经阅读了文档,使用字体和其他一些地方,但是我发现如何从PDF和它的风格,但我创建它,而不是阅读。



我正在做的东西

我一直在使用流:



不知道这是否是所有帮助我的代码,因为我刚刚加入了这个项目,但是当我加入时,它已经开始了。
$ b $如果你能帮我实施这个代码,或者帮助我提供一个想法或者来源,我可以阅读这个解决方案,那么我会非常感谢你。



我离开我的代码在这里:

pre $ public void rCovernoteFooter(PDPageContentStream stream,float x,float y)throws IOException {
y = y -20;
String currentDate = df1.format(new Date());
String [] issues = constants.issued()。split(/);
String issued = issues [0] + currentDate + issues [1];
y = rText(stream,x,y,90,0,10,issued,null);
stream.setFont(PDType1Font.TIMES_ROMAN,8);
y = rText(stream,x,y - 50,117,0,10,constants.terms(),null);


public float rText(PDPageContentStream cStream,float x,float y,int col1,int col2,int spc,String data1,String data2)throws IOException {
float y1 = 0f;
float y2 = 0f;
if(data2 == null){
y = iterate(data1,col1,x,y,spc,cStream);
} else {
y1 = iterate(data1,col1,x,y,spc,cStream);
y2 = iterate(data2,col2,x + 125,y,spc,cStream);
if(y1> = y2){
return y2;
} else {
return y1;
}
}
return y;


private float iterate(String text,int len,float x,float y,int space,PDPageContentStream stream)throws IOException {
int rowHeight = 0;
列表< String> wordList = Lists.newArrayList();
wordList = wordLines(text,len);

stream.setFont(PDType1Font.TIMES_BOLD,10);
for(String word:wordList){
stream.beginText();
stream.moveTextPositionByAmount(x,y - rowHeight);
stream.drawString(word);
stream.endText();
if(wordList.size()!= 1)
rowHeight = rowHeight + 10;

if(rowHeight> = space){
y = y - (rowHeight + 10);
} else {
y = y - space;
}
return y;

$ / code $


感谢您的建议




我正在做一些事情



(在同一行上有粗体和普通文字)。流:

创建粗体(或其他样式)文本的最好方法是使用一个明确创建的字体变体变种。如果没有这样的字体可用,但可以人为地模拟这些样式:


  • 人为粗体:使用文本渲染模式2只填满了字母区域,但也沿着轮廓勾画了一条线:人造边框:使用文本渲染模式1沿着字母的轮廓绘制一条线,而不是填充它

  • 人为地倾斜(实际上是倾斜的:改变文本矩阵来扭曲输出)。


    在代码中:

  • p>

      PDRectangle rec = new PDRectangle(220,120); 
    PDDocument document = null;
    document = new PDDocument ();

    PDPage page = new PDPage(rec);
    document.addPage(page);

    PDPageContentStream content = new PDPageContentStream(document,page,true ,true);

    content.beginText();
    content.moveTextPositionByAmount(7,105);
    content.setFont(PDType1Font.HELVETICA,12);
    content.drawString(普通文本和);
    content.setFon t(PDType1Font.HELVETICA_BOLD,12);
    content.drawString(bold text);
    content.moveTextPositionByAmount(0,-25);
    content.setFont(PDType1Font.HELVETICA_OBLIQUE,12);
    content.drawString(Italic text and);
    content.setFont(PDType1Font.HELVETICA_BOLD_OBLIQUE,12);
    content.drawString(bold italic text);
    content.endText();

    content.setLineWidth(.5f);

    content.beginText();
    content.moveTextPositionByAmount(7,55);
    content.setFont(PDType1Font.HELVETICA,12);
    content.drawString(Normal text and);
    content.appendRawCommands(2 Tr \\\
    );
    content.drawString(人为粗体文本);
    content.appendRawCommands(0 Tr \\\
    );
    content.moveTextPositionByAmount(0,-25);
    content.appendRawCommands(1 Tr \\\
    );
    content.drawString(人工勾画的文字);
    content.appendRawCommands(0 Tr \\\
    );
    content.setTextMatrix(1,0,.2f,1,7,5);
    content.drawString(人造斜体文本和);
    content.appendRawCommands(2 Tr \\\
    );
    content.drawString(bold italic text);
    content.appendRawCommands(0 Tr \\\
    );
    content.endText();

    content.close();

    document.save(StyledTexts.pdf);
    document.close();

    结果:



    关于提供的代码



    您的代码不会创建100%有效的PDF:它设置字体

      stream.setFont(PDType1Font.TIMES_BOLD,10)

    在文本对象外

      stream.beginText(); 
    ...
    stream.endText();

    这不是有效的PDF;很多PDF阅读器都接受它。

    此外,它真的很难阅读(神秘的名字,奇怪的架构)。

    Well I've been working with PDFBox I still don't understand it at all, but I've read the Documentation, working with fonts, and some other places, but I've found how to get the text from the PDF and it's style, but I'm creating it, not reading it.

    I am trying to make something

    Like: this (Having bold and normal text on the same line).

    I've been using streams:

    Not sure if this is all the code needed to help me, 'cause I just joined this project but it had started when I joined.

    I would thank you all if you could help me implementing this code or maybe helping me with an idea or source where I could read the solution.

    I leave my code here:

    public void rCovernoteFooter(PDPageContentStream stream , float x, float y) throws IOException {
        y = y-20;
        String currentDate = df1.format(new Date());
        String[] issues = constants.issued().split("/");
        String issued = issues[0] + currentDate + issues[1];
        y = rText(stream, x, y, 90, 0, 10, issued, null);
        stream.setFont(PDType1Font.TIMES_ROMAN, 8);
        y = rText(stream, x, y - 50, 117, 0, 10, constants.terms(), null);
    }
    
    public float rText(PDPageContentStream cStream, float x, float y, int col1, int col2, int spc, String data1, String data2) throws IOException {
        float y1 = 0f;
        float y2 = 0f;
        if (data2 == null) {
            y = iterate(data1, col1, x, y, spc, cStream);
        } else {
            y1 = iterate(data1, col1, x, y, spc, cStream);
            y2 = iterate(data2, col2, x + 125, y, spc,  cStream);
            if (y1 >= y2) {
            return y2;
            } else {
            return y1;
            }
        }
        return y;
    }
    
    private float iterate(String text, int len, float x, float y, int space, PDPageContentStream stream) throws IOException {
        int rowHeight = 0;
        List<String> wordList = Lists.newArrayList();
        wordList = wordLines(text, len);
    
        stream.setFont(PDType1Font.TIMES_BOLD, 10);
        for (String word : wordList) {
            stream.beginText();
            stream.moveTextPositionByAmount(x, y - rowHeight);
            stream.drawString(word);
            stream.endText();
            if (wordList.size() != 1)
            rowHeight = rowHeight + 10;
        }
        if (rowHeight >= space) {
            y = y - (rowHeight + 10);
        } else {
            y = y - space;
            }
        return y;
    }
    

    Thanks in advice

    解决方案

    Concerning your question

    I am trying to make something

    Like: this (Having bold and normal text on the same line).

    I've been using streams:

    The preferable way for creating bold (or otherwise styled) text is to use a font variant which has been created explicitly for that variant. If no such font is available, though, it is possible to artificially emulate those styles:

    • artificially bold: use text rendering mode 2 to not only fill the letter area but also draw a line along its outline
    • artificially outlined: use text rendering mode 1 to draw a line along the outline of the letter instead of filling it
    • artificially italic (actually slanted: change the text matrix to skew the output.

    In code:

    PDRectangle rec = new PDRectangle(220, 120);
    PDDocument document = null;
    document = new PDDocument();
    
    PDPage page = new PDPage(rec);
    document.addPage(page);
    
    PDPageContentStream content = new PDPageContentStream(document, page, true, true);
    
    content.beginText();
    content.moveTextPositionByAmount(7, 105);
    content.setFont(PDType1Font.HELVETICA, 12);
    content.drawString("Normal text and ");
    content.setFont(PDType1Font.HELVETICA_BOLD, 12);
    content.drawString("bold text");
    content.moveTextPositionByAmount(0, -25);
    content.setFont(PDType1Font.HELVETICA_OBLIQUE, 12);
    content.drawString("Italic text and ");
    content.setFont(PDType1Font.HELVETICA_BOLD_OBLIQUE, 12);
    content.drawString("bold italic text");
    content.endText();
    
    content.setLineWidth(.5f);
    
    content.beginText();
    content.moveTextPositionByAmount(7, 55);
    content.setFont(PDType1Font.HELVETICA, 12);
    content.drawString("Normal text and ");
    content.appendRawCommands("2 Tr\n");
    content.drawString("artificially bold text");
    content.appendRawCommands("0 Tr\n");
    content.moveTextPositionByAmount(0, -25);
    content.appendRawCommands("1 Tr\n");
    content.drawString("Artificially outlined text");
    content.appendRawCommands("0 Tr\n");
    content.setTextMatrix(1, 0, .2f, 1, 7, 5);
    content.drawString("Artificially italic text and ");
    content.appendRawCommands("2 Tr\n");
    content.drawString("bold italic text");
    content.appendRawCommands("0 Tr\n");
    content.endText();
    
    content.close();
    
    document.save("StyledTexts.pdf");
    document.close();
    

    The result:

    Concerning the provided code

    The code you have does not create 100% valid PDF: It sets the font

    stream.setFont(PDType1Font.TIMES_BOLD, 10)
    

    outside a text object

    stream.beginText();
    ...
    stream.endText();
    

    which is not valid PDF; many PDF viewers accept it, though.

    Furthermore it is really hard to read (cryptic names, weird architecture).

    这篇关于PDFBOX使用粗体和普通文本的相同流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆