Content invisible using Itext in Java - java

I'm using the iText library with Java and I have an issue, let me explain:
I have a Word file converted into a PDF and i want to add a table at the end of the document, this works perfectly except when the Word file hasn’t active the feature "No Color" at the tab "Page Layout", the table adds it as "invisible".
I've already tried to change the property Font from the Chunk object that created the content, but it does not work, the table continues showing as "invisible".
How I can solve this
The following code is the one that I use to create the table:
Font font = new Font(1,fontSize, Font.BOLD);
font.setColor(0,0,0);
Chunk name = new Chunk(v_emp.getNombre(),font);
Chunk tipo = new Chunk(v_emp.getDireccion(),font);
Paragraph paragraph = new Paragraph();
paragraph.add(tipo);
paragraph.add(new Chunk("\n"));
paragraph.add(name);
PdfPCell cell1 = new PdfPCell(paragraph);
cell1.setPadding(2);
table.addCell(cell1);

Try selecting the table in PDF with your mouse and see if the table is there and still it is invisible.
If it is there, go to the file menu and do print as pdf.

Related

PDFBox - Setting Cover Sheet for PDF Portfolio

I am creating a PDF portfolio from scratch using PDFBox 2.0.16. I create the PDF and add attachments. Once the attachments are added, I then create the PDF Portfolio. The following is the code to create the PDF Portfolio/Package.
COSDictionary collectionDictionary = new COSDictionary();
collectionDictionary.setName( COSName.TYPE, "Collection" );
collectionDictionary.setName( "PDF Filename", "T" );
PDDocumentCatalog catalog = pdfDoc.getDocumentCatalog();
catalog.getCOSObject().setItem( "Collection", collectionDictionary );
pdfDoc.save( pdfFile );
When I open the PDF, the Cover Sheet is a blank page. I would like the Cover Sheet to be the first attachment in the PDF Portfolio/Package so that when a user opens the PDF, the 1st attachment in the portfolio is viewed.
What somewhat worked for me (it positioned to this file, as if I had clicked on it in the list) is this:
collectionDictionary.setString(COSName.D, "Attachment 1");
where "Attachment 1" is the name used in the name tree in this example.

Add HTML Markup using java Apache PDFBOX

I have been using PDFBOX and EasyTable which extends PDFBOX to draw datatables. I have hit a problem whereby I have a java object with a string of HTML data that I need to be added to the PDF using PDFBOX. A dig at the documentation seems not to bear any fruits.
The code below is a snippet hello world, which I want on the pdf been generated to have H1 formatting.
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage( page );
// Create a new font object selecting one of the PDF base fonts
PDFont font = PDType1Font.HELVETICA_BOLD;
// Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( 100, 700 );
contentStream.drawString( "<h1>HelloWorld</h1>" );
contentStream.endText();
// Make sure that the content stream is closed:
contentStream.close();
// Save the results and ensure that the document is properly closed:
document.save( "Hello World.pdf");
document.close();
}
Use jerico to format the html to free text while mapping correctly the output of tags.
sample
public String extractAllText(String htmlText){
return new net.htmlparser.jericho
.Source(htmlText)
.getRenderer()
.setMaxLineLength(Integer.MAX_VALUE)
.setNewLine(null)
.toString();
}
Include on your gradle or maven:
compile group: 'net.htmlparser.jericho', name: 'jericho-html', version: '3.4'
PDFBox does not know HTML, at least not for creating content.
Thus, with plain PDFBox you have to parse the HTML yourself and derive special text drawing characteristics from the tags text is in.
E.g. when you encounter "<h1>HelloWorld</h1>", you have to extract the text "HelloWorld" and use the information that it is in a h1 tag to select an appropriate prime header font and font size to draw that "HelloWorld".
Alternatively you can look for a library doing that HTML parsing and transforming to PDF text drawing instructions for PDFBox, e.g. Open HTML to PDF.

PDFTable Itext arabic

I have coded java code and I wanted Arabic words to be displayed at PdfPTable which was asses to itext document to create PDF document
as attached picture "???" is Arabic code '
PdfPTable header = new PdfPTable(6);
PdfPTable tbame = new PdfPTable(1);
tbame.addCell(" >>>>>> " + install.getCustId().getFullName() + " <<<<<<");
tbame.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
tbame.setLockedWidth(false);
tbame.setExtendLastRow(false);
tbame.setWidthPercentage(100);
header.addCell("End");
header.addCell("Start");
Please read the documentation and you'll find out that the addCell(String content) method can not be used to add Arabic text for two reasons:
When you use this method, the default font Helvetica is used. You need to use a font that knows how to draw Arabic shapes. This is explained in the answer to this question: Itext Arabic Font coming as question marks
Arabic is written from right to left, which means that you need to change the run direction of the content of the cell as is explained in my answer to this question: RTL not working in pdf generation with itext 5.5 for Arabic text
A code snippet:
BaseFont bf = BaseFont.createFont("c:/WINDOWS/Fonts/arialuni.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
Phrase phrase = new Phrase(
"\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628", font);
PdfPCell cell = new PdfPCell(phrase);
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
table.addCell(cell);
If you don't have access to the font arialuni.ttf, you'll have to find another font that contains Arabic glyphs.

Paragraph leading inside table cell

I have a table with cells set up like this:
Paragraph p = new Paragraph(content,font);
p.setLeading(30);
PdfPCell c = new PdfPCell(p);
My problem is that the paragraph leading is ignored. Can someone please tell me how to set the paragraph leading when inside a table cell? It works perfectly when not inside a cell.
Thanks.
Google for the difference between "text mode" and "composite mode".
You are using "text mode": the leading of the cell is taken into
account; the leading of the paragraph is ignored.
If you use "composite mode", it's the other way round.
Try:
PdfPCell c = new PdfPCell();
c.addElement(p);

Make AcroFields(iTextSharp) are non editable and set bold

I'm using iTextSharp to populate the data to PDF Templates, which is created in OpenOffice.
it populating fine, I'm getting proper PDF, But that is coming editable mode. I want non-editable PDF. And also make some rows BOLD( by Program). below is my snippet code.
PdfStamper stamper = new PdfStamper(reader, outputStream);
AcroFields fields = stamper.getAcroFields();
//loop
fields.setField("Desc_", "HILINSKI, MARK");
Please help me.
Thanks.
If you don't want to form to be editable, use form flattening as is done in the FillDataSheet example. Add this to your code:
fields.setGenerateAppearances(true);
stamper.setFormFlattening(true);
If you want to change the font of specific fields, use the setFieldProperty() method to change the "textfont" as is done in the TextFieldFonts example:
BaseFont bold =
BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
form.setFieldProperty("Desc_", "textfont", bold, null);
For more info, read the official documentation.

Categories