Changing print margins on JTextPane - java

I have come across various solutions to changing the margins when printing in Java but none seem to work. Here and Here.
What I have so far is,
TextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
// Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);
Style style = doc.addStyle("StyleName", null);
StyleConstants.setIcon(style, new ImageIcon(qrcode));
doc.insertString(0, "Title Here\n", null );
doc.insertString(doc.getLength(), "Ignored", style);
textPane.print();
When using the inbuilt print method the margins are set to default as 25.4mm. I'd like to be able to edit these margins while still being able to have a print dialog.

What I "can" verify is, something like this will effect the page size and margins of the output
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
// Define a keyword attribute
SimpleAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);
Style style = doc.addStyle("StyleName", null);
//StyleConstants.setIcon(style, new ImageIcon(qrcode));
doc.insertString(0, "Title Here\n", null);
doc.insertString(doc.getLength(), "Ignored", style);
Paper paper = new Paper();
paper.setSize(fromCMToPPI(21.0), fromCMToPPI(29.7)); // A4
paper.setImageableArea(fromCMToPPI(5.0), fromCMToPPI(5.0),
fromCMToPPI(21.0) - fromCMToPPI(10.0), fromCMToPPI(29.7) - fromCMToPPI(10.0));
PageFormat pageFormat = new PageFormat();
pageFormat.setPaper(paper);
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(textPane.getPrintable(null, null), pageFormat);
PageFormat pf = pj.pageDialog(pageFormat);
if (pj.printDialog()) {
pj.print();
}
Normal output vs modified output (border added after to highlight change)
And the conversation methods...
protected static double fromCMToPPI(double cm) {
return toPPI(cm * 0.393700787);
}
protected static double toPPI(double inch) {
return inch * 72d;
}
What I can't verify is if those values appear in either page setup or printer dialogs, as MacOS has decided I don't need to see them :/
From previous experience on Windows, I seem to remember it working

Related

alignment using StyledDocument in drag and drop java

I tired from search the solution for this: i want to align text pane from left to right however but drag and drop java this is last code i written :
` StyledDocument doc = txtio.getStyledDocument();
Style style = txtio.addStyle("right",null);
StyleConstants . setAlignment (style, StyleConstants .ALIGN_RIGHT);
try {
doc.insertString(0,txtio.getSelectedText(), style);
}
catch (BadLocationException ex) {
Logger . getLogger ( mswordframe.class.getName() ).log(Level.SEVERE,null, ex);
}
txtio.setStyledDocument(doc);`
txtio : is the name of text pane;
it doesn't work ,
Sorry i am weak in english
JTextPane supports character and paragraph attributes. Character attributes are for pieces of text and paragraph attributes are for a whole line of text.
Alignment of text is a paragraph attribute because you can't have part of the text center aligned and part right aligned for the same line of text.
Try the following:
SimpleAttributeSet green = new SimpleAttributeSet();
StyleConstants.setForeground(green, Color.GREEN);
SimpleAttributeSet right = new SimpleAttributeSet();
StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
try
{
doc.insertString(0, txtio.getSelectedText(), green);
doc.setParagraphAttributes(0, 1, right, false);
}
catch(Exception e) {}

How to set different JTextArea text alignment per line?

I have a JTextArea in which i want to display messages aligned to right or left, depending on a variable I pass along with the message. Can I do that?
What did I do wrong? Nothing gers added to the text pane when i run my GUI
battleLog = new JTextPane();
StyledDocument bL = battleLog.getStyledDocument();
SimpleAttributeSet r = new SimpleAttributeSet();
StyleConstants.setAlignment(r, StyleConstants.ALIGN_RIGHT);
try {
bL.insertString(bL.getLength(), "test", r);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
Not with a JTextArea.
You need to use a JTextPane which supports different text and paragraph attributes. An example to CENTER the text:
JTextPane textPane = new JTextPane();
textPane.setText("Line1");
StyledDocument doc = textPane.getStyledDocument();
// Define the attribute you want for the line of text
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
// Add some text to the end of the Document
try
{
int length = doc.getLength();
doc.insertString(doc.getLength(), "\ntest", null);
doc.setParagraphAttributes(length+1, 1, center, false);
}
catch(Exception e) { System.out.println(e);}
if("left".equals(input)){
setAlignmentX(Component.LEFT_ALIGNMENT);
}
Have a try!

how can I find default attributes for JTextPane document?

I'm working with a JTextPane.
JTextPane pane = new JTextPane();
String content = "I'm a line of text that will be displayed in the JTextPane";
StyledDocument doc = pane.getStyledDocument();
SimpleAttributeSet aSet = new SimpleAttributeSet();
If I add this aSet to the textpane's document like this:
doc.setParagraphAttributes(0, content.length(), aSet, false);
Nothing visible happens. No big surprise since I haven't set any custom attributes for aSet. However, if I allow aSet to replace the current ParagraphAttributes of doc like this:
doc.setParagraphAttributes(0, content.length(), aSet, true);
A lot of things happen. How can I get information on those default values of the JTextPane document? Particularly my problems is that when I'm defining a custom Font for aSet and set it to replace the current attributes, the font is displayed as if it was bold. StyleConstants.setBold(aSet, false); doesn't help.
I have looked at the source code to see what data structures are holding the information that you want. This is a modification of that code that prints the attributes for each paragraph.
int offset, length; //The value of the first 2 parameters in the setParagraphAttributes() call
Element section = doc.getDefaultRootElement();
int index0 = section.getElementIndex(offset);
int index1 = section.getElementIndex(offset + ((length > 0) ? length - 1 : 0));
for (int i = index0; i <= index1; i++)
{
Element paragraph = section.getElement(i);
AttributeSet attributeSet = paragraph.getAttributes();
Enumeration keys = attributeSet.getAttributeNames();
while (keys.hasMoreElements())
{
Object key = keys.nextElement();
Object attribute = attributeSet.getAttribute(key);
//System.out.println("key = " + key); //For other AttributeSet classes this line is useful because it shows the actual parameter, like "Bold"
System.out.println(attribute.getClass());
System.out.println(attribute);
}
}
The output for a simple textPane with some text added through the setText() method gives:
class javax.swing.text.StyleContext$NamedStyle
NamedStyle:default {foreground=sun.swing.PrintColorUIResource[r=51,g=51,b=51],size=12,italic=false,name=default,bold=false,FONT_ATTRIBUTE_KEY=javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=plain,size=12],family=Dialog,}
About your particular problem, looking at a related SO question I have been able to set the text of a paragraph to bold with:
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aSet = sc.addAttribute(aSet, StyleConstants.Bold, true);
In this case the class of aSet is javax.swing.text.StyleContext$SmallAttributeSet which is not mutable (does not implement MutableAttributeSet). For your case something along the lines:
aSet.addAttribute(StyleConstants.Bold, true);
should work.

Font displays in text field very small

I have this class, and I am trying to display a custom font in a text field, but but when I run it the font is super tiny like 2px tiny. If i just run font = new Font("sans-serif", Font.PLAIN, 24); it displays just fine at the right font size.
Here is what it looks like:
Here is what it looks like when I only use font = new Font("sans-serif", Font.PLAIN, 24);
What is causing the small text box with a custom font?
public class Search extends JTextField{
public Search(int width){
super(width);
Font font;
String filename = "/media/fonts/SourceCodePro-Light.ttf";
try{
InputStream is = this.getClass().getResourceAsStream(filename);
font = Font.createFont(Font.TRUETYPE_FONT, is);
font = font.deriveFont(24);
}catch(FontFormatException | IOException ex){
font = new Font("sans-serif", Font.PLAIN, 24);
}
this.setFont(font);
}
}
font.deriveFont has two overloaded forms that can be quite similar. The one taking int sets the font style, the one taking float sets the font size. YOu are invoking the int version instead of the float version. Change 24 to 24.0f, and it will work

Java add global JTextPane styles/attributes?

I want to add a global AttributeSet to my JTextPane.
I found this:
SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setLeftIndent(style, 20);
StyleConstants.setFirstLineIndent(style, -20);
From http://java-sl.com/tip_hanging_first_line.html
I'm wondering how I can set the "default stylesheet"? (not using HTML). I then tried this:
StyleContext style = new StyleContext();
Style s = style.addStyle("test", null);
StyleConstants.setForeground(s, Color.BLUE);
StyledDocument d = (StyledDocument) console.getOutputField().getDocument();
From http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample1.htm without luck.
I know StyledDocument has specific properties for setting stuff like foreground colour - which is why this may not work - but can anyone point me as to how to use the other style attributes? Such as the left indent and first line indent.
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setLeftIndent(style, 20);
StyleConstants.setFirstLineIndent(style, -20);
StyleConstants.setForeground(style, Color.BLUE);
doc.setParagraphAttributes(0, doc.getLength(), style, true);

Categories