Tables
25
You’remixingtextmodeandcompositemode.
Intextmode,youcreatethe
PdfPCell
witha
Phrase
astheparameteroftheconstructor,andyou
definethealignmentatthe levelofthecell. However,you’reworking incomposite mode. This
modeistriggeredassoonasyouusethe
addElement()
method.Incompositemode,thealignment
definedatthelevelofthecellisignored(whichexplainsyourproblem).Instead,thealignmentof
theseparateelementsisused.
Howtosolveyourproblem?
Eitherworkintextmodebyaddingyour
Phrase
tothecellinadifferentway.Orworkincomposite
modeandusea
Paragraph
forwhichyoudefinethealignment.
Theadvantageof composite modeover text mode isthatdifferentparagraphsin thesamecell
canhavedifferentalignments, whereasyoucanonlyhaveonealignmentintextmode.Another
advantage is that you can add more than justtext: you can also add images, lists, tables,… An
advantageof textmodeisspeed:ittakeslessprocessingtimetodealwiththecontentofacell.
How to use multiple fonts in a single cell?
I’mmakingawindowsformforafriendthatdeliverspackages.SoIwanttotransferhis
currentpaperform,intoa.pdfwiththelibraryiTextSharp.
WhatIneed:
Iwantthetabletohavealittleheadline,“Companyname”forexample,thetextshouldbe
alittlesmallerthanthetextinputfromthewindowsform.CurrentlyI’musingcellsand
waswonderingifIcanuse2differentfontsizeswithinthesamecell?
WhatIhave:
table.AddCell("Static headline" + Chunk.NEWLINE + richTextBox1.Text);
WhatI“want”:
var normalFont = = FontFactory.GetFont(FontFactory.HELVETICA, 9);
var boldFont = = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);
table.AddCell("Static headline", boldFont + Chunk.NEWLINE + richTextBox1.Text, n\
ormalFont);
PostedonStackOverflowonFeb13,2014⁴³byFrederikKiel⁴⁴
⁴³
http://stackoverflow.com/questions/21750597/c-sharp-itextsharp-multi-fonts-in-a-single-cell
⁴⁴
http://stackoverflow.com/users/3300515/frederik-kiel
Convert pdf to multipage tiff - software application dll:C# PDF Convert to Tiff SDK: Convert PDF to tiff images in C#.net, ASP.NET MVC, Ajax, WinForms, WPF
Online C# Tutorial for How to Convert PDF File to Tiff Image File
www.rasteredge.com
Convert pdf to multipage tiff - software application dll:VB.NET PDF Convert to Tiff SDK: Convert PDF to tiff images in vb.net, ASP.NET MVC, Ajax, WinForms, WPF
Free VB.NET Guide to Render and Convert PDF Document to TIFF
www.rasteredge.com
Tables
26
You’repassinga
String
anda
Font
to the
AddCell()
method.That’snotgoingtowork.Youneed
the
AddCell()
methodthattakesa
Phrase
objectora
PdfPCell
objectasparameter.
A
Phrase
isanobjectthatconsistsofdifferent
Chunk
s,andthedifferent
Chunk
scanhavedifferent
fontsizes.Forinstance:
Phrase phrase = new Phrase();
phrase.Add(
new Chunk("Some BOLD text", new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.\
BOLD))
);
phrase.Add(new Chunk(", some normal text"new Font()));
table.AddCell(phrase);
A
PdfPCell
is an object to which you can add different objects, such as
Phrase
s,
Paragraph
s,
Image
s,…
PdfPCell cell = new PdfPCell();
cell.AddElement(new Paragraph("Hello"));
cell.AddElement(list);
cell.AddElement(image);
Inthissnippet
list
isoftype
List
and
image
isoftype
Image
.
The first snippet uses text mode; the second snippet uses composite mode. Cells behave very
differentlydependingonthemodeyouuse.
software application dll:C# TIFF: C#.NET Code to Split Multipage TIFF File
XDoc.Tiff ›› C# Tiff: Split Tiff. C# TIFF - Split Multi-page TIFF File in C#.NET. C# Guide for How to Use TIFF Processing DLL to Split Multi-page TIFF File.
www.rasteredge.com
software application dll:.NET Multipage TIFF SDK| Process Multipage TIFF Files
to work with .NET development environments, this Multipage TIFF Processing SDK on the Web, open and view TIFF files on to SharePoint and save to PDF documents.
www.rasteredge.com
Tables
27
How to introduce a rowspan?
IamaddingatabletoaPDFfile.Ihave3rowsand3columns.Iwantthefirstcolumn
toappearonlyonceasasinglecellforalltherows.Theresultshouldbelikewhere
itsaysDeloitteinthecolumnofcompanyasshownintheimage:
Tableshowingdesiredresult
PostedonStackOverflowonApr5,2014⁴⁵bysindhujampani⁴⁶
TheMyFirstTable⁴⁷examplefrommybookdoesexactlywhatyouneed.PortedtoC#,itlookslike
this:
PdfPTable table = new PdfPTable(3);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
// now we add a cell with rowspan 2
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.Rowspan = 2;
table.AddCell(cell);
// we add the four remaining cells with addCell()
table.AddCell("row 1; cell 1");
table.AddCell("row 1; cell 2");
table.AddCell("row 2; cell 1");
table.AddCell("row 2; cell 2");
⁴⁵
http://stackoverflow.com/questions/22878410/constant-column-for-multiple-rows
⁴⁶
http://stackoverflow.com/users/3191619/sindhu-jampani
⁴⁷
http://itextpdf.com/examples/iia.php?id=75
software application dll:C# TIFF: C# Code for Multi-page TIFF Processing Using RasterEdge .
process, convert, annotate, and save various image and document file formats. Most commonly, images and documents like Tiff, Jpeg, Bmp, Png, Gif, PDF, Word
www.rasteredge.com
software application dll:VB.NET Image: Multi-page TIFF Editor SDK; Process TIFF in VB.NET
imaging SDK owns rich APIs, using which developers can easily load, save, view, edit, annotate, manipulate, convert and compress source TIFF document image
www.rasteredge.com
Tables
28
YoucanlookattheresultingPDFhere⁴⁸.Inyourcaseyou’dneed
cell.Rowspan = 6;
ForthecellwithvalueDeloitte.
How to change width of single column of table?
helloIhavecreatedtableinaPDFfileusingiText.Theheadingofmytablecolumnsare
“MedicineName”,“Doses”,and“time:Thisiswhatmycolumnslooklike:
|Medicin|Doses|time|
| e name|
|
|
Asyoucanseetheword“Medicine”issplitinto“Medicin”and“e”.Iwanttoavoidthisby
changingthewidthofthefirstcolumn,butIdon’tknowhowtodothat.
PostedonStackOverflowonJun10,2014⁴⁹byDCoder⁵⁰
TheColumnWidths⁵¹exampledemonstratesdifferentwaysofchangingthewidthofacolumn.This
isonespecificway:
PdfPTable table new PdfPTable(3);
table.setWidths(new int[]{2, 1, , 1});
Nowthewidthofthefirstcolumnisdoublethesizeofthesecondandthirdcolumn.
⁴⁸
http://examples.itextpdf.com/results/part1/chapter04/first_table.pdf
⁴⁹
http://stackoverflow.com/questions/24141791/how-to-change-width-of-single-coloumn-of-table-itext-android
⁵⁰
http://stackoverflow.com/users/2843856/dcoder
⁵¹
http://itextpdf.com/examples/iia.php?id=76
software application dll:Process Multipage TIFF Images in Web Image Viewer| Online
Convert TIFF to other30+ formats supported by .NET imaging page TIFF image to a PDF; More image viewing & displaying functions. Multipage TIFF Processing.
www.rasteredge.com
software application dll:Process Multipage TIFF Images in .NET Winforms | Online Tutorials
Convert multipage TIFF files into other 30+ formats supported by Swap a Page in a Multipage TIFF Image. Tiff Processing; RasterEdge OCR Engine; PDF Reading; Encode
www.rasteredge.com
Tables
29
What is the
PdfPTable.DefaultCell
property used
for?
What is the
DefaultCell
property used for? The Java documentation for
PdfPTable.getDefaultCell()
reads:
Getsthedefault
PdfPCell
thatwillbeusedasreferenceforallthe
addCell
methodsexcept
addCell(PdfPCell)
.
Idon’tunderstandthis.
PostedonStackOverflowonJun3,2014⁵²byWaterCoolerv2⁵³
Whencreatinga
PdfPTable
,youaddcells.
Onewayistocreatea
PdfPCell
objectandtoaddthatcellwiththe
addCell()
method.
Anotherwayistouseashort-cut:youdon’tcreatea
PdfPCell
,butyouadda
String
ora
Phrase
tothetablewiththe
addCell()
method.Inthiscase,a
PdfPCell
iscreatedinternallyusingdefault
properties.Youcanchangethedefaultpropertiesbychangingthepropertiesofthedefaultcell.
Thedefaultcellisobtainedusingthe
getDefaultCell()
method.
This is what the Javadoc information is about: this default
PdfPCell
will be used as reference
forallthe
addCell()
methodsexcept
addCell(PdfPCell)
.(Becausewhenaddinga
PdfPCell
,the
propertiesofthat
PdfPCell
willbeused,notthepropertiesofthedefaultcell.)
How to draw a borderless table in iTextSharp?
It appearsasthough the
PDfPCell
class does have a border property on itbut not the
PdfPTable
class.
Istheresomepropertyonthe
PdfPTable
classtosetthebordersofallitscontainedcellsin
onestatement?
PostedonStackOverflowonJun3,2014⁵⁴byWaterCoolerv2⁵⁵
Bordersaredefinedatthelevelofthecell,notatthelevelofthetable.Hence:ifyouwanttoremove
thebordersofthetable,youneedtoremovethebordersofeachcell.
⁵²
http://stackoverflow.com/questions/24006618/what-is-the-pdfptable-defaultcell-property-used-for
⁵³
http://stackoverflow.com/users/303685/water-cooler-v2
⁵⁴
http://stackoverflow.com/questions/24006547/draw-a-borderless-table-in-itextsharp
⁵⁵
http://stackoverflow.com/users/303685/water-cooler-v2
software application dll:.NET PDF SDK | Read & Processing PDF files
Able to convert PDF documents into other formats (multipage TIFF, JPEG, etc); Multiple font types support, including TrueType, Type0, Type1, Type3 & OpenType;
www.rasteredge.com
software application dll:C# PDF Convert to Images SDK: Convert PDF to png, gif images in C#
NET control able to batch convert PDF documents to image Create image files including all PDF contents, like Turn multipage PDF file into single image files
www.rasteredge.com
Tables
30
Bydefault,eachcellhasaborder.Youcanchangethisdefaultbehaviorbychangingtheborderof
eachcell.Forinstance:ifyoucreate
PdfPCell
objects,youuse:
cell.setBorder(Rectangle.NO_BORDER);
Incasethecellsarecreatedinternally,youneedtochangethatpropertyatthelevelofthedefault
cell.
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
Forspecialborders,forinstanceborderswithroundedcornersorasingleborderforthewholetable,
ordoubleborders,youcanuseeithercelleventsortableevents,oracombinationofboth.
Why doesn’t
getDefaultCell()
.setBorder(PdfPCell.NO_BORDER)
have any effect?
I’m new with iText and I’m trying to build a table. For some reason
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER)
has no effect: my table
hasstillborders.
Hereismycode:
PdfPTable table new PdfPTable(new float[] { { 1, 1, , 1, 1, , 1 1 });
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
Font tfont new Font(Font.FontFamily.UNDEFINED, 10, Font.BOLD);
table.setWidthPercentage(100);
PdfPCell cell;
cell new PdfPCell(new Phrase("Menge", tfont));
table.addCell(cell);
cell new PdfPCell(new Phrase("Beschreibung", tfont));
table.addCell(cell);
cell new PdfPCell(new Phrase("Einzelpreis", tfont));
table.addCell(cell);
cell new PdfPCell(new Phrase("Gesamtpreis", tfont));
table.addCell(cell);
cell new PdfPCell(new Phrase("MwSt", tfont));
table.addCell(cell);
document.add(table);
DoyouhaveanyideawhatIamdoingwrong?
PostedonStackOverflowonNov30,2014⁵⁶byhiasl⁵⁷
⁵⁶
http://stackoverflow.com/questions/27212695/itext-5-getdefaultcell-setborderpdfpcell-no-border-has-no-effect
⁵⁷
http://stackoverflow.com/users/4308508/hiasl
Tables
31
Youaremixingtwodifferentconcepts.
Concept1:youdefineevery
PdfPCell
manually,forinstance:
PdfPCell cell new PdfPCell(new Phrase("Menge", tfont));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
Inthiscase,youdefineeveryaspect,everypropertyofthecellonthecellitself.
Concept2:youallowiTexttocreatethe
PdfPCell
implicitly,forinstance:
table.addCell("Adding a String");
table.addCell(new Phrase("Adding a phrase"));
Inthiscase,youcandefinepropertiesatthelevelofthedefaultcell.Thesepropertieswillbeused
internallywheniTextcreatesa
PdfPCell
inyourplace.
Conclusion:eitheryoudefinetheborderforallthe
PdfPCell
instancesseparately,oryouletiText
createthe
PdfPCell
instancesinwhichcaseyoucandefinetheborderatthelevelofthedefaultcell.
Ifyouchoosethesecondoption,youcanadaptyourcodelikethis:
PdfPTable table new PdfPTable(new float[] { { 1, , 1, , 1, 1, 1 });
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
Font tfont new Font(Font.FontFamily.UNDEFINED, 10, Font.BOLD);
table.setWidthPercentage(100);
table.addCell(new Phrase("Menge", tfont));
table.addCell(new Phrase("Beschreibung", tfont));
table.addCell(new Phrase("Einzelpreis", tfont));
table.addCell(new Phrase("Gesamtpreis", tfont));
table.addCell(new Phrase("MwSt", tfont));
document.add(table);
Thisdecisionwasmadebydesign,basedonexperience:itoffersthemostflexibletoworkwithcells
andproperties.
Table events
Wecontinuewithsomequestionsabouttablesofwhichtheanswerinvolvestableorcellevents.
How to use adotted line as a cell border?
Iamtryingtocreateatablewithcellsthathaveadottedlineforaborder.HowcanIdo
this?
PostedonStackOverflowonNov21,2013⁵⁸withuser1913695⁵⁹
I’vemadeanexamplethatsolvesyourproblem:DottedLineCell⁶⁰;TheresultingPDFisadocument
withtwotables:dotted_line_cell.pdf⁶¹
Forthefirsttable,weuseatableevent:
class DottedCells implements PdfPTableEvent {
@Override
public void tableLayout(PdfPTable tablefloat[][] widths,
float[] heights, int headerRows, int rowStart,
PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.setLineDash(3f, 3f);
float llx widths[0][0];
float urx widths[0][widths[0].length -1];
for (int = 0; heights.lengthi++) {
canvas.moveTo(llx, heights[i]);
canvas.lineTo(urx, heights[i]);
}
for (int = 0; widths.lengthi++) {
for (int = 0; widths[i].lengthj++) {
canvas.moveTo(widths[i][j], heights[i]);
canvas.lineTo(widths[i][j], heights[i+1]);
}
⁵⁸
http://stackoverflow.com/questions/20117321/dotted-line-for-cell-border
⁵⁹
http://stackoverflow.com/users/1913695/user1913695
⁶⁰
http://itextpdf.com/sandbox/tables/DottedLineCell
⁶¹
http://itextpdf.com/sites/default/files/dotted_line_cell.pdf
Tableevents
33
}
canvas.stroke();
}
}
Thisisthemostelegantwaytodrawthecellborders,asitusesonlyone
stroke()
operatorforall
thelines.Unfortunately,thissolutionisn’tanoptionifyouhavetableswithrowspans.
Thesecondtableusesacellevent:
class DottedCell implements PdfPCellEvent {
@Override
public void cellLayout(PdfPCell cellRectangle position,
PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.setLineDash(3f, 3f);
canvas.rectangle(position.getLeft(), position.getBottom(),
position.getWidth(), position.getHeight());
canvas.stroke();
}
}
Withacellevent,aborderisdrawnaroundeverycell.Thismeansyou’llhavemultiple
stroke()
operatorsandoverlappinglines.However:thissolutionalwaysworks,alsowhenthetablehascells
witharowspangreaterthanone.
How to create atable with rounded corners?
Ihavetocreateatablehavingroundedcorners,somethinglikeit:
Cellwithroundedborder
CanIdoitwithiTextSharp?
PostedonStackOverflowonMay14,2014⁶²byAndreaNobili⁶³
⁶²
http://stackoverflow.com/questions/23650957/how-to-create-a-rounded-corner-table-using-itext-itextsharp
⁶³
http://stackoverflow.com/users/1833945/andreanobili
Tableevents
34
Thisisdoneusingcellevents.
Makesurethatyoudon’taddany“automated”borderstothecell,butdrawthebordersyourselfin
acellevent:
table.DefaultCell.Border = PdfPCell.NO_BORDER;
table.DefaultCell.CellEvent = new RoundedBorder();
The
RoundedBorder
classwouldthenlooklikethis:
class RoundedBorder : IPdfPCellEvent {
public void CellLayout(PdfPCell cell, , Rectangle rect, PdfContentByte[] canvas)\
{
PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
cb.RoundRectangle(
rect.Left + 1.5f,
rect.Bottom + 1.5f,
rect.Width - 3,
rect.Height - 34
);
cb.Stroke();
}
}
Youcanofcoursefine-tunethevalues1.5,3and4togetdifferenteffects.
Documents you may be interested
Documents you may be interested