My application is developed in two different version English and Arabic.
I have created pdf table in iText with rowspan and colspan which is working perfectly in English version but in arabic version rowspan is not working.
when i'm using setRunDirection(3) to put arabic text in my table,the setRowspan(2) is not working.
please tell me how set rowspan value here.
please help me to solve this issue.
Thanks
This is part of code for table size 2 column and rowspan 2.
BaseFont base=BaseFont.createFont("C:/Windows/Font/arial.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font sourceFont= new Font(base, 9,Font.NORMAL,Color.RED);
PdfPTable tab1=new PdfPTable(2);
tab1.setRunDirection(3);//**(if you comment this line it will work)**
PdfPCell cells;
cells=new PdfPCell(new Paragraph("arabic text", sourceFont));
cells.setRowspan(2);
tab1.addCell(cells);
cells=new PdfPCell(new Paragraph("arabic text", sourceFont));
tab1.addCell(cells);
cells=new PdfPCell(new Paragraph("arabic text", sourceFont));
tab1.addCell(cells);
When I use table.setRunDirection(PdfWriter.RUN_DIRECTION_RTL) RowSpan is NotWorking!
so I Remove This Line And Add this:
PdfPCell title_cell= new PdfPCell(new Paragraph("متن عربی"));
title_cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
table.addCell(title_cell);
My Table have 6 column and 5 Rows. Right cell in First Row is rowspan 5.
My Code here:
PdfPTable table = new PdfPTable(new float[] {10,10,10,10,10,50});
table_mehr.setTotalWidth(PageSize.A4.getWidth()-30);
table_mehr.setLockedWidth(true);
table_mehr.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
PdfPCell title_cell = new PdfPCell(new Paragraph("متن عربی"));
title_cell.setRowspan(5);
title_cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
table.addCell(title_cell);
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
table.addCell("ali");
doc.add(table);
Related
I am using itextsharp for creating PDF . But when my content goes beyong 1 page it does not create and append text to next page. Below is my source code. Cannot identify whats goin wrong with this. Please guide.
Dim pdfTemplate As String = "C:\Program Files\mycrm\Documents\Client\Statement_.pdf"
Dim newFile As String = "D:\test.pdf"
Dim pdfReader As New PdfReader(pdfTemplate)
Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create))
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
pdfFormFields.SetField("[CLIENT NAME]", "siddhesh")
'For adding table
Dim PdfTable As New PdfPTable(2)
Dim PdfPCell As PdfPCell = Nothing
For column As Integer = 0 To 1
PdfPCell = New PdfPCell(New Phrase(New Chunk(column.ToString())))
PdfTable.AddCell(PdfPCell)
Next
For rows As Integer = 0 To 100
For column As Integer = 0 To 1
PdfPCell = New PdfPCell(New Phrase(New Chunk(rows.ToString() + column.ToString())))
PdfTable.AddCell(PdfPCell)
Next
Next
PdfTable.HeaderRows = 1
pdfStamper.FormFlattening = True
PdfTable.SetTotalWidth(New Single() {
(iTextSharp.text.PageSize.A4.Rotate().Height - 25) / 10,
(iTextSharp.text.PageSize.A4.Rotate().Height - 25) / 10
})
PdfTable.WriteSelectedRows(0, 50, 35, 460, pdfStamper.GetOverContent(1)) 'X Y départ en bas à gauche? plus yPos est au plus le texte est haut
PdfTable.CompleteRow()
pdfStamper.Close()
MsgBox("Exported")
PdfTable.WriteSelectedRows is documented as
/**
* Writes the selected rows to the document.
*
* #param rowStart the first row to be written, zero index
* #param rowEnd the last row to be written + 1. If it is -1 all the
* rows to the end are written
* #param xPos the x write coodinate
* #param yPos the y write coodinate
* #param canvas the <CODE>PdfContentByte</CODE> where the rows will
* be written to
* #return the y coordinate position of the bottom of the last row
*/
virtual public float WriteSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas)
You use
PdfTable.WriteSelectedRows(0, 50, 35, 460, pdfStamper.GetOverContent(1)) 'X Y départ en bas à gauche? plus yPos est au plus le texte est haut
Thus, you explicitly only draw the first 50 rows of your table over page 1 and completely ignore the remaining rows.
Your observation, therefore,
when my content goes beyong 1 page it does not create and append text to next page.
is exactly what is to be expected.
To draw all the rows, you can iterate and draw rows 50..99 over page 2, 100..149 over page 3, etc. PdfPTable has a property Size returning the number of rows in the table; you can use this to determine how often you have to loop.
If the document you loaded into your PdfReader does not have enough pages, you can add extra blank pages using PdfStamper.InsertPage.
I have a Table made of many PdfPcell like this:
enter code cell = New PdfPCell(New Phrase(txt0, myfontB))
cell.BackgroundColor = New BaseColor(System.Drawing.ColorTranslator.FromHtml("#f5f5f5"))
cell.PaddingTop = 15
cell.PaddingBottom = 15
cell.Border = 0
cell.Colspan = 2
tablegg.AddCell(cell)
cell = New PdfPCell(New Phrase(txt1, myfontN))
cell.BackgroundColor = New BaseColor(System.Drawing.ColorTranslator.FromHtml("#f5f5f5"))
cell.PaddingTop = 15
cell.PaddingBottom = 5
cell.Border = 0
cell.Colspan = 2
tablegg.AddCell(cell) here
My problem it's, where txt1 is long the PdfPcell is wrote directly next page, instead to continue part on first page and the rest on next page.
Any suggestion how to fix it?
Tks
V
I am using Itextsharp.
doc.SetMargins(36, 36, 36, 36);
doc.NewPage();
writer.PageEvent = new PDFPageEvent();
int horizontalMarginForColumn = 54;
int bottomMarginForColumn = 72;
int topMarginForColumn = 112;
int interval = 8;
Rectangle pageNumberLeftLocation = new Rectangle(horizontalMarginForColumn, doc.PageSize.Height - 36, horizontalMarginForColumn + 36, doc.PageSize.Height);
paragraph = new Paragraph();
paragraph.SpacingBefore = 0;
paragraph.Alignment = Element.ALIGN_LEFT;
line = new LineSeparator(1f, 100f, BaseColor.BLACK, 0, 0f);
line.Alignment = Element.ALIGN_CENTER;
paragraph.Add(new Chunk(line));
paragraph.Add(Chunk.NEWLINE);
chunk = new Chunk(" Index", FontFactory.GetFont("Arial", 15, Font.BOLD));
chunk.setLineHeight(23);
paragraph.Add(chunk);
paragraph.Add(Chunk.NEWLINE);
paragraph.Add(new Chunk(line));
doc.Add(paragraph);
writer.DirectContent.SetColorStroke(BaseColor.BLUE);
writer.DirectContent.Rectangle(pageNumberLeftLocation.Left, pageNumberLeftLocation.Bottom, pageNumberLeftLocation.Width, pageNumberLeftLocation.Height);
writer.DirectContent.Stroke();
The top margin of the page is 36, and the height of the rectangle is 36. So I assume that the bottom line of the rectangle and the beginning of the paragraph which is a black line, sholud be in the same vertical position. But you can see, there is some space between the two. Why is this? Thank you.
I use the following statement to create a Arial Narrow basefont with iText Sharp.
BaseFont arialn = BaseFont.CreateFont("c:\\windows\\fonts\\Arial-narrow_[allfont.net].ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
This is how I use where cb2 is the content byte and f_cn2 is the base font.
The output is not Arial Narrow.
cb2.AddTemplate(page, 0, 0);
cb2.BeginText();
cb2.SetFontAndSize(f_cn2, 10);
foreach (var xy in xylist)
{
cb2.ShowTextAligned(Element.ALIGN_LEFT, xy.Text, xy.xpos, xy.ypos, 0F);
}
cb2.EndText();
I am using iText5 (Java) to write a PDF which may contain Chinese characters. So I am using FontSelector to process the String and this works fine.
Now the problem is that if there are 2 strings
String str1 = "Hello Test1";
String str2 = "Hello Test2";
I need to write str1 witch Font Color = Blue and size = 10, whereas str2 with Font Color = Gray and size = 25.
I am not able to figure out how to achieve this using FontSelector.
Any help is appreciated.
That's easy. Here you have a code snippet that adds the Times Roman text in Blue and the Chinese text in Red:
FontSelector selector = new FontSelector();
Font f1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12);
f1.setColor(BaseColor.BLUE);
Font f2 = FontFactory.getFont("MSung-Light",
"UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED);
f2.setColor(BaseColor.RED);
selector.addFont(f1);
selector.addFont(f2);
Phrase ph = selector.process(TEXT);
In your case you need two FontSelectors.
FontSelector selector1 = new FontSelector();
Font f1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12);
f1.setColor(BaseColor.BLUE);
selector1.addFont(f1);
Phrase ph = selector1.process(str1);//First one
FontSelector selector2 = new FontSelector();
Font f2 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12);
f2.setColor(BaseColor.GRAY);
selector2.addFont(f2);
Phrase ph = selector2.process(str2);//Second one
you can do it in another way in my case i use 14 for header and 10 for data in tabular report
private Font fHeader;
private Font f1;
BaseFont bf = BaseFont.createFont(Constants.Settings.ARIAL_FONT, BaseFont.IDENTITY_H, true);
f1 = new Font(bf, 10);
fHeader= new Font(bf,14);
PdfPCell cell = new PdfPCell();
//for report header
cell = new PdfPCell(new Phrase(reportKingdomData + "\n" + departmentData + " " + username + " \n " + reportHeader + " \n ", fHeader));
//and for background color
cell .setBackgroundColor(new GrayColor(0.40f));//if 0.10f will be closer to black