Itext5, highlight one word in a phrase - itext

I am designing my pdf with a table, so pdf cells recieve a phrase and not a paragraph.
I have this phrase:
Phrase subject = new Phrase( "Subject: " + NEWLINE + investigation.Title);
and I want the word "subject" will be highlighted with a diffrent font, how do I change the font for a single word in a phrase
I would like to do it somehing like that:
Chunk chunk = new Chunk("Conclusions", titleFont);
Chapter chapter = new Chapter(new Paragraph(chunk), 1);
chapter.NumberDepth = (0);
chapter.Add(new Paragraph(investigation.InvestigationResult, textFont));
doc.Add(chapter);
but in a phrase

Split your text and create a chunk for each part (or at least a chunk for the part that varies). You can set the font for each chunk individually and then add all chunks to a phrase.

Related

powershell edit powerpoint slide notes

I am trying to use PowerShell to pro-grammatically update notes in PowerPoint slide notes. Being able to do this will save tremendous amounts of time. The code below allows me to edit the notes field with PowerShell but it messes up the format each time.
$PowerpointFile = "C:\Users\username\Documents\test.pptx"
$Powerpoint = New-Object -ComObject powerpoint.application
$ppt = $Powerpoint.presentations.open($PowerpointFile, 2, $True, $False)
foreach($slide in $ppt.slides){
if($slide.NotesPage.Shapes[2].TextFrame.TextRange.Text -match "string"){
$slide.NotesPage.Shapes[2].TextFrame.TextRange.Text = $slide.NotesPage.Shapes[2].TextFrame.TextRange.Text -replace "string","stringreplaced"
}
}
Sleep -Seconds 3
$ppt.Save()
$Powerpoint.Quit()
For example, right now it will iterate through each slide's notes and update the word string to stringreplaced but then the entire notes text becomes bold. In my notes I have a single word at the top of the notes that is bold and then text below it. For example, a note on a slide my look like this:
Note Title
Help me with this string.
After PowerShell updates the notes field it saves it to a new .pptx file but the note now looks like this:
Note Title
Help me with this stringreplaced.
Any ideas on how to update slide notes without messing up any formatting found in the notes? It only messes up formatting for slides the script updates.
When you change the entire text content of a textrange in PPT, as your code's doing, the changed textrange will pick up the formatting of the first character in the range. I'm not sure how you'd do this in PowerShell, but here's an example in PPT VBA that demonstrates the same problem and shows how to use PPT's own Replace method instead to solve the problem:
Sub ExampleTextReplace()
' Assumes two shapes with text on Slide 1 of the current presentation
' Each has the text "This is some sample text"
' The first character of each is bolded
' Demonstrates the difference between different methods of replacing text
' within a string
Dim oSh As Shape
' First shape: change the text
Set oSh = ActivePresentation.Slides(1).Shapes(1)
With oSh.TextFrame.TextRange
.Text = Replace(.Text, "sample text", "example text")
End With
' Result: the entire text string is bolded
' Second shape: Use PowerPoint's Replace method instead
Set oSh = ActivePresentation.Slides(1).Shapes(2)
With oSh.TextFrame.TextRange
.Replace "sample text", "example text"
End With
' Result: only the first character of the text is bolded
' as it was originally
End Sub

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

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().

How can I use regular and bold in a single String?

I have a String that consists of a constant part and a variable part.
I want the variable to be formatted using a regular font within the text paragraph, whereas I want the constant part to be bold.
This is my code:
String cc_cust_name = request.getParameter("CC_CUST_NAME");
document.add(new Paragraph(" NAME " + cc_cust_name, fontsmallbold));
My code for a cell in a table looks like this:
cell1 = new PdfPCell(new Phrase("Date of Birth" + cc_cust_dob ,fontsmallbold));
In both cases, the first part (" NAME " and "Date of Birth") should be bold and the variable part (cc_cust_name and cc_cust_dob) should be regular.
Right now you are creating a Paragraph using a single font: fontsmallbold. You want to create a Paragraph that uses two different fonts:
Font regular = new Font(FontFamily.HELVETICA, 12);
Font bold = Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Paragraph p = new Paragraph("NAME: ", bold);
p.add(new Chunk(CC_CUST_NAME, regular));
As you can see, we create a Paragraph with content "NAME: " that uses font bold. Then we add a Chunk to the Paragraph with CC_CUST_NAME in font regular.
See also How to set two different colors for a single string in itext and Applying color to Strings in Paragraph using Itext which are two questions that address the same topic.
You can also use this in the context of a PdfPCell in which case you create a Phrase that uses two fonts:
Font regular = new Font(FontFamily.HELVETICA, 12);
Font bold = Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Phrase p = new Phrase("NAME: ", bold);
p.add(new Chunk(CC_CUST_NAME, regular));
PdfPCell cell = new PdfPCell(p);

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

Displaying text from two fields, separated by a varying number of "." symbols, while preserving the total string length

I'm trying to create a Table of Contents for a small publication using Filemaker 10, since that's what the data has been stored in previously.
I'm able to generate page numbers, add heading to the TOC and pretty much everything else I've needed to do - one thing withstanding.
Our designer wants to fill each TOC line with "." to make it easier to read.
Currently:
Using Stack Overflow 1
Why Reddit is better than digg 7
Does Filemaker really suck this much 84
Ways to convince bosses 92
Ditching FileMaker 97
Wanted:
Using Stack Overflow..................................................1
Why Reddit is better than digg........................................7
Does Filemaker really suck this much.................................84
Ways to convince bosses..............................................92
Ditching FileMaker...................................................97
The item and page number are in different fields. Using a border is unsatisfactory because it underlines everything.
Solutions?
You can do this using tab stops in the Format -> Text menu
1) Create a calc field with the following definition (the character in the quotes is a tab):
title & " " & page
2) Add this field to your layout (it needs to be an actual field, not a merge field)
3) Highlight the field and choose format -> text -> paragraph -> tabs
4) Create a new Tab with a position of 6 inches and a Fill Character of "." or "…"
Now when viewed, any space from the end of the title up to the tab stop 6 inches away is filled with the fill character. No monospace font required.
You need to break it up into bits and then put it back with the right spacing. Something like this would do :
Let ( [
text = "Why Reddit is better than digg........................................7" ;
len = Length ( text ) ;
end = RightWords ( text ; 1 ) ;
lenEnd = Length ( end ) ;
lenStart = Length ( Trim ( Right ( text ; len - lenEnd ) ) ) ] ;
Left ( text ; lenStart ) &
Left ( "..........................................................................." ; len - lenStart - lenEnd ) &
end )
I've built the "text" variable into the calc for testing, but you could do this as a Custom Function or just inside a calculation with the field instead.
Also this assumes you're using a mono spaced font and the gap in the middle is a space character.