问题
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:
When I open the file with IcePDF programaticly it looks like thi:
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);
}
回答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.
来源:https://stackoverflow.com/questions/34104669/icepdf-special-character-rendering-issue