简体   繁体   English

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

[英]Setting font to paragraph in pdf using iText java

I was trying to create pdf using iText in java.我试图在 java 中使用 iText 创建 pdf。 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?谁能帮我找到解决方案?

//use this code.Sometimes setfont() willnot work with Paragraph //使用此代码。有时 setfont() 不适用于段落

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);
    }
}

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.你的问题在于你的 for 循环。 To the paragraph, you're adding a Element objects.在段落中,您要添加一个 Element 对象。 An Element is composed of Chunk objects, which each have their own Font data. Element 由 Chunk 对象组成,每个对象都有自己的 Font 数据。

Try setting the Font of the Chunks in your Elements when they are instantiated.在实例化元素时尝试在元素中设置块的字体。 That should solve your problem.那应该可以解决您的问题。

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));
        }

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.要将Font添加到itextpdf Paragraph ,您可以简单地使用Chunk ,然后您可以将Font设置为Chunk ChunkParagraph

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

I'm new in this.我是这方面的新手。
but i'm showing ua 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..快乐的编码..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM