Hi everybody,
I try to change the character spacing for AcroForm Textfield.
What I found so far, is that I can modify the font Widths using the
following code.
BaseFont font = BaseFont.CreateFont("Courier", "winansi", true,
true,null, null);
int[] widths = font.Widths;
for (int k = 0; k < widths.Length; k++)
{
widths[k] = width;
}
font.ForceWidthsOutput = true;
But with this solution, all the field on the forms will have the same
character spacing.
Does anyone know another way to do this ?
Thanks for your help.
Laurent.
The complete code :
// Create the output files
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, new
FileStream("c:\\0\\out.pdf", FileMode.Create, FileAccess.Write));
document.Open();
PdfContentByte cb = writer.DirectContent;
//Add exisiting Pdf to the first page
PdfReader reader = new PdfReader("c:\\0\\a.pdf");
PdfImportedPage page1 = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page1, 1, 0, 0, 1, 0, 0);
//Add text field to the existing page, using absolute coordinate of the
existing fill case.
_addField(writer, "f01", 125, 737, 200, 15, cb, 975, 14);
_addField(writer, "f02", 200, 712, 370, 15, cb, 810, 32);
private void _addField(PdfWriter writer, string name, int x, int y, int
w, int h, PdfContentByte cb, int width, int maxSize)
{
Rectangle rec = new Rectangle(x, y-h, x+w, y);
// Change the font Widths
BaseFont font = BaseFont.CreateFont("Courier",
"winansi", true, true, null, null);
float fontSize = 14;
int[] widths = font.Widths;
for (int k = 0; k < widths.Length; k++)
{
if (widths[k] != 0)
widths[k] = width;
}
font.ForceWidthsOutput = true;
string text = "Ici";
string all =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-*/+.,\"'():;,?./!$+=_{[]}&éèàêîâ
";
Color textColor = Color.RED;
// Create the Acroform TextField
PdfFormField field =
PdfFormField.CreateTextField(writer, PdfFormField.SINGLELINE,
PdfFormField.PLAINTEXT, maxSize);
field.FieldName = name;
field.DefaultValueAsString = text;
field.SetFieldFlags(PdfAnnotation.FLAGS_PRINT);
field.SetPage();
writer.AddAnnotation(field);
// Position the field on the page
field.SetWidget(rec, PdfAnnotation.HIGHLIGHT_INVERT);
// Create apperance so the field Size and font match
what we want
PdfAppearance tp =
cb.CreateAppearance(rec.Right-rec.Left, rec.Top-rec.Bottom);
PdfAppearance da = (PdfAppearance)tp.Duplicate;
da.SetFontAndSize(font, fontSize);
da.SetColorFill(textColor);
field.DefaultAppearanceString = da;
// Fill the default value of the field
tp.BeginVariableText();
tp.SaveState();
tp.Rectangle(rec);
tp.Clip();
tp.NewPath();
tp.BeginText();
tp.SetFontAndSize(font, fontSize);
tp.SetColorFill(textColor);
tp.SetTextMatrix(100,100);
tp.ShowText(all);
tp.SetTextMatrix(2,
(rec.Top-rec.Bottom)/2-(fontSize*0.3f));
tp.ShowText(text);
tp.EndText();
tp.RestoreState();
tp.EndVariableText();
field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
}
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions