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

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);

Related

Ə character in Pdf

I want to add paragraph with Ə character to pdf file. I try every Thing but Ə character is not shown in pdf file. What can I do in this situation.
My code is here: BaseFont bf = BaseFont.createFont("assets/font/AZER_TM.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Paragraph tranportParagraph = new Paragraph("XƏƏƏƏƏöğııəçş\u0259", new Font(bf, 22));
tranportParagraph.setAlignment(Element.ALIGN_CENTER);
document.add(tranportParagraph);
this is a picture of my AZER_TM.ttf file:
As you see in the above picture There Ə character in the my AZER_TM.ttf file
But the character is not shown in the pdf file.

Itext5, highlight one word in a phrase

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.

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

IText Pdf Reader with Images

I have the pdf which is of 2 column format. I am able to parse it to simple text, but these pdfs also have images in between . As a result my text output gets jumbled up for that specific page of the pdf which have images in between.
For example consider a 2 column page format
Image Text2
Image Image
Image Text3
Text1 Image
Text4
Output is
Text4 Text3 Text2 Text1 instead of Text1 Text2 Text3 Text4
Any solution for this to read the text in the proper order?
I am using the following code
public void parsePdf(String pdf, String txt) throws IOException {
PdfReader reader = new PdfReader(pdf);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
PrintWriter out = new PrintWriter(new FileOutputStream(txt));
TextExtractionStrategy strategy;
for (int i = 76; i <= reader.getNumberOfPages(); i++) {
strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
out.println(strategy.getResultantText());
}
out.flush();
out.close();
}
You are using the SimpleTextExtractionStrategy. This strategy assumes the letter groups in the page content are already in a sensible order. Try the LocationTextExtractionStrategy instead which sorts those letter groups.
You seem to prefer an interesting order, though. According to your question, you want to get Text1 Text2 Text3 Text4 for
Image Text2
Image Image
Image Text3
Text1 Image
Text4
The LocationTextExtractionStrategy will order top to bottom primarily, though, and only secondarily left to right. Thus, you'll get Text2 Text3 Text1 Text4. For your requirement you should copy LocationTextExtractionStrategy and change it to order the text fragments the way you need them.
If that desired order is due to the content being meant to be interpreted as being in two columns, though, you may want to parse the columns separately by filtering the strategy input:
Rectangle rect = new Rectangle(x1, y1, x2, y2);
RenderFilter filter = new RegionTextRenderFilter(rect);
TextExtractionStrategy strategy = new FilteredTextRenderListener(
new LocationTextExtractionStrategy(), filter);
Confer the iText in Action, 2nd edition example ExtractPageContentArea.
Regards, Michael

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