Apache POI word, how to remove the white space (new line) from beginning of table cell? - ms-word

I am only able to style the contents of a table cell using paragraph element to change the color of the text for example.
When i create a paragraph inside the table it creates a new line. For my project i need these cell to be as small as possible for that i am using
tr.setHeight(300);
tr.getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);
that works when there is no blank lines.
the code below is the problem
XWPFTable tb = document.createTable(2,2);
XWPFParagraph p1 = tb.getRow(0).getCell(0).addParagraph();
XWPFRun p1run = p1.createRun();
p1run.setText("why is there a space");
XWPFParagraph p2 = tb.getRow(1).getCell(1).addParagraph();
XWPFRun p2run = p2.createRun();
p2run.setText("still there ");
This create this output
http://i.stack.imgur.com/bSONW.png

I found the solution here..
Duplicate Table Paragraphs in Docx created with Apache POI
It seems you add a paragraph then remove the original with this code:
XWPFParagraph paragraph = cell.addParagraph();
cell.removeParagraph(0);

Before adding a paragraph clearing text in cell worked for me
cell.clearText();

This works >>
Use cell.getParagraphs().get(0) instead of cell.addParagraph().

Related

I want to copy the contents of the shapes in visio

hope you can help. I have a visio file will a lot of shapes (100s) I want to copy the contents of the shapes (the words) and paste it into a spreadsheet. I can't figure out if this is possible. Grateful for any help.
You mean text in shapes ?
These shapes are groups or single shapes ?
That code iterate single shapes in page and fill workbook rows
Sub vv()
Dim sh As Shape
Dim ea As Object
' create new excel session
Set ea = CreateObject("Excel.Application")
' make new excel session visible
ea.Visible = True
Dim ew As Object
' create new workbook
Set ew = ea.workbooks.Add
Dim r As Integer
r = 0
' iterate all shapes in active page
For Each sh In ActivePage.Shapes
' if shape have some text add content to workbook
If Len(sh.Text) > 0 Then
r = r + 1
ew.sheets(1).Cells(r, 1) = sh.Text
End If
Next
End Sub
Use a report to export the text of the shapes.
http://www.hamishking.com/2010/05/27/using-visio-shape-reports-to-export-detail-from-your-diagrams/
Thank you for your responses. I ran a report to export the text of the shapes as someone suggested at it worked. ty

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.

LibreOffice Draw -add hyperlinks based on query table

I am using draw to mark up a pdf format index map. So in grid 99, the text hyperlinks to map99.pdf
There are 1000's of grid cells - is there a way for a (macro) to scan for text in a sheet that is like
Text in File | Link to add
99|file:///c:/maps/map99.pdf
100|file:///c:/maps/map100.pdf
and add links to the relevant file whenever the text is found (99,100 etc).
I don't use libre much but happy to implement any programatic solution.
Ok, after using xray to drill through enumerated content, I finally have the answer. The code needs to create a text field using a cursor. Here is a complete working solution:
Sub AddLinks
Dim oDocument As Object
Dim vDescriptor, vFound
Dim numText As String, tryNumText As Integer
Dim oDrawPages, oDrawPage
Dim oField, oCurs
Dim numChanged As Integer
oDocument = ThisComponent
oDrawPages = oDocument.getDrawPages()
oDrawPage = oDrawPages.getByIndex(0)
numChanged = 0
For tryNumText = 1 to 1000
vDescriptor = oDrawPage.createSearchDescriptor
With vDescriptor
'.SearchString = "[:digit:]+" 'Patterns work in search box but not here?
.SearchString = tryNumText
End With
vFound = oDrawPage.findFirst(vDescriptor)
If Not IsNull(vFound) Then
numText = vFound.getString()
oField = ThisComponent.createInstance("com.sun.star.text.TextField.URL")
oField.Representation = numText
oField.URL = numText & ".pdf"
vFound.setString("")
oCurs = vFound.getText().createTextCursorByRange(vFound)
oCurs.getText().insertTextContent(oCurs, oField, False)
numChanged = numChanged + 1
End If
Next tryNumText
MsgBox("Added " & numChanged & " links.")
End Sub
To save relative links, go to File -> Export as PDF -> Links and check Export URLs relative to file system.
I uploaded an example file here that works. For some reason your example file is hanging on my system -- maybe it's too large.
Replacing text with links is much easier in Writer than in Draw. However Writer does not open PDF files.
There is some related code at https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=1401.

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".

How to use non breaking space in iTextSharp

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