iText7 Tabbing for paragraphs - itext7

My requirement is build pdf as below using iText 7.
Using the below code.
Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
table.addCell(getNameCell("First Lastname", TextAlignment.LEFT));
// table.addCell(getCell("Text in the middle", TextAlignment.CENTER));
table.addCell(getLogoCell("Company", TextAlignment.RIGHT));
table.addCell(getAddressCell("1111 Boones Ridge Rd", TextAlignment.LEFT));
table.addCell(getLogoCell(" ", TextAlignment.RIGHT));
table.addCell(getAddressCell("Acworth, Ga 30102", TextAlignment.LEFT));
table.addCell(getLogoCell(" ", TextAlignment.RIGHT));
table.addCell(getAddressCell("P: 1234567890", TextAlignment.LEFT));
table.addCell(getLogoCell(" ", TextAlignment.RIGHT));
table.addCell(getAddressCell("test#gmail.com", TextAlignment.LEFT));
table.addCell(getLogoCell(" ", TextAlignment.RIGHT));
table.addCell(getAddressCell("www.testonline.com", TextAlignment.LEFT));
table.addCell(getLogoCell(" ", TextAlignment.RIGHT));
doc.add(table);
Here, unnecessarily adding empty space or else text gets aligned horizontally. Also, since the right text font is bigger, there is a space b/w the name and address. Is there any better way to handle this?

iText 7 has tab stops functionality. You can add a tab and align the text to the right in the following way:
Paragraph p = new Paragraph();
p.addTabStops(new TabStop(PageSize.A4.getRight() - document.getLeftMargin() - document.getRightMargin(),
TabAlignment.RIGHT));
p.add(new Text("Hello")).add(new Tab()).add(new Text("World"));
To tweak spacing between your name and address, you may play around with leading property:
p.setFixedLeading(12);

Related

How can I Bold one Word in a Line in iText 7?

I can set text bold using iText 7 like so:
parExecSummHeader2.Add(new Text(subj).SetBold());
...but when I try to combine a "normal" (non-bolded) bit of text with a bolded part, it doesn't work. I have this, which prints the line all "regular" (no bold):
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): " + Math.Round(WordsPerSentenceInDoc, 2).ToString());
...but want to make the calculated value bold. I tried both this:
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
parExecSummHeader2.Add(new Text(Math.Round(WordsPerSentenceInDoc, 2).ToString().SetBold()));
...and this:
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
string mathval = Math.Round(WordsPerSentenceInDoc, 2).ToString();
parExecSummHeader2.Add(new Text(mathval.SetBold()));
...but they both won't compile, complaining, "Error CS1061 'string' does not contain a definition for 'SetBold' and no accessible extension method 'SetBold' accepting a first argument of type 'string' could be found"
Shorter option which may result in not perfect quality of text rendering because bold simulation is used instead of a proper bold font:
Paragraph parExecSummHeader2 = new Paragraph();
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
parExecSummHeader2.Add(new Text("123").SetBold());
Option with more code but better output quality because the proper bold font is use:
PdfFont boldFont = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
Paragraph parExecSummHeader2 = new Paragraph();
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
parExecSummHeader2.Add(new Text("123").SetFont(boldFont));
For iText 7:
public static final Font HELVETICA_BOLD =
new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLUE);
new Text("MyText").setFontColor(Color.BLUE)
.setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD));
You have some more details examples here
For iText 5:
public const string DEFAULT_FONT_FAMILY = "Arial";
public static Font SmallFontBold
{
get
{
BaseColor black = new BaseColor(0, 0, 0);
Font font = FontFactory.GetFont(DEFAULT_FONT_FAMILY, 10, Font.BOLD, black);
return font;
}//get
}//SmallFontBold
...
Phrase aPh = new Phrase("My Bold", SmallFontBold);
And from here, you can try to use it combined.

Xamarin.Forms VoiceOver in two vertical stacks

I do have one hozitontal StackLayout that contains two vertical StackLayouts. When I turn on VoiceOver it reads by rows not by stacks. Is there any solution? I also tried that in Grid but problem was same... ty
I solved my problem by using
AutomationProperties.SetIsInAccessibleTree(myStackLayout, true);
AutomationProperties.SetName(myStackLayout, Title + " " + Value + " " + Unit);
it reads well whole control at once
source: https://blog.xamarin.com/accessbility-xamarin-forms/

Dynamically change the font size in Birt

I am using a Dynamic Text in Birt. I want to display a sentence on one line but if it's too width it's displayed on two or more. Is there a way to dynamically decrease the font size when the sentence is too width so it stays on one line?
Thank you for your help.
first choose your cell and then click on the script tab,there choose onRender from the drop-down and put this code
this.getStyle().fontSize this gives you to change the fontsize dynamically while rendering the page...
Cheers..
Here is my solution - building on what #Sundar said.
In my case I have two fields in one cell. It is not perfect, but the only way I have to know when the text is "too big" is to measure the length of the combined strings and then adjust the font size accordingly.
Click on the cell and then the script tab, and enter code like this in the onRender script
// Decide if we need to change font size.
totalLength = 0;
if (row["Field Name 1"]) {
totalLength += row["Field Name 1"].length;
}
if (row["Field Name 2"]) {
totalLength += row["Field Name 2"].length;
}
// Make the font smaller with larger amounts of text.
switch (true) {
case (totalLength > 200):
this.getStyle().fontSize = "6pt";
break;
case (totalLength > 100 && totalLength < 200):
this.getStyle().fontSize = "8pt";
break;
default:
this.getStyle().fontSize = "10pt";
break;
}

Adding text to existing pdf which is closed using itextsharp

Hi
I am creating PDF using itextsharp. Now my requirement is to add more text to the existing pdf. Is it possible if so then how can I do that?
Thanks Dipa
Yes, with certain limitations.
It is difficult, but not impossible, to determine what is already on an existing page.
If all you want to do is add "page X of Y" to the bottom left corner of all your pages, that's easy.
PdfReader reader = new PdfReader( inPath );
PdfStamper stamper = new PdfStamper( reader, new FileOutputStream( outPath ) );
BaseFont font = BaseFont.createFont(); // Helvetica, WinAnsiEncoding
for (int i = 0; i < reader.getNumberOfPages(); ++i) {
PdfContentByte overContent = stamper.getOverContent( i + 1 );
overContent.saveState();
overContent.beginText();
overContent.setFontAndSize( font, 10.0f );
overContent.setTextMatrix( xLoc, yLoc );
overContent.showText( "Page " + (i + 1) + " of " + reader.getNumberOfPages() );
overContent.endText();
overContent.restoreState();
}
stamper.close();
A big watermark isn't much more difficult. Adding things to a PDF at one or more predetermined locations is quite doable.
At the other end of the spectrum is "change text within existing paragraphs and reflow them". That's all but impossible. It would be much easier to rebuild the original PDF with the new data.
In fact, if at all possible, just rebuild them. You did it once, do it again.

AS3 TextField.textWidth ignore whitespace at end

I'm trying to calculate the textWidth, but it seems to ignore the white spaces at the end. The text
"Hello"
"Hello "
returns the same text width. A string with " " returns 0. How do I calculate the width with the space?
You could always get the length of the string.
text.length
And then multiply that by how wide your characters are.
For example:
var aChar:String = "A";
var textWidth:int = (text.length) * (width of aChar);
Choose a terminator character, for Example "|".
Calculate Width(Text+Termainator)-Width(Terminator).

Resources