Why is the JSeparator not displayed as a horizontal line? - jseparator

Here are the relevant parts of my code:
JPanel panel = new JPanel(new FlowLayout());
JLabel label1 = new JLabel("INFO");
JLabel label2 = new JLabel("TEXT");
panel.add(label1);
panel.add(new JSeparator());
panel.add(label2);
No horizontal line is displayed. Any help?

Related

How to keep the text while moving (resizing) the Stamp Annotation iText

I'm working in some Annotation stuff with iText.
I tried to draw a Stamp Annotation with text on an existing PDF file.
The Stamp Annotation did show on the PDF but when I moving the stamp (or resizing), the text (will all format) is disappear and can't gain it back anymore.
Here is my code
PdfReader reader = new PdfReader(SRC_FILE);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(DEST_FILE));
PdfWriter writer = stamper.getWriter();
BaseColor textColor = BaseColor.ORANGE;
BaseColor fillColor = BaseColor.GRAY;
Rectangle rectangle = new Rectangle(150, 250, 450, 320);
PdfAnnotation annotation = PdfAnnotation.createSquareCircle(writer, rectangle, null, true);
annotation.setColor(textColor);
PdfDictionary dict = new PdfDictionary();
PdfTemplate template = PdfTemplate.createTemplate(writer, rectangle.getWidth(), rectangle.getHeight());
template.setBoundingBox(rectangle);
template.setColorFill(fillColor);
//template.setColorStroke(textColor);
template.setLineWidth(4);
template.rectangle(rectangle.getLeft(), rectangle.getBottom(), rectangle.getWidth(), rectangle.getHeight());
template.closePathFillStroke();
template.setColorFill(textColor);
ColumnText columnText = new ColumnText(template);
columnText.setSimpleColumn(rectangle);
Paragraph p = new Paragraph("This is my test", new Font(Font.FontFamily.HELVETICA, 24, Font.BOLD));
p.setAlignment(Element.ALIGN_CENTER);
p.setLeading(50);
columnText.setAlignment(Element.ALIGN_CENTER);
columnText.addElement(p);
columnText.go();
writer.releaseTemplate(template);
dict.put(PdfName.N, template.getIndirectReference());
annotation.put(PdfName.AP, dict);
annotation.put(PdfName.F, new PdfNumber(4));
stamper.addAnnotation(annotation, 1);
stamper.close();
reader.close();
I attached the Screenshot this issue for your review too.
I did some research and if I change the line code
PdfAnnotation annotation = PdfAnnotation.createSquareCircle(writer, rectangle, null, true);
to
PdfAnnotation annotation= stamper.getWriter().createAnnotation(rectangle, PdfName.FREETEXT);
The text will still there when I moving the Stamp, but still disappear if I resize the Rectangle.

Keep Anchored Buttons Below Autosized Label

I have a Windows form with an image, a label that contains an error message, and a "Close" button:
The error message could be very short or very long depending on what the error is. When it's long, I want the window to grow in height to display the whole message. Using the label's Anchor and MaximumSize properties and the form's AutoSize property, I was able to get the Label to expand to the maximum width and then expand vertically until the message is displayed. Setting the button's Anchor property to Bottom, Right locks the button to the bottom right corner as intended but the form only expands far enough to display the label and not enough for the button too. So the button is displayed behind the label and can't be seen:
I'd like to know how I can stretch the form to fully contain the text of the label and still leave room at the bottom for the anchored button to produce something like this:
Add 3 panels to your form docked left, bottom and fill. Set the properties as indicated in the image below.
Then set the Label's maximum width to a fixed value in the properties or you can calculate it at run-time:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.Label1.MaximumSize = New Size(Me.panelFill.Width, 0)
End Sub
The easiest way is to build the layout with 3 docked panels, like this (hope you can adjust for your needs):
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Samples
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form { Padding = new Padding(8), AutoSizeMode = AutoSizeMode.GrowAndShrink, AutoSize = true, Font = new Font("Consolas", 9, FontStyle.Bold) };
var contentPanel = new Panel { Dock = DockStyle.Fill, Parent = form, AutoSizeMode = AutoSizeMode.GrowAndShrink, AutoSize = true };
var imagePanel = new Panel { Dock = DockStyle.Left, Parent = form };
var buttonPanel = new Panel { Dock = DockStyle.Bottom, Parent = form };
var image = new PictureBox { BackColor = Color.Red, Width = 32, Height = 32, Parent = imagePanel };
imagePanel.Width = image.Width + 8;
var button = new Button { Top = 8, AutoSize = true, BackColor = Color.Green, ForeColor = Color.White, Text = "Test", Parent = buttonPanel };
button.Left = buttonPanel.DisplayRectangle.Right - button.Width;
buttonPanel.Height = button.Height + 8;
button.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
var label = new Label { Dock = DockStyle.Fill, BackColor = Color.Blue, ForeColor = Color.White, MaximumSize = new Size(300, 0), AutoSize = true, Parent = contentPanel };
label.Text = #"The error message could be very short or very long depending on what the error is. When it's long, I want the window to grow in height to display the whole message. Using the label's Anchor and MaximumSize properties and the form's AutoSize property, I was able to get the Label to expand to the maximum width and then expand vertically until the message is displayed. Setting the button's Anchor property to Bottom, Right locks the button to the bottom right corner as intended but the form only expands far enough to display the label and not enough for the button too. So the button is displayed behind the label and can't be seen:";
Application.Run(form);
}
}
}
Result:

WP RichTextBox Vertical Alignment

I'm dynamically parsing data and adding text as Run, Hyperlinks and images as InlineUIContainer into a Windows Phone 8.0 RichTextBox. Somehow I can't manage that the images vertically align centered with the text.
Images are added like this:
Paragraph paragraph = new Paragraph();
richTextBox.Blocks.Add(paragraph);
var img = new Image
{
Stretch = Stretch.Uniform,
Source = imageSource,
VerticalAlignment = VerticalAlignment.Center,
Height = inlineImageSize,
};
paragraph.Inlines.Add(new InlineUIContainer {Child = img});
And text like that:
Paragraph paragraph = new Paragraph();
richTextBox.Blocks.Add(paragraph);
paragraph.Inlines.Add(new Run { Text = text });
I tried to set a few values for alignment on the RichTextBox as well, but the text is never centered with the images. The text is always bottom aligned.
Any chance getting the inline images vertically centered with the inline text in the WP RichTextBox?
I think what you are looking for is the BaselineAlignment Property.
try the following :
Paragraph paragraph = new Paragraph();
richTextBox.Blocks.Add(paragraph);
var img = new Image
{
Stretch = Stretch.Uniform,
Source = imageSource,
BaselineAlignment = BaselineAlignment.Center,
Height = inlineImageSize,
};
paragraph.Inlines.Add(new InlineUIContainer {Child = img});
Sorry for late reply. Try to set Margin for your Inline Images:
Paragraph paragraph = new Paragraph();
richTextBox.Blocks.Add(paragraph);
var img = new Image
{
Stretch = Stretch.Uniform,
Source = imageSource,
Height = inlineImageSize,
Margin = new Thickness(0,0,0,-5);
};
paragraph.Inlines.Add(new InlineUIContainer {Child = img});

JPanel resize and scroll errors

I use a text area with variable hight and some other things (a combo box in the sample code) filling the window width.
The following error happens:
- When using "innerPanel.setPreferredSize", the scroller does not appear when the text area becomes higher than the window by typing line feeds into the text area or when resizing the window vertically.
- When not using "innerPanel.setPreferredSize", (as shown commented in the sample code below), the used swing elements enlarge horizontally when resizing the window width, but do never shrink horizontally.
JPanel innerPanel = new JPanel(new GridBagLayout());
JTextArea editArea = new JTextArea();
editArea.setLineWrap(true);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.;
innerPanel.add(editArea, gbc);
JComboBox combo = new JComboBox();
gbc.gridy = 1;
innerPanel.add(combo, gbc);
JScrollPane scroller = new JScrollPane(innerPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//innerPanel.setPreferredSize(new Dimension(50, 50));
JPanel outerPanel = new JPanel(new BorderLayout());
outerPanel.add(scroller);
frame.setContentPane(outerPanel);

ITextSharp - text field in PdfPCell

I'm using iTextSharp to create a PDF, how can I add a textField into PdfPCell?
You wouldn't really add a 'text field' to a PdfPCell, you'd create a PdfPCell and add text (or other stuff) to that.
mikesdotnetting.com might have the clearest example and there's always the iTextSharp tutorial.
Give this a try. It works for me.
Document doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f);
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
doc.Open();
// Create your PDFPTable here....
TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(67, 585, 140, 800), "cellTextBox");
PdfPCell tbCell = new PdfPCell();
iTextSharp.text.pdf.events.FieldPositioningEvents events = new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tf.GetTextField());
tbCell.CellEvent = events;
myTable.AddCell(tbCell);
// More code...
I adapted this code from this post.
Edit:
Here is a full working console application that puts a TextBox in a table cell. I tried to keep the code to a bare minimum.
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace iTextSharpTextBoxInTableCell
{
class Program
{
static void Main(string[] args)
{
// Create a PDF with a TextBox in a table cell
BaseFont bfHelvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false);
Font helvetica12 = new Font(bfHelvetica, 12, Font.NORMAL, Color.BLACK);
Document doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f);
FileStream fs = new FileStream("TextBoxInTableCell.pdf", FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
PdfPTable myTable = new PdfPTable(1);
myTable.TotalWidth = 568f;
myTable.LockedWidth = true;
myTable.HorizontalAlignment = 0;
TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(67, 585, 140, 800), "cellTextBox");
PdfPCell tbCell = new PdfPCell(new Phrase(" ", helvetica12));
iTextSharp.text.pdf.events.FieldPositioningEvents events =
new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tf.GetTextField());
tbCell.CellEvent = events;
myTable.AddCell(tbCell);
doc.Add(myTable);
doc.Close();
fs.Close();
Console.WriteLine("End Of Program Execution");
Console.ReadLine();
}
}
}
Bon chance
DaveB's answer works, but the problem is that you have to know the coordinates to place the textfield into, the (67, 585, 140, 800). The more normal method of doing this is to create the table cell and add a custom event to the cell. When the table generation calls the celllayout event it passes it the dimensions and coordinates of the cell which you can use to place and size the textfield.
First create this call, which is the custom event
public class CustomCellLayout : IPdfPCellEvent
{
private string fieldname;
public CustomCellLayout(string name)
{
fieldname = name;
}
public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
{
PdfWriter writer = canvases[0].PdfWriter;
// rectangle holds the dimensions and coordinates of the cell that was created
// which you can then use to place the textfield in the correct location
// and optionally fit the textfield to the size of the cell
float textboxheight = 12f;
// modify the rectangle so the textfield isn't the full height of the cell
// in case the cell ends up being tall due to the table layout
Rectangle rect = rectangle;
rect.Bottom = rect.Top - textboxheight;
TextField text = new TextField(writer, rect, fieldname);
// set and options, font etc here
PdfFormField field = text.GetTextField();
writer.AddAnnotation(field);
}
}
Then in your code where you create the table you'll use the event like this:
PdfPCell cell = new PdfPCell()
{
CellEvent = new CustomCellLayout(fieldname)
// set borders, or other cell options here
};
If you want to different kinds of textfields you can either make additional custom events, or you could add extra properties to the CustomCellLayout class like "fontsize" or "multiline" which you'd set with the class constructor, and then check for in the CellLayout code to adjust the textfield properties.