I am trying to add some text with a bar code in a table cell using itext as per the following code but it does not show in the pdf file. i tried adding chunks and paragraph. Any help on this would be appreciated.
Barcode128 barcode = new Barcode128();
//barcode.setCodeType(Barcode.EAN8);
barcode.setCode(code);
PdfPCell cell = new PdfPCell(barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY), true);
Paragraph paragraph = new Paragraph("Hello World");
cell.addElement(paragraph);
cell.setPadding(10);
You are probably confused by text vs. composite mode.
When using the PdfPCell(Image) constructor, you create a cell in text mode. Any subsequent call to addElement(Element) will then switch the cell to composite mode, removing all content previously entered in the constructor.
You'll have to change your code that way:
PdfPCell cell = new PdfPCell();
Barcode128 barcode = new Barcode128();
barcode.setCode(code);
Image barcodeImage = barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY);
cell.addElement(barcodeImage);
Paragraph paragraph = new Paragraph("Hello World");
cell.addElement(paragraph);
Related
I need to insert header like the screenshot below in word document using apache poi
I have a code to insert the header. But it aligned to left like the screenshot below:
I used below code to insert header:
// write header content
XWPFDocument docx = new XWPFDocument();
CTSectPr sectPr = docx.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx,sectPr);
CTP ctpHeader = CTP.Factory.newInstance();
CTR ctrHeader = ctpHeader.addNewR();
CTText ctHeader = ctrHeader.addNewT();
String headerText = "This is header";
ctHeader.setStringValue(headerText);
XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, docx);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
FileOutputStream out = new FileOutputStream("D:/giri.docx");
docx.write(out);
out.close();
System.out.println("Done");
XWPFHeader header=policy.getDefaultHeader();
CTP ctpHeader = CTP.Factory.newInstance();
CTR ctrHeader = ctpHeader.addNewR();
CTText ctheader = ctrHeader.addNewT();
String HeaderText = "TSS Word Documentsss";
//ctheader.set(HeaderText);
XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
XWPFRun headerRun= headerParagraph.createRun();
headerRun.setBold(true);
headerRun.setFontSize(39);
headerRun.setColor("808000");
headerRun.setImprinted(true);
headerRun.setShadow(true);
headerRun.setCapitalized(true);
headerRun.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
headerRun.setText(HeaderText);
headerParagraph.setAlignment(ParagraphAlignment.CENTER);
headerParagraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
Unlike Excel, Word doesn't have left, centre and right headers - Word has a header. If you want three separate entries on a single line, you can format a paragraph with the appropriate alignment (e.g. centred/justified) and tab-stops (possibly 3 centred, or one each of left, centre and right), then insert tab characters into the text you're inserting. Alternatively, for multi-line inputs especially, you could insert a 3-column table with the appropriate cell formatting and send the outputs to the relevant cells.
In VBA, you might add a table to the page header using code like:
With ActiveDocument
.Tables.Add Range:=.Sections.First.Headers(wdHeaderFooterPrimary).Range, NumRows:=1, NumColumns:=3, AutoFitBehavior:=wdAutoFitFixed
End With
I'll leave it to you to do the C# conversion.
I want to make a pdf report with English and Arabic texts. I have many tables/phrases across the page. I want to display Arabic text also along with English. I have seen the Arabic example in iText doxument also, using ColumnText. I couldn't help myself with that. My doubt is how to set canvas.setSimpleColumn(36, 750, 559, 780), the arguments in this method for tables/phrases at different positions. I have referred below questions also.Still I have issues.
Writing Arabic in pdf using itext,
http://developers.itextpdf.com/examples/font-examples/language-specific-exampleshe
Below is my code..
private static final String ARABIC = "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a";
private static final String FONT = "resources/fonts/ARIALUNI.TTF";
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
document.open();
Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfPTable table = new PdfPTable(3);
Phrase phrase = new Phrase();
Chunk chunk = new Chunk("test value", inlineFont);
phrase.add(chunk);
// I want to add Arabic text also here..but direction is not //correct.also coming as single alphabets
p.add(new Chunk(ARABIC, f));
PdfPCell cell1 = new PdfPCell(phrase);
cell1.setFixedHeight(50f);
table.addCell(cell1);
document.add(table);
document.close();
Your code is kind of sloppy.
For example:
you define a PdfPTable with 3 columns, but you only add a single cell. That table will never be rendered.
you define a Phrase with name phrase, but later in your code you use p.add(...). There is no variable with name p in your code.
...
This lack of respect for the StackOverflow reader can result in not getting an answer, because you are expecting the reader not only to fix the actual problem –not being able to use English and Arabic text in a single PdfPCell—, but also to fix all the other (avoidable) errors in your code.
This is a working example:
public static final String FONT = "resources/fonts/NotoNaskhArabic-Regular.ttf";
public static final String ARABIC = "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a";
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfPTable table = new PdfPTable(1);
Phrase phrase = new Phrase();
Chunk chunk = new Chunk("test value");
phrase.add(chunk);
phrase.add(new Chunk(ARABIC, f));
PdfPCell cell = new PdfPCell(phrase);
cell.setUseDescender(true);
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
table.addCell(cell);
document.add(table);
document.close();
}
The result looks like this:
As you can see, both the English and the Arabic text can be read fine. You may be surprised by the alignment and the order of the text. As we are working in the Right-to-Left writing system, left and right are switched. By default, text is left aligned, but as soon as we introduce the RTL run direction, this changes to right aligned.
In your code, you add the English text first, followed by the Arabic text. Text in Arabic is read from right to left. That's why you see the English text to the right, and why the Arabic text is added to the left of the English text.
All of this has been improved in iText 7. iText 7 has an extra pdfCalligraph module that takes care of other writing systems in a transparent way.
I am in the process of converting the contents page of my PDF from using page numbers as a hyperlink, to anchors because of a few circumstantial limitations and the linking needs to be more dynamic.
I have omitted outer-loop code, but I am attempting to create a hyperlinked entry in the contents page using the following:
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
Anchor anchor = new Anchor("page18 link");
anchor.setReference("#page18");
p.add(anchor);
cell.addElement(p);
table.addCell(cell);
Once the contents page has been generated (i.e. all the rows have been added), I then use the writeSelectedRows on the table:
table.writeSelectedRows(0, -1, PageSize.A4.getWidth()*.05f, PageSize.A4.getHeight()-100, stamper.getOverContent(prevSectionPageCount+currentIndexPage+1));
On doing this, I get the following exception:
Cause Exception was: Error in StamperPDFPlugin. null
java.lang.NullPointerException at
com.itextpdf.text.pdf.internal.PdfAnnotationsImp.addPlainAnnotation(PdfAnnotationsImp.java:125)
at com.itextpdf.text.pdf.PdfDocument.localGoto(PdfDocument.java:2115)
at
com.itextpdf.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:1612)
at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:1025) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:877) at
com.itextpdf.text.pdf.ColumnText.goComposite(ColumnText.java:1381) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:882) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:877) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:866) at
com.itextpdf.text.pdf.PdfPRow.writeCells(PdfPRow.java:549) at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:767)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:897)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:845)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:823)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.transform(StamperPDFPlugin.java:584)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.transform(StamperPDFPlugin.java:328)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.executeProfile(StamperPDFPlugin.java:171)
On seeing the stack trace entry for localGoto, I took out the line anchor.setReference("#18.pdf"); and it completed fine without error (but obviously with the absence of the hyperlinks - only plain text).
What is going wrong here? Am I adding the anchor to the cell incorrectly?
Thanks
Please take a look at LinkInPositionedTable:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Anchor target = new Anchor("top");
target.setName("page18");
document.add(target);
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(500);
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
Anchor anchor = new Anchor("page18 link");
anchor.setReference("#page18");
p.add(anchor);
cell.addElement(p);
table.addCell(cell);
table.writeSelectedRows(0, -1, 36, 700, writer.getDirectContent());
document.close();
}
In this example, I create an anchor with name page18 (although it just refers to the top of the page) and a reference to that anchor added to a PdfPTable using your code snippet.
You can find the result here: link_in_positioned_table.pdf
This works for me, when using iText 5.5.7 (which is the most recent version).
When having a PdfPTable in the header of the document, the first Chapter added gets added too page two, leaving the first page blank. If having just text in the header all works fine (see the out commented line in code sample). Am I doing something wrong or are there a workaround for this problem? I'm using iText-2.1.7.
So to be clear: The code below generates a pdf with one empty page as the first page and if going with the out commented row instead there is no empty page first.
Another thing is that if having a table in the header the generated header gets no height making the text of the document placed over the header table. That one I can work around. But it could perhaps give some help in understanding what's happening...
Document vDocument = new Document();
PdfWriter.getInstance(vDocument, new FileOutputStream("C:/Test.pdf"));
PdfPTable vTable = new PdfPTable(1);
vTable.addCell(new PdfPCell (new Phrase("Header text")));
Phrase vPhr = new Phrase();
vPhr.add(vTable);
HeaderFooter vHeaderFooter = new HeaderFooter(vPhr, false);
// HeaderFooter vHeaderFooter = new HeaderFooter(new Phrase("Header text"), false);
vDocument.setHeader(vHeaderFooter);
vDocument.open();
vDocument.add(new Chapter("New Chapter", 0));
for (int i=0; i<1000; i++) {
vDocument.add(new Paragraph(" TEXT " + i));
}
vDocument.close();
I need to position different text before I generate the pdf using IText. I've been thinking of using Chunks, but I don't know how to position them separately. I also tried using PdfContentByte but it doesn't generate any PDF File.
Why don't you use tables combined with Chunks for your layout. ex:
PdfPTable headerTable = new PdfPTable(2);
float[] headerTableWidths = { 80f, 20f };
headerTable.setWidthPercentage(100f);
headerTable.setWidths(headerTableWidths);
headerTable.getDefaultCell().setBorderWidth(0);
headerTable.getDefaultCell().setPadding(2);
headerTable.getDefaultCell().setBorderColor(BaseColor.BLACK);
headerTable.getDefaultCell().setFixedHeight(90f);
PdfPCell infoCell = new PdfPCell();
infoCell.setHorizontalAlignment(Element.ALIGN_CENTER);
infoCell.setVerticalAlignment(Element.ALIGN_TOP);
infoCell.addElement("test");
infoCell.addElement("text");
table.addCell(infoCell);