I’m trying to do some simple formatting like underlining and font color etc, but it doesn’t seem to be working. I think im not understanding how to apply styles to the text, if someone could let me know where I’m going wrong, that would be fantastic.
I have a function that returns a byte array of the pdf, and it works, but the text is unformatted.
Below is the meat of the code:
com.aspose.pdf.elements.Pdf pdf1 = new com.aspose.pdf.elements.Pdf();
//add a section
com.aspose.pdf.elements.Section sec1 = pdf1.getSections().add();
//add a table
com.aspose.pdf.elements.Table table = new com.aspose.pdf.elements.Table(sec1);
String widths = “150 120 200”;
table.setColumnWidths(widths);
//add a row
com.aspose.pdf.elements.Row row1 = new com.aspose.pdf.elements.Row(table);
row1.getDefaultCellTextInfo().setTextColor(com.aspose.pdf.elements.Color.Green);
table.getRows().add(row1);
com.aspose.pdf.elements.Cell cell11 = new com.aspose.pdf.elements.Cell(row1, “text”);
com.aspose.pdf.elements.Cell cell12 = new com.aspose.pdf.elements.Cell(row1,“text”);
com.aspose.pdf.elements.Cell cell13 = new com.aspose.pdf.elements.Cell(row1,“text”);
row1.getCells().add(cell11);
row1.getCells().add(cell12);
row1.getCells().add(cell13);
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
//doc.save(baos, SaveFormat.DOC);
pdf1.save(baos);
return baos.toByteArray();
I've tested the code snippet that you've shared and I'm unable to notice the problem with the attached latest hotfix of Aspose.Pdf for Java. I've also attached the HotFix and the resultant PDF document. Please take a look.
[Java]
com.aspose.pdf.elements.Pdf pdf1 = new com.aspose.pdf.elements.Pdf(); //add a section com.aspose.pdf.elements.Section sec1 = pdf1.getSections().add(); //add a table com.aspose.pdf.elements.Table table = new com.aspose.pdf.elements.Table(sec1); String widths = "150 120 200"; table.setColumnWidths(widths);
//Add the table object in the paragraphs collection of the section sec1.getParagraphs().add(table);
//add a row com.aspose.pdf.elements.Row row1 = new com.aspose.pdf.elements.Row(table); row1.getDefaultCellTextInfo().setTextColor(com.aspose.pdf.elements.Color.Green); table.getRows().add(row1); com.aspose.pdf.elements.Cell cell11 = new com.aspose.pdf.elements.Cell(row1, "text"); row1.getDefaultCellTextInfo().setTrueTypeItalic(true); com.aspose.pdf.elements.Cell cell12 = new com.aspose.pdf.elements.Cell(row1,"text in Italic"); // when a new TextInfo is provided, the previously set information is over written row1.getDefaultCellTextInfo().setUnderLine(true); com.aspose.pdf.elements.Cell cell13 = new com.aspose.pdf.elements.Cell(row1,"text in UnderLine and Italic"); row1.getCells().add(cell11); row1.getCells().add(cell12); row1.getCells().add(cell13);
FileOutputStream out = new FileOutputStream(new File("d:/pdftest//TextInGreen_InsideTableCell..pdf")); pdf1.save(out);