I would like to know how to define a default font style which will be assigned implicitly to all text elements but i don't know how.
I don't use JasperReport, only itext v 5.3.3 with the class PdfAWriter and elements like PdfPTable, PdfPCell, Paragraph, Phrase... I know how to embed font to insert paragraph or phrase but i didn't manage to define a default when i work with a PdfPtable : i have this type of error : "All the fonts must be embedded. This one isn't: Helvetica".
Here is my code, simplified to facilitate the analysis :
public class MainClass { public static Font BOLD; public static Font NORMAL; protected static BaseFont timesbd;
public static void main(String[] args) throws IOException, DocumentException { MainClass example = new MainClass(); example.createPdfA("c:/test/test.pdf"); }
public void createPdfA(String filename) throws IOException, DocumentException {
FontFactory.register("c:/NewYorker.ttf"); try { Font fontTest = FontFactory.getFont("new yorker","UTF-8",BaseFont.EMBEDDED); timesbd = fontTest.getBaseFont(); BOLD = new Font(timesbd, 12); NORMAL = new Font(times, 12);
}catch(Exception e) { e.printStackTrace(); }
File dir = new File("c:/test"); if (dir.exists() != true) { dir.mkdir(); }
Problem with PdfXConformanceException. Font related.
in Products
Posted
Dear phnxck,
I can't load the page with your example : it's not available any more : http://jasperforge.org/plugins/mantis/view.php?id=2819.
I would like to know how to define a default font style which will be assigned implicitly to all text elements but i don't know how.
I don't use JasperReport, only itext v 5.3.3 with the class PdfAWriter and elements like PdfPTable, PdfPCell, Paragraph, Phrase... I know how to embed font to insert paragraph or phrase but i didn't manage to define a default when i work with a PdfPtable : i have this type of error : "All the fonts must be embedded. This one isn't: Helvetica".
Here is my code, simplified to facilitate the analysis :
public class MainClass
{
public static Font BOLD;
public static Font NORMAL;
protected static BaseFont timesbd;
public static void main(String[] args) throws IOException, DocumentException
{
MainClass example = new MainClass();
example.createPdfA("c:/test/test.pdf");
}
public void createPdfA(String filename) throws IOException, DocumentException {
FontFactory.register("c:/NewYorker.ttf");
try
{
Font fontTest = FontFactory.getFont("new yorker","UTF-8",BaseFont.EMBEDDED);
timesbd = fontTest.getBaseFont();
BOLD = new Font(timesbd, 12);
NORMAL = new Font(times, 12);
}catch(Exception e)
{
e.printStackTrace();
}
File dir = new File("c:/test");
if (dir.exists() != true)
{
dir.mkdir();
}
try {
Path path = Paths.get("c:/test/test.pdf");
if (path != null) Files.createDirectories(path.getParent());
}catch (IOException e) {
e.printStackTrace();
}
Document document = new Document();
PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_1B);
document.open();
float[] columnWidths = { 3.5f, 4.5f, 2.8f };
PdfPTable table = new PdfPTable(columnWidths);
PdfPCell defaultCell = table.getDefaultCell();
defaultCell.setPhrase(new Phrase("",NORMAL));
table.setWidthPercentage(90);
table.setHorizontalAlignment(Element.ALIGN_RIGHT);
PdfPCell cell1 = new PdfPCell(new Phrase("test 1",BOLD));
cell1.setBorder(0);
cell1.setIndent(27);
cell1.setLeading(4.5f, 1);
PdfPCell cell2 = new PdfPCell(new Phrase("test 2",NORMAL));
cell2.setBorder(0);
cell2.setIndent(27);
cell2.setLeading(4.5f, 1);
PdfPCell cell3 = new PdfPCell(new Phrase("test 2",FONT_NORMAL));
cell3.setBorder(0);
cell3.setIndent(27);
cell3.setLeading(4.5f, 1);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
List chunks = table.getChunks();
for(Chunk chunk : chunks)
{
if(!chunk.getFont().getBaseFont().isEmbedded())
{
chunk.setFont(NORMAL);
}
}
document.add(table);
document.close();
}
}