iText spacing inbetween paragraph - itextpdf

managerp4.add(Chunk.NEWLINE);
managerp4.add(managerVO.getManagerAddress().getRoad2());
managerp4.add(Chunk.NEWLINE);
managerp4.add(managerVO.getManagerAddress().getRoad3());
This will generate output in newline like this
Road 2
Road 3
but i want to display in same line with space between two values

you can use the method com.itextpdf.text.Paragraph.setSpacingAfter(float) as follows:
Example
Paragraph p = new Paragraph("This a string");
p.setSpacingAfter(10);

Related

Format text with indentation

I am writing multi-lines in the file.
Number Name
Jack
The first line is a header.
I am going to write lines like the above format.
If the number is null, I am writing a line like this.
file.print "Jack".indentation("Number".length + 2)
There are 2 spaces between Number and Name in the header.
But it is showing like this since the length 6 spaces are not the same as "Number".
Number Name
Jack
Is there any solution to format better?

how to tokenize/parse/search&replace document by font AND font style in LibreOffice Writer?

I need to update a bilingual dictionary written in Writer by first parsing all entries into their parts e.g.
main word (font 1, bold)
foreign equivalent transliterated (font 1, italic)
foreign equivalent (font 2, bold)
part of speech (font 1, italic)
Each line of the document is the main word followed by the parts listed above, each separated by a space or punctuation.
I need to automate the process of walking through the whole file, line by line, and place a delimiter between each part, ignoring spaces and punctuation, so I can mass import it into a Calc file. In other words, "each part" is a sequence of character (ignoring spaces and punctuation) that have the same font AND font-style.
I have tried the standard Search&Replace feature, and AltSearch extension, but neither are able to complete the task. The main problem is I am not able to write a search query that says:
Find: consecutive characters with the same font AND font_style, ignore spaces and punctuation
Replace: term found above + "delimiter"
Any suggestions how I can write a script for this, or if an existing tool can solve the problem?
Thanks!
Pseudo code for desired effect:
var delimiter = "|"
Go to beginning of document
While not end of document do:
var $currLine = get line from doc
var $currChar = get next character which is not space or punctuation;
var $font = currChar.font
var $font_style - currChar.font_style (e.g. bold, italic, normal)
While not end of line do:
$currChar = next character which is not space or punctuation;
if (currChar.font != $font || currChar.font_style != $font_style) { // font or style has changed
print $delimiter
$font = currChar.font
$font_style - currChar.font_style (e.g. bold, italic, normal)
}
end While
end While
Here are tips for each of the things your pseudocode does.
First, the easiest way to move line by line is with the TextViewCursor, although it is slow. Notice the XLineCursor section. For the while loop, oVC.goDown() will return false when the end of the document is reached. (oVC is our variable for the TextViewCursor).
Get each character by calling oVC.goRight(0, False) to deselect followed by oVC.goRight(1, True) to select. Then the selected value is obtained by oVC.getString(). To ignore space and punctuation, perhaps use python's isalnum() or the re module.
To determine the font of the character, call oVC.getPropertyValue(attr). Values for attr could simply be CharAutoStyleName and CharStyleName to check for any changes in formatting.
Or grab a list of specific properties such as 'CharFontFamily', 'CharFontFamilyAsian', 'CharFontFamilyComplex', 'CharFontPitch', 'CharFontPitchAsian' etc. Character properties are described at https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Formatting.
To insert the delimiter into the text: oVC.getText().insertString(oVC, "|", 0).
This python code from github shows how to do most of these things, although you'll need to read through it to find the relevant parts.
Alternatively, instead of using the LibreOffice API, unzip the .odt file and parse content.xml with a script.

Fill line up with special Character before and after Text

I would like to create a Sublime Text 2 Snippet that fills up the space before and after the Variable I type with spaces.
It should look like this:
===========================my_filename.js===========================
The Filename should be centered so the number of spaces before and after the Text have to match.
Also I need the overall column width of this line the stay the same. So when I add 2 Characters the number of spaces on each side gets reduced by one.
I think a sample for this would be:
spacesLeft = roundDown((columnWidth/2) - (textSize/2))
spacesRight = roundUp((columnWidth/2) - (textSize/2))
But since only RegEx is available in Sublime Snippets I don't see me able, to accomplish this task.
Could Vintagmode help me in any Way?
Thanks for your help!
Because snippets are essentially static, they wouldn't be able to help you in this case. However, you could create a plugin to do this relatively easily. Sublime Text uses python for its plugins. You can create one by going to Tools > New Plugin. ST2's API can be found here and is very impressive. Essentially, you'll want to store the current selection (your variable) using
sel_reg = self.view.sel()[0] # the current selection 'Region'
sel = self.view.substr(sel_reg) # the text within the 'Region'
Then generate the ='s
signs = ''
each_side = len(80 - len(sel))/2 # 80 is the standard window length,
# change this to what you want.
# Not sure about how to make it dynamic.
for x in xrange(each_side):
signs += '='
Then replace the current line (self.view.line(sel)) with signs + sel + signs.

Remove hard line breaks from text with Ruby

I have some text with hard line breaks in it like this:
This should all be on one line
since it's one sentence.
This is a new paragraph that
should be separate.
I want to remove the single newlines but keep the double newlines so it looks like this:
This should all be on one line since it's one sentence.
This is a new paragraph that should be separate.
Is there a single regular expression to do this? (or some easy way)
So far this is my only solution which works but feels hackish.
txt = txt.gsub(/(\r\n|\n|\r)/,'[[[NEWLINE]]]')
txt = txt.gsub('[[[NEWLINE]]][[[NEWLINE]]]', "\n\n")
txt = txt.gsub('[[[NEWLINE]]]', " ")
Replace all newlines that are not followed by or preceded by a newline:
text = <<END
This should all be on one line
since it's one sentence.
This is a new paragraph that
should be separate.
END
p text.gsub /(?<!\n)\n(?!\n)/, ' '
#=> "This should all be on one line since it's one sentence.\n\nThis is a new paragraph that should be separate. "
Or, for Ruby 1.8 without lookarounds:
txt.gsub! /([^\n])\n([^\n])/, '\1 \2'
text.gsub!(/(\S)[^\S\n]*\n[^\S\n]*(\S)/, '\1 \2')
The two (\S) groups serve the same purposes as the lookarounds ((?<!\s)(?<!^) and(?!\s)(?!$)) in #sln's regexes:
they confirm that the linefeed really is in the middle of a sentence, and
they ensure that the [^\S\n]*\n[^\S\n]* part consumes any other whitespace surrounding the linefeed, making it possible for us to normalize it to a single space.
They also make the regex easier to read, and (perhaps most importantly) they work in pre-1.9 versions of Ruby that don't support lookbehinds.
There is more to formatting (turning off word wrap) than you think.
If the output is a result of a formatting operation, then you should go by
those rules to reverse engineer the original.
For instance, the test you have there is
This should all be on one line
since it's one sentence.
This is a new paragraph that
should be separate.
If you removed just the single newlines only, it would look like this:
This should all be on one line since it's one sentence.
This is a new paragraph thatshould be separate.
Also, other formatting such as intentional newlines will be lost, so something like:
This is Chapter 1
Section a
Section b
Turns into
This is Chapter 1 Section a Section b
Finding the newline in question is easy /(?<!\n)\n(?!\n)/
but, what do you replace it with.
Edit: Actually, its not that easy even to find standalone newlines, because visually they sit amongst hidden from view (horizontal) whitespaces.
There are 4 ways to go.
Remove newline, keep the surrounding formatting
$text =~ s/(?<!\s)([^\S\n]*)\n([^\S\n]*)(?!\s)/$1$2/g;
Remove newline and formatting, substitute a space
$text =~ s/(?<!\s)[^\S\n]*\n[^\S\n]*(?!\s)/ /g;
Same as above but ignore newline at beginning or end of string
$text =~ s/(?<!\s)(?<!^)[^\S\n]*\n[^\S\n]*(?!$|\s)/ /g;
$text =~ s/(?<!\s)(?<!^)([^\S\n]*)\n([^\S\n]*)(?!$|\s)/$1$2/g;
Example breakdown of regex (this is the minimum required just to isolate a single newline):
(?<!\s) # Not a whitespace behind us (text,number,punct, etc..)
[^\S\n]* # 0 or more whitespaces, but no newlines
\n # a newline we want to remove
[^\S\n]* # 0 or more whitespaces, but no newlines
(?!\s)/ # Not a whitespace in front of us (text,number,punct, etc..)
Well, there is this:
s.gsub /([^\n])\n([^\n])/, '\1 \2'
It won't do anything to leading or trailing newlines. If you don't need leading or trailing white space at all, then you will win with this variation:
s.gsub(/([^\n])\n([^\n])/, '\1 \2').strip
$ ruby -00 -pne 'BEGIN{$\="\n\n"};$_.gsub!(/\n+/,"\0")' file
This should all be on one line since it's one sentence.
This is a new paragraph thatshould be separate.

Latex Multiple Linebreaks

I use LaTeX to type up programming homeworks for classes. I need to do this:
my line of text blah blah blah
new line of text with blank line between
I know I can use double slash to break lines \\, but LaTeX will only take the first line break (complains about more) and starts a new line, it produces this :
my line of text blah blah blah
new line of text with blank line between
How can I get that extra line break in there so I can have space between my lines of text?
Line break accepts an optional argument in brackets, a vertical length:
line 1
\\[4in]
line 2
To make this more scalable with respect to font size, you can use other lengths, such as \\[3\baselineskip], or \\[3ex].
Do you want more space between paragraphs? Then you can change the parameter \parskip.
For example, try
\setlength{\parskip}{10pt plus 1pt minus 1pt}
This means that the space between paragraphs is usually 10pt, but can grow or shrink by up to 1pt. This means you give LaTeX the ability to change it up to one 1pt in order to achieve a better page layout. You can remove the plus and minus parts to make it always your specified length.
If you are trying to display source code, try the listings package or use verbatim. If you are trying to typeset pseudocode, try the algorithm package.
Insert some vertical space
blah blah blah \\
\vspace{1cm}
to scale to the font, use ex (the height of a lowercase "x") as the unit, and there are various predefined lengths related to the line spacing available, you might be particularly interested in baselineskip.
You can use the setspace package which gives you spacing environments, e.g.:
\documentclass{article}
\usepackage{setspace}
\begin{document}
\doublespace
my line of text blah blah blah
new line of text with blank line between
\end{document}
Or use a verbatim environment to control the layout of your code precisely.
For programs you are really better off with a verbatim or alltt environment, but if you want a blank line that LaTeX will not bitch about, try
my line of text blah blah blah\\
\mbox{ }\\ %% space followed by newline
new line of text with blank line between
While verbatim might be the best choice, you can also try the commands \smallskip , \medskip or guess what, \bigskip .
Quoting from this page:
These commands can only be used after
a paragraph break (which is made by
one completely blank line or by the
command \par). These commands output
flexible or rubber space,
approximately 3pt, 6pt, and 12pt high
respectively, but these commands will
automatically compress or expand a
bit, depending on the demands of the
rest of the page
I find that when I include a blank line in my source after the \\ then I also get a blank line in my output. Example:
It's time to recognize the income tax as another horrible policy mistake like banning beer, and to return to the tax policies that were correct in the Constitution in the first place. Our future depends on it.
\\
Wherefore the 16th Amendment must forthwith be repealed.
However you are correct that LaTeX only lets you do this once. For a more general solution allowing you to make as many blank lines as you want, use \null to make empty paragraphs. Example:
It's time to recognize the income tax as another horrible policy mistake like banning beer, and to return to the tax policies that were correct in the Constitution in the first place. Our future depends on it.
\null
\null
\null
Wherefore the 16th Amendment must forthwith be repealed.
\\\\
This works on pdfLatex. It creates 2 new lines for you.
Maybe try inserting lines with only a space?
\ \\
\ \\
This just worked for me:
I was trying to leave a space in the Apple Pages new LaTeX input area. I typed the following and it left a clean line.
\mbox{\phantom{0}}\\

Resources