How to use non breaking space in iTextSharp - itext

How can the non breaking space can be used to have a multiline content in a PdfPTable cell. iTextSharp is breaking down the words with the space characters.
The scenario is I want a multiline content in a table head, such as in first line it may display "Text1 &" and on second line it would display "Text", on rendering the PDF the Text1 is displayed in first line, then on second line & is displayed and on third it takes the length of the first line and truncates the remaining characters to the next line.
Or can I set specific width for each and every column of the table so as to accomodate text content within it, such as the text would wrap within that specific width.

You didn't specify a language so I'll answer in VB.Net but you can easily convert it to C# if needed.
To your first question, to use a non-breaking space just use the appropriate Unicode code point U+00A0:
In VB.Net you'd declare it like:
Dim NBSP As Char = ChrW(&HA0)
And in C#:
Char NBSP = '\u00a0';
Then you can just concatenate it where needed:
Dim Text2 As String = "This is" & NBSP & "also" & NBSP & "a test"
You might also find the non-breaking hyphen (U+2011) helpful, too.
To your second question, yes you can set the width of every column. However, column widths are always set as relative widths so if you use:
T.SetTotalWidth(New Single() {2.0F, 1.0F})
What you are actually saying is that for the given table, the first column should be twice as large as the second column, you are NOT saying that the first column is 2px wide and the second is 1px. This is very important to understand. The above code is the exact same as the next two lines:
T.SetTotalWidth(New Single() {4.0F, 2.0F})
T.SetTotalWidth(New Single() {100.0F, 50.0F})
The column widths are relative to the table's width which by default (if I remember correctly) is 80% of the writable page's width. If you would like to fix the table's width to an absolute width you need to set two properties:
''//Set the width
T.TotalWidth = 200.0F
''//Lock it from trying to expand
T.LockedWidth = True
Putting the above all together, below is a full working WinForms app targetting iTextSharp 5.1.1.0:
Option Explicit On
Option Strict On
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
''//File that we will create
Dim OutputFile As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TableTest.pdf")
''//Standard PDF init
Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
Using Doc As New Document(PageSize.LETTER)
Using writer = PdfWriter.GetInstance(Doc, FS)
Doc.Open()
''//Create our table with two columns
Dim T As New PdfPTable(2)
''//Set the relative widths of each column
T.SetTotalWidth(New Single() {2.0F, 1.0F})
''//Set the table width
T.TotalWidth = 200.0F
''//Lock the table from trying to expand
T.LockedWidth = True
''//Our non-breaking space character
Dim NBSP As Char = ChrW(&HA0)
''//Normal string
Dim Text1 As String = "This is a test"
''//String with some non-breaking spaces
Dim Text2 As String = "This is" & NBSP & "also" & NBSP & "a test"
''//Add the text to the table
T.AddCell(Text1)
T.AddCell(Text2)
''//Add the table to the document
Doc.Add(T)
Doc.Close()
End Using
End Using
End Using
Me.Close()
End Sub
End Class

Related

how to set paragraph column counts using MS Word

I just want to set the last para columns in below code:
wordDoc.Paragraphs.Last.Range.PageSetup.TextColumns.SetCount(3);
But it sets the page 3 columns;
So, what code can set the last para columns?
There is no such thing as a paragraph columns property - only a page columns property (TextColumns). If you want to limit the scope of TextColumns, insert a Section break before (and after, if applicable), the paragraph concerned. For example:
Sub Demo()
Dim wordDoc As Document
Set wordDoc = ActiveDocument
With wordDoc.Paragraphs.Last.Range
.InsertBreak Type:=wdSectionBreakContinuous
.PageSetup.TextColumns.SetCount (3)
End With
End Sub

Finding text AND fields with variable content in Word

I need to find and delete every occurrence of the following pattern in a Word 2010 document:
RPDIS→ text {INCLUDEPICTURE c:\xxx\xxx.png" \*MERGEFORMAT} text ←RPDIS
Where:
RPDIS→ and ←RPDIS are start and end delimiters
Between the start and end delimiters there can be just text or text and fields with variable content
The * wildcard in the Word Find and Replace dialog box will find the pattern if it contains text only but it will ignore patterns where text is combined with fields. And ^19 will find the field but not the rest of the pattern until the end delimiter.
Can anyone help, please?
Here's a VBA solution. It wildcard searches for RPDIS→*←RPDIS. If the found text contains ^19 (assuming field codes visible; if objects are visible instead of field codes, then the appropriate test is text contains ^01), the found text is deleted. Note that this DOES NOT care about the type of embedded field --- it will delete ANY AND ALL embedded fields that occur between RPDIS→ and ←RPDIS, so use at your own risk. Also, the code has ChrW(8594) and ChrW(8592) to match right-arrow and left-arrow respectively. You may need to change that if your arrows are encoded differently.
Sub test()
Dim wdDoc As Word.Document
Dim r As Word.Range
Dim s As String
' Const c As Integer = 19 ' Works when field codes are visible
Const c As Integer = 1 ' Works when objects are visible
Set wdDoc = ActiveDocument
Set r = wdDoc.Content
With r.Find
.Text = "RPDIS" & ChrW(8594) & "*" & ChrW(8592) & "RPDIS"
.MatchWildcards = True
While .Execute
s = r.Text
If InStr(1, s, chr(c), vbTextCompare) > 0 Then
Debug.Print "Delete: " & s
' r.Delete ' This line commented out for testing; remove comments to actively delete
Else
Debug.Print "Keep: " & s
End If
Wend
End With
End Sub
Hope that helps.

Prevent word wrap in GUI text

I have a MATLAB GUI that allows a user to load a configuration file. I then want the filename to be displayed in a static text field. My problem is that the string is too long for my text field and wraps around. I want the text to display as much of the string as it can without wrapping, prioritizing the end of the string.
For example, if I have a filename 'C:\folders\more\folders\thisismylongfilename.txt', I am currently seeing
C:\folders\more\folders\thisism
ylongfilename.txt
If I use an edit text rather than a static text, I see C:\folders\more\folders\thisism
I would like my text field to display olders\thisismylongfilename.txt, or maybe ...ers\thisismylongfilename.txt. The missing portion can either be "displayed" but outside the visible box, or something I can remove before displaying. I would just need to know how much of the string to remove.
How can I properly display my long string in a fixed width text box?
One way to accomplish this would be read the length of your textbox and shorten the string before you display it.
myString = 'path/to/file/file.txt';
set(handles.textbox,'Units', 'Characters'); %set units to characters for convenience
pos = get(handles.textbox,'Position'); %get the position info
maxLength = floor(pos(3)); %extract the length of the box
if length(myString) > maxLength % cut beginning if string is too long
newStart = length(myString) - maxLength + 1;
displayString = myString(newStart:end);
else
displayString = myString;
end
set(handles.textbox,'String', displayString);

jasperreports html split text without whitespaces

String example:
"fooTextExmaple" x 100
So, it looks like one large word without whitespaces.
I have no problems with same strings in pdf, however,
I have problems dealing with large text without whispaces while exporting to HTML.
It looks like long row which does not fit any fixed width. The row's width would change dynamically by adding new chars without whitespaces to this string:
Is there any way to deal with this problem?
EDIT:
The way like it does not work:
PrintWriter out = resp.getWriter();
resp.setContentType("text/html");
req.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print);
exporter = new HtmlExporter();
SimpleHtmlReportConfiguration configuration = new SimpleHtmlReportConfiguration();
configuration.setWrapBreakWord(true);
exporter.setConfiguration(configuration);
exporter.setExporterInput(new SimpleExporterInput(print));
SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(out);
String[] uriParts = req.getRequestURI().split("/");
output.setImageHandler(new WebHtmlResourceHandler("/" + uriParts[1] + "/image?image={0}"));
exporter.setExporterOutput(output);
exporter.exportReport();
Set the net.sf.jasperreports.text.save.line.breaks property to true.
Read more about it at http://jasperreports.sourceforge.net/config.reference.html
The net.sf.jasperreports.export.html.wrap.break.word property is supposed to help as well, but it only works in some browsers.

maskedEditColumn datagridview how to use class? is it what i need?

I am trying to mask user's input in a datagridview column and i found this ready class Masked edit column Class that adds a 'mask edit column' option in the column types list. When i select this column type a mask field is being added in the list of column properties. I tried to do my job by adding some mask elements in this 'Mask' field, but when I run the code it didnt restrict me from adding other characters. I re-opened the 'edit columns menu' and I saw that the 'Mask' field was empty.
I want the text cell to accept 20 chars maximum and only: 1.Capital Letters(English & Greek), 2.these three chars(.,-), 3.Numbers 0-9
So as a first test i used only this mask(>????????????????????) but it didnt work as it didnt convert my characters to Uppercase and accepted more than 20 chars when i end the cell edit.
i am not sure the way to go is the Masked Text Box way. i have made many projects on vb and i used to use a loop in the textChanged event of a text box to restrict characters entry. the loop is this : (but i cant use it now in the valueChanged event cause it seems that 'value' doesn't have a selectionStart property.)
Dim charactersDisallowed As String = "!##$%^&*()+=|}{][:;?/><.,~""
Dim theText As String = txtCopies.Text
Dim Letter As String
Dim SelectionIndex As Integer = txtCopies.SelectionStart
Dim Change As Integer
For x As Integer = 0 To txtCopies.Text.Length - 1
Letter = txtCopies.Text.Substring(x, 1)
If charactersDisallowed.Contains(Letter) Then
theText = theText.Replace(Letter, String.Empty)
Change = 1
End If
Next
txtCopies.Text = theText
txtCopies.Select(SelectionIndex - Change, 0)
So,
Is a masked text cell what i need? and if yes( Why is this mask box not keeping the mask i enter? And how can i use this class to do my job?)
What can i alternately do to restrict some characters in a column's cells? (I will then convert to Uppercase on cellEndEdit)
I finally did it by removing the unwanted characters on cellvaluechanged event, which seems that is being raised when I end the cell's edit by for example hitting "Enter".