ITextSharp, Assigning a font to List within a cell? - itext

Is there a way to assign a font to a list that is added to table cell in ITextsharp. I'm sure it must be straightforward but I'm missing it.
Dim tblSignature As New PdfPTable(1)
tblSignature.WidthPercentage = 90.0F
Dim _baseFont As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED)
Dim _bFont As New Font(_baseFont, 4, Font.ITALIC, Color.RED)
Dim cell As New PdfPCell(New Phrase(TheItem.Value, _bFont))
TheParagraph.Font = _bFont
Dim list As New List(list.UNORDERED, 25.0F)
list.SetListSymbol("[ ]")
If ListItems.Count > 0 Then
For Each ListItem In .ListItems
//I have tried adding as chunk/phrase and applying font but no joy
//Item not added to cell
//list.Add(New Chunk(ListItem, _bFont))
list.Add(ListItem)
Next
list.IndentationLeft = 5.0F
cell.AddElement(list)
End If
cell.Colspan = 6
cell.HorizontalAlignment = 0
cell.PaddingBottom = 5.0F
cell.PaddingLeft = 5.0F
cell.PaddingRight = 5.0F
'' add to doc
tblSignature.AddCell(cell)
TheParagraph.Add(tblSignature)
Does anyone know How I'd change this so that the list/cell has the specific font I've set at the top.
Cheers

The iTextSharp.text.ListItem object has an iTextSharp.text.Font property you can set.

Please note that declarations such as new PdfPCell or new Phrase will not work with iTextSharp.text.List's add method.But both of them fairly work with PdfPTable.
I used iTextSharp.text.List for one of my software's pdf print out in the following ways.I hope this may be useful to you also.
This is a program which is used in one of my software developed with vb.net(visual studio 2010 and windows 7 platform) and iTextSharp latest version.
for pdf print out.It demonstrates how we can manupulate iTextSharp.text.List's font properties.
Dim doc1 As New Document()
Dim pathpdf As String = "C:/temp"
'path = ServerMapPath("PDFs");
PdfWriter.GetInstance(doc1, New FileStream(pathpdf + "/Doc1.pdf", FileMode.Create))
' Now to begin actually working with the document, open it, , add a new list,and, then close it:
doc1.Open()
Dim fnt1 As Font = FontFactory.GetFont(FontFactory.HELVETICA, 8, iTextSharp.text.Font.NORMAL)
Dim fnt2 As Font = FontFactory.GetFont(FontFactory.HELVETICA, 14, iTextSharp.text.Font.BOLD)
Dim fnt3 As Font = FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.NORMAL)
Dim lst3 As New iTextSharp.text.List()
lst3.SetListSymbol("")
'add a list item in bold letters.
Dim m_DataHeadOnly As String
m_DataHeadOnly = "This is the itextsharp font setting with list"
lst3.Add(New iTextSharp.text.ListItem(m_DataHeadOnly, fnt2))
doc1.add(lst3)
doc1.close()

Related

Inset an image into an Open Office Draw document using Basic

I am wanting to insert an image into a LibreOffice Draw document using Basic. I create a Draw document and then I add a page to the document and then rename the pages. I want ti insert an image into Page1 and then add another Image to Page2. I have the pages created as I want but I am unable to insert the image into the page. Below is my code
sub InsertImage()
Dim Doc As Object
Dim Page1 as Object
Dim Page2 as Object
Dim DocPath1 as String
DocPath1 = ConvertToURL("MyImage1.jpg")
Dim DocPath2 as String
DocPath2 = ConvertToURL("MyImage2.jpg")
Dim noArgs() 'An empty array for the arguments
Dim sURL As String
sURL = "private:factory/sdraw"
Doc = StarDesktop.LoadComponentFromUrl(sURL, "_blank", 0, noArgs())
Page1 = Doc.DrawPages(0)
Page1.Name = "Image1"
Page2 = Doc.Drawpages.insertNewByIndex(2)
Page2.Name = "Image2"
' Page1.FillStyle = com.sun.star.drawing.FillStyle.BITMAP
' Page1.FillBitmapURL = DocPath1
End sub
I have been reading Andrew Pitonyak's book but unable to find a source for what I am trying to do. FillStyle breaks the code.
I have got the program to work.
I created two pdf's and saved them in a folder. I run the macro MergePDF and the sub opens Draw, creates the two pages, copes the pdf's onto each page. The document is ready to ExportToPDF which is my next task. Draw must be closed for this to work.
sub MergePDF()
Dim Doc As Object 'This workbook
Dim NewWorkBookURL As String
NewWorkBookURL = "private:factory/sdraw"
Dim noArgs() 'An empty array for the arguments
Dim Point As New com.sun.star.awt.Point
Dim Size As New com.sun.star.awt.Size
Point.x = 0
Point.y = 0
'A4
Size.Width = 21000
Size.Height = 29700
Dim Page1 As Object 'Excel sheet
Dim Page2 As Object 'AutoCAD sheet
Dim Image1 As Object 'PDF1
Dim Image2 As Object 'PDF2
Dim DocPath1 As String
Dim DocPath2 As String
Dim DocPath3 As String
DocPath1 = ConvertToURL("C:\Users\pdf1.pdf")
DocPath2 = ConvertToURL("C:\Users\\pdf2.pdf")
Doc = StarDesktop.LoadComponentFromUrl(NewWorkBookURL, "_blank", 0, noArgs())
Page1 = Doc.DrawPages(0)
Page1.Name = "PDF1"
Page2 = Doc.Drawpages.insertNewByIndex(2)
Page2.Name = "PDF2"
'Page 1
Image1 = Doc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Image1.GraphicURL = DocPath1
Image1.Size = Size
Image1.Position = Point
Page1.add(Image1)
'Page 2
Image2 = Doc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Image2.GraphicURL = DocPath2
Image2.Size = Size
Image2.Position = Point
Page2.add(Image2)
'ExportToPDF
'To Do
msgbox "Done"
End sub
Very basic but works. Thanks for the other forum members for the advice on the way.

Itext How to set both UTF encoding and Font to a Paragraph

I want to set a font to a text that is in cyrillic.I successfully convert the text to cyrilic, but i cannot set a Font to the same text.
File fontFile = new File("arialuni.ttf");
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
document.open();
writer.getAcroForm().setNeedAppearances(true);
Font boldFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
Font normalFont = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.ITALIC);
BaseFont unicode = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
FontSelector fs = new FontSelector();
fs.addFont(new Font(unicode));
addContent(document,article.getTitle(),fs,boldFont);
private static void addContent(Document document,String paragraph,FontSelector fs,Font font) throws DocumentException {
Phrase phrase = fs.process(paragraph);
Paragraph p = new Paragraph(phrase.toString(),font);
document.add(p);
}
As #mkl indicates in the comments, you are mixing FontSelector functionality that gives you a Phrase that could use the appropriate unicode fonts (fonts with BaseFont.IDENTITY_H as encoding parameter), with creating a Paragraph with a simple font (Font.FontFamily.TIMES_ROMAN).
When you do fs.process(paragraph), you get a Phrase in which every Chunk has the correct font, but when you do phrase.toString(), you throw away all those fonts, and you replace them with Font.FontFamily.TIMES_ROMAN. That doesn't make any sense.
Why don't you replace this:
Phrase phrase = fs.process(paragraph);
Paragraph p = new Paragraph(phrase.toString(),font);
document.add(p);
with:
document.add(fs.process(paragraph));
Why does your addContent() method need a Font as parameter? Also, if you really need a Paragraph object you can also do this:
Paragraph p = new Paragraph();
p.add(fs.process(paragraph));
document.add(p);
Or even:
Paragraph p = new Paragraph(fs.process(paragraph));
document.add(p);
As long as you don't replace the correct fonts with the incorrect fonts by "flattening" the Phrase to a String, you're probably OK.
Note that you probably even don't need the FontSelector. There's nothing wrong with doing this:
BaseFont unicode = BaseFont.createFont(
fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(unicode, 12);
Paragraph p = new Paragraph(paragraph, font);
It seems to me that you are making things unnecessary complex.

Header with 2 cells aligned in the same row, itextpdf 5.5.10

I have a row in my header, which looks good, but I need to add another String in the same row, so it is on the right side of it
When I try to add that String in the same cell, everything loses the alignment
private void addHeader(PdfWriter writer) {
PdfPTable tableHeader = new PdfPTable(2);
try {
// set defaults
header.setWidths(new int[] { 20, 12, 12 });
header.setTotalWidth(527);
header.setLockedWidth(true);
header.getDefaultCell().setFixedHeight(5);
header.getDefaultCell().setBorder(Rectangle.BOTTOM);
header.getDefaultCell().setBorderColor(BaseColor.LIGHT_GRAY);
// add text
PdfPCell text = new PdfPCell();
text.setPaddingBottom(12);
text.setPaddingLeft(8);
text.setBorder(Rectangle.BOTTOM);
text.setBorderColor(BaseColor.LIGHT_GRAY);
text.addElement(new Phrase("Hi", new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD)));
text.addElement(new Phrase("", new Font(Font.FontFamily.HELVETICA, 8)));
text.addElement(new Phrase("Hi" + "CVE-2017-2018", new Font(Font.FontFamily.HELVETICA, 9)));
header.addCell(text);
// add image
Image logo = Image.getInstance(App.class.getResource(LOGO_2));
header.addCell(logo);
PdfPCell cveTitle = new PdfPCell();
cveTitle.setPaddingBottom(15);
cveTitle.setPaddingLeft(8);
cveTitle.setBorder(Rectangle.BOTTOM);
cveTitle.setBorderColor(BaseColor.LIGHT_GRAY);
cveTitle.addElement(new Phrase("3 Cell", new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD)));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
header.addCell(cveTitle);
}
CVE-2010-2016
Is just what I want to do, leave it on the right side
Update
The solution was to add a new value to the array, corresponding to a new cell, and also instantiating PdfPCell again, thks
Just from the classname PdfPTable, I can tell the iText version you are using is pretty outdated.
Please consider switching to a newer version. Latest version is iText7.
I think what you're looking for is colspan and rowspan.
Have a look at the tutorial:
http://developers.itextpdf.com/examples/tables/clone-colspan-and-rowspan

