Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setFont method not working on paragraph and phrase. #897

Closed
devrj8 opened this issue Apr 19, 2023 · 4 comments · Fixed by #1092
Closed

setFont method not working on paragraph and phrase. #897

devrj8 opened this issue Apr 19, 2023 · 4 comments · Fixed by #1092
Labels

Comments

@devrj8
Copy link

devrj8 commented Apr 19, 2023

Description
setFont method not working on paragraph and phrase.

Reproduce

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ReproduceBug {

    public static void main(String[] args) throws FileNotFoundException {

        Document document = new Document();
        FileOutputStream pdfOutputFile = new FileOutputStream("./bug.pdf");
        PdfWriter pdfWriter = PdfWriter.getInstance(document, pdfOutputFile);

        document.open();

//bug code
        Paragraph para1 = new Paragraph("Hello World!");
        para1.setFont(new Font(Font.COURIER, 30));
        document.add(para1);

//workaround
        Paragraph para2 = new Paragraph("Hello World!", new Font(Font.COURIER, 30));
        document.add(para2);

        document.close();
    }
}

Expected behavior
first line "Hello World" should also display with font size 30. but it is not displaying it.

Screenshots
Output Screenshot -
image

System:

  • OS: Windows
  • Used Font: Font.COURIER
@devrj8 devrj8 added the bug label Apr 19, 2023
@mkl-public
Copy link
Contributor

The setFont method sets the font to use for plain text added to the paragraph afterwards.

Thus, instead of

        Paragraph para1 = new Paragraph("Hello World!");
        para1.setFont(new Font(Font.COURIER, 30));

do

        Paragraph para1 = new Paragraph();
        para1.setFont(new Font(Font.COURIER, 30));
        para1.add("Hello World!");

@devrj8
Copy link
Author

devrj8 commented Apr 21, 2023

Thanks for the explanation. I will try that.
I feel this is a bug cause, it is just a paragraph object and setting font is not working using setter method.
do you consider this as a bug?

@asturio
Copy link
Member

asturio commented Feb 23, 2024

I wouldn't consider this a bug.
We could add a check in setFont() if there is already some Elements in the Phrase. Or we just should update the JavaDoc.

@mkl-public
Copy link
Contributor

@devrj8

I feel this is a bug cause, it is just a paragraph object and setting font is not working using setter method.
do you consider this as a bug?

But setting font is working using setter method. You merely incorrectly assume the font here is the font used for all the paragraph content. But wouldn't that be a primitive model of a paragraph, having just content of a single font?

I have to admit, though, that the name of the property, font, is a bit imprecise and does not convey which font exactly is meant. Thus, a bit of JavaDocs documentation would be great, @asturio.

asturio added a commit that referenced this issue Mar 4, 2024
- describes that setFont() only affects new content, and don't change
  existing one
@asturio asturio linked a pull request Mar 4, 2024 that will close this issue
1 task
asturio added a commit that referenced this issue Mar 4, 2024
- describes that setFont() only affects new content, and don't change
  existing one
asturio added a commit that referenced this issue Mar 4, 2024
- describes that setFont() only affects new content, and don't change
  existing one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants