使用 iText java 将字体设置为 pdf 中的段落

发布于 2024-11-03 16:51:36 字数 545 浏览 4 评论 0原文

我试图在java中使用iText创建pdf。当我尝试将字体设置为段落时失败。确切的问题只是字体大小没有得到应用。我使用了以下代码。

StringReader strReader = new StringReader(content);
arrList = HTMLWorker.parseToList(strReader, null);

Font font = new Font(BaseFont.createFont("c:\\ARIALUN0.ttf", BaseFont.IDENTITY_H, 
    BaseFont.EMBEDDED), 6, Font.BOLD, new Color(0, 0, 0));

Paragraph para = new Paragraph();  
para.setFont(font);
for (int k = 0; k < arrList.size(); ++k) {                   
    para.add((com.lowagie.text.Element)arrList.get(k)); 
}

谁能帮我找到解决方案?

I was trying to create pdf using iText in java. And am failed when I tried to set font to paragraph. The exact problem is only the font size is not getting applied. I used the following code.

StringReader strReader = new StringReader(content);
arrList = HTMLWorker.parseToList(strReader, null);

Font font = new Font(BaseFont.createFont("c:\\ARIALUN0.ttf", BaseFont.IDENTITY_H, 
    BaseFont.EMBEDDED), 6, Font.BOLD, new Color(0, 0, 0));

Paragraph para = new Paragraph();  
para.setFont(font);
for (int k = 0; k < arrList.size(); ++k) {                   
    para.add((com.lowagie.text.Element)arrList.get(k)); 
}

Can anyone help me to find a solution?

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

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

发布评论

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

评论(5

愛上了 2024-11-10 16:51:36

//使用此代码。有时 setfont() 不适用于 Paragraph

try
{

    FileOutputStream out=new FileOutputStream(name);
    Document doc=new Document();
    PdfWriter.getInstance(doc, out);
    doc.open();

    Font f=new Font(FontFamily.TIMES_ROMAN,50.0f,Font.UNDERLINE,BaseColor.RED);
    Paragraph p=new Paragraph("New PdF",f);

    p.setAlignment(Paragraph.ALIGN_CENTER);

    doc.add(p);
    doc.close();
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}

//use this code.Sometimes setfont() willnot work with Paragraph

try
{

    FileOutputStream out=new FileOutputStream(name);
    Document doc=new Document();
    PdfWriter.getInstance(doc, out);
    doc.open();

    Font f=new Font(FontFamily.TIMES_ROMAN,50.0f,Font.UNDERLINE,BaseColor.RED);
    Paragraph p=new Paragraph("New PdF",f);

    p.setAlignment(Paragraph.ALIGN_CENTER);

    doc.add(p);
    doc.close();
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}
陌上芳菲 2024-11-10 16:51:36

我很困惑,几乎发布了错误的答案。

您的段落的字体设置正确。只需尝试插入一个字符串即可查看。

你的问题出在你的for循环上。您将向该段落添加 Element 对象。 Element 由 Chunk 对象组成,每个 Chunk 对象都有自己的 Font 数据。

尝试在实例化元素时设置元素中块的字体。那应该可以解决你的问题。

I was pretty confused and almost posted the wrong answer to this.

Your paragraph is having its font set correctly. Just try inserting a String to see.

Your problem lies in your for loop. To the paragraph, you're adding a Element objects. An Element is composed of Chunk objects, which each have their own Font data.

Try setting the Font of the Chunks in your Elements when they are instantiated. That should solve your problem.

我三岁 2024-11-10 16:51:36

试试这个,它将保存文本的样式:

 Paragraph _p = new Paragraph();

         _p.setFont(regular);

        ArrayList htmlObjs = (ArrayList) HTMLWorker.parseToList(new StringReader(text_), null);

        for (int k = 0; k < htmlObjs.size(); ++k)
        {
            ArrayList<Chunk> chunk = ((Paragraph) htmlObjs.get(k)).getChunks();
            for (int l = 0; l < chunk.size(); l++)
            {
                Font _original_chunk_font = chunk.get(l).getFont();
                Font _newchunk_font = new Font(unicode);

                _newchunk_font.setFamily(_original_chunk_font.getFamilyname());
                _newchunk_font.setStyle(_original_chunk_font.getStyle());
                _newchunk_font.setSize(_original_chunk_font.getSize());
                _newchunk_font.setColor(_original_chunk_font.getColor());

                chunk.get(l).setFont(_newchunk_font);
            }
            _p.add((Element)htmlObjs.get(k));
        }

Try this ,It will save the style of the text:

 Paragraph _p = new Paragraph();

         _p.setFont(regular);

        ArrayList htmlObjs = (ArrayList) HTMLWorker.parseToList(new StringReader(text_), null);

        for (int k = 0; k < htmlObjs.size(); ++k)
        {
            ArrayList<Chunk> chunk = ((Paragraph) htmlObjs.get(k)).getChunks();
            for (int l = 0; l < chunk.size(); l++)
            {
                Font _original_chunk_font = chunk.get(l).getFont();
                Font _newchunk_font = new Font(unicode);

                _newchunk_font.setFamily(_original_chunk_font.getFamilyname());
                _newchunk_font.setStyle(_original_chunk_font.getStyle());
                _newchunk_font.setSize(_original_chunk_font.getSize());
                _newchunk_font.setColor(_original_chunk_font.getColor());

                chunk.get(l).setFont(_newchunk_font);
            }
            _p.add((Element)htmlObjs.get(k));
        }
鼻尖触碰 2024-11-10 16:51:36

我是新手。
但我正在展示 ua 设置字体的最简单方法
段落。

document.open();
document.add(new Paragraph(" Hello World ", FontFactory.getFont(FontFactory.TIMES_ROMAN,18, Font.BOLD, BaseColor.BLACK)));
document.close();

通过这种方式,我可以更改字体、字体大小、颜色等。

这对我有用..

快乐编码..

I'm new in this.
but i'm showing u a simplest way to set font to
paragraph.

document.open();
document.add(new Paragraph(" Hello World ", FontFactory.getFont(FontFactory.TIMES_ROMAN,18, Font.BOLD, BaseColor.BLACK)));
document.close();

By this way i'm changing the font, font size , color etc.

this worked for me..

Happy coding..

哭了丶谁疼 2024-11-10 16:51:36

要将Font添加到itextpdf Paragraph,您只需使用Chunk,然后您就可以设置Font 到 Chunk 然后将该 Chunk 添加到 Paragraph 中。

示例:

Font f3 = new Font(Font.FontFamily.TIMES_ROMAN, 18.0f, Font.BOLD, BaseColor.BLACK);
Chunk c3 = new Chunk("INVOICE", f3);
c3.setBackground(BaseColor.WHITE);       
Paragraph p3 = new Paragraph(c3);
p3.setAlignment(Element.ALIGN_CENTER);

For adding Font to itextpdf Paragraph you can simply use a Chunk then you can set Font to Chunk add that Chunk to Paragraph afterwards.

Example:

Font f3 = new Font(Font.FontFamily.TIMES_ROMAN, 18.0f, Font.BOLD, BaseColor.BLACK);
Chunk c3 = new Chunk("INVOICE", f3);
c3.setBackground(BaseColor.WHITE);       
Paragraph p3 = new Paragraph(c3);
p3.setAlignment(Element.ALIGN_CENTER);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文