2

I use itext library for creating PDF file because has very detailed rendering functions for PDF creation. When user click the button i write a template and fill the blank cells from DB everytime.

Than i use Icepdf library for show to user and taking output of the created pdf file.

But Icepdf has some character encoding problem i think. When PDf created and callled by Icepdf one of Turkish character looks as square. Turkish characters can be seen at this link. All characters rendered succesfully but eighth character at the picture is not.

When i go to filepath of created pdf file (created by itext library) and open it manually with Adobe Acrobat Reader all characters showing correctly. But if programaticly Icepdf open the file and show to user, eighth character at the picture looks as square.

I need change character encoding of Icepdf but i can't yet. Reading many articles about character and Font encoding of Icepdf but i have not yet succeeded. If i solve this character problem my application ready to deploy.

The generated PDF file can be downloaded here.

When I open this file with Adobe Acrobat it looks like this:

Adobe View

When I open the file with IcePDF programaticly it looks like thi:

IcePdf View

Also i read some questions and answers about this on Stackoverflow but none of them have an accepted answer/help.

Code used to create the file path:

File fUrl = new File(CreateAndViewPdf
                     .class
                     .getProtectionDomain()
                     .getCodeSource()
                     .getLocation()
                     .getPath()
            );

String path = fUrl
              .toString()
              .substring(0,fUrl.toString().lastIndexOf(File.separator))
              .replace("%20", " ") + "\\hello.pdf";

Code for the createPdf() method:

public void createPdf()throws DocumentException, IOException {
  BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, "ISO-8859-9", BaseFont.EMBEDDED);
  Document document = new Document(PageSize.A5);
  PdfWriter.getInstance(document, new FileOutputStream(path));
  document.setMargins(10, 10, 10, 10);
  document.setMarginMirroring(true);
  document.open();

  Font font = new Font( bf );
  PdfPCell cell;
  PdfPTable table = new PdfPTable(2);

  font = new Font( bf );
  font.setSize(15);
  font.setStyle("bold");
  cell = new PdfPCell(new Phrase("Sender\n"+"Information", font));
  cell.setPaddingBottom(7);
  cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  table.addCell(cell);

  font = new Font( bf );
  font.setSize(12);
  font.setStyle("normal");
  cell = new PdfPCell(new Phrase("â ç ğ ı İ î ö ş ü û\n\n"+"Â Ç Ğ I İ Î Ö Ş Ü Û",font));
  cell.setPaddingBottom(7);
  cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  table.addCell(cell);

  table.setWidths(new int[]{50,200});
  table.setWidthPercentage(100);
  table.setSpacingAfter(10);
  table.setSpacingBefore(10);

  document.add(table);
  document.close();
}

Code for the viewPdf() method:

public void viewPdf(String fileP)throws IOException, InterruptedException {
  String filePath = fileP;
  SwingController controller = new SwingController();
  SwingViewBuilder factory = new SwingViewBuilder(controller);
  JPanel viewerComponentPanel = factory.buildViewerPanel();
  controller.getDocumentViewController().setAnnotationCallback(
      new org.icepdf.ri.common.MyAnnotationCallback(
              controller.getDocumentViewController()));
  Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  CreateAndViewPdf.this.getContentPane().add(viewerComponentPanel);
  CreateAndViewPdf.this.setSize(new Dimension(screen.width/2,screen.height));
  CreateAndViewPdf.this.setLocationRelativeTo(null);
  controller.openDocument(filePath); 
}
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Black White
  • 700
  • 3
  • 11
  • 31
  • Did you embed the font? That is: did you embed it correctly? Which font did you use? Show us the actual PDF (not a screen shot of the PDF). – Bruno Lowagie Dec 05 '15 at 12:04
  • I added IcePdf viewer method to my question. How can i show PDF itself to you ? Should i transfer file to any free File Store site? – Black White Dec 05 '15 at 12:17
  • That's how most people do it: they put the file on Dropbox, Google Drive,... (preferably a service without too many ads and without any malware) and they make that file public so that other people on SO can download it. – Bruno Lowagie Dec 05 '15 at 12:35
  • File added to http://www.filedropper.com/hello_2 – Black White Dec 05 '15 at 12:38
  • @Mike 'Pomax' Kamermans File download link also added to question about 6 hours ago. Thanks. – Black White Dec 05 '15 at 19:36
  • 1
    Your code is not embedding the font in the PDF. This can be trivially verified using acrobat's "properties" -> "fonts" tab (which you should have checked). Acrobat has some application-embedded fonts that it'll fall back to, but any PDF viewer that only uses what's in the PDF will not be able to render the characters for which you didn't embed the font. The problem you want to solve here is not "my code doesn't work" but rather you need to look up how to properly embed the font you wanted to use. – Mike 'Pomax' Kamermans Dec 05 '15 at 20:58
  • So i see embeded font in File -> Properties "Fonts" tab. `BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, "ISO-8859-9", BaseFont.EMBEDDED);` in `createPdf()` method should embed the font to PDF correctly ? – Black White Dec 06 '15 at 11:08
  • HELVETICA is one of the standard 14 fonts which the pdf specification expects every pdf viewer to provide out of the box. Thus, iText will not embed it. BUT the specification merely expects a limited character set to be provided, and the characters missing in the sample pdf may well be beyond that character set. So please use a different, not standard 14 font and make sure it contains all the characters you intend to use. – mkl Dec 06 '15 at 12:06

1 Answers1

1

--- SOLVED ---

Firstly, thanks for the router comment to @Mike 'Pomax' Kamermans

After reading him/her comments begin investigating " how can i embed the spesific font file to PDF with iText " After 1-2 days later, i found solution just like below;

Going to Windows Control Panel\Appearance and Personalization\Fonts and copy the times.ttf (which tested all special characters supported font) file to my Java Application resources container.

Than i add below lines to top of my createPDF method.

Font fontBase = FontFactory.getFont("/resources/times.ttf",
                                     BaseFont.IDENTITY_H,
                                     BaseFont.EMBEDDED,
                                     0.8f, Font.NORMAL,
                                     BaseColor.BLACK);
BaseFont bf = fontBase.getBaseFont();

After this, viewPdf() method get the document to screen with all special characters truely rendering.

Black White
  • 700
  • 3
  • 11
  • 31