positioning text using itextSharp in C3

I am trying to generate a pdf on button click. I am having challenge to design the page. If anybody can help on, how to position the text in particular position.
Say I want my address to in the top right corner and heading in the center.
Here is the code I am working on:
private void pdf_btn_Click(object sender, EventArgs e)
{
SaveFileDialog svg = new SaveFileDialog();
svg.ShowDialog();
using (FileStream stream = new FileStream(svg.FileName + ".pdf", FileMode.Create))
{
// Create a Document object
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWrite object, writing the output to a MemoryStream
// var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, stream);
document.Open();
// First, create our fonts... (For more on working w/fonts in iTextSharp, see: http://www.mikesdotnetting.com/Article/81/iTextSharp-Working-with-Fonts
var titleFont = FontFactory.GetFont("Arial", 18, Convert.ToInt32(Font.Bold));
var AddressFont = FontFactory.GetFont("Arial", 7, Convert.ToInt32(Font.Bold));
var boldTableFont = FontFactory.GetFont("Arial", 12, Convert.ToInt32(Font.Bold));
var endingMessageFont = FontFactory.GetFont("Arial", 10, Convert.ToInt32(Font.Italic));
var bodyFont = FontFactory.GetFont("Arial", 12);
// Add the "" title
document.Add(new Paragraph("Certificate of Analysis", titleFont));
// Add Address
var text = document.Add(new Paragraph("Abc Company", AddressFont));
document.Add(new Paragraph("Tel:(xx) xxx-xxxx, (xxx) xxx-xxx", AddressFont));
document.Add(new Paragraph("Fax: (xxx) xxx-xxx, , AddressFont));
document.Close();
stream.Close();
}
}
There are different ways to achieve what you want.
Currently, you are telling iText to do the layout. If you want to center the text "Certificate of Analysis", then you could change the alignment of the Paragraph like this:
Paragraph header = new Paragraph("Certificate of Analysis", titleFont);
header.Alignment = Element.ALIGN_CENTER;
document.Add(header);
If you want the text "Abc Company" to be right-aligned, you can change he alignment like this:
Paragraph company = new Paragraph("Abc Company", AddressFont);
company.Alignment = Element.ALIGN_RIGHT;
document.Add(company);
Of course: when we add company, it will start on a new line under header. Maybe you want the text to be on the same line.
In that case, why not use a PdfPTable?
PdfPTable myTable = new PdfPTable(3);
myTable.WidthPercentage = 100;
PdfPCell mydate = new PdfPCell(new Phrase("April 22, 2015", myfont));
mydate.Border = Rectangle.NO_BORDER;
myTable.AddCell(mydate);
PdfPCell header = new PdfPCell(new Phrase("Certificate of Analysis", titleFont));
header.Border = Rectangle.NO_BORDER;
header.HorizontalAlignment = Element.ALIGN_CENTER;
myTable.AddCell(header);
PdfPCell address = new PdfPCell(new Phrase("Company Address", AddressFont));
address.Border = Rectangle.NO_BORDER;
address.HorizontalAlignment = Element.ALIGN_RIGHT;
myTable.AddCell(address);
doc.Add(myTable);
Now the available width of your page will be divided in three, the date will be aligned to the left, the header will be centered, and the address will be aligned to the right. Obviously, you can add more data to a PdfPCell and more rows to the PdfPTable. This is just a simple proof of concept.
Another option, is to add the text at an absolute position (using coordinates). This can be done by using the ColumnText object. There are plenty of examples on this in The Best iText Questions on StackOverflow

Using iTextSharp v4.1.2, how do I add an image to the header?

I can add a text header in iTextSharp v4.1.2 but how do I add an image instead of text? I am currently using a text header but I would like to replace it with an image.
' Create a Document object
Dim doc As Document = New Document(PageSize.LETTER)
doc.SetMargins(pageMarginLeft, pageMarginRight, pageMarginTopBottom, pageMarginTopBottom)
' Create a new PdfWriter object, specifying the output stream
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, HttpContext.Current.Response.OutputStream)
' Set header and footer
Dim headerFont As iTextSharp.text.Font = FontFactory.GetFont("Arial", 12, iTextSharp.text.Color.BLUE)
Dim header As New HeaderFooter(New Paragraph("VOLUNTEER MASTER SCHEDULE", headerFont), False)
Dim footerFont As iTextSharp.text.Font = FontFactory.GetFont("Arial", 8, iTextSharp.text.Color.BLACK)
Dim footer As New HeaderFooter(New Phrase("Page: ", footerFont), True)
doc.Header = header
doc.Footer = footer
' Open the Document for writing
doc.Open()