0
私の質問は2倍です。iTextg(アンドロイド用)はテーブルをレンダリングせずにフォント設定を無視します
最初に、Paragraphオブジェクトを作成し、テキストとカスタムFontオブジェクトを追加すると、Fontオブジェクトは完全に無視され、テキストには影響しません。
第2に、私はPdfTableを作成するときにテストするためにただ1つの行を追加すると、全くレンダリングされません。
ところで、このコードの90%は、hereとhereのチュートリアルに由来していますが、すべて正のフィードバックがありました。ここでは完全なコードです:
PdfPTable table;
private void createPdf() throws FileNotFoundException, DocumentException {
Font bfBold12 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 25, Font.BOLD, new BaseColor(0, 0, 0));
Font bf12 = new Font(Font.FontFamily.TIMES_ROMAN, 12);
File pdfFolder = new File(Environment.getExternalStorageDirectory()+ "/pdfdemo");
if (!pdfFolder.exists()) {
pdfFolder.mkdirs();
//Log.i(LOG_TAG, "Pdf Directory created");
}
//Create time stamp
Date date = new Date() ;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);
myFile = new File(pdfFolder, "testPDF.pdf");
OutputStream output = new FileOutputStream(myFile);
//Step 1
Document document = new Document();
document.setPageSize(PageSize.LETTER);
//Step 2
PdfWriter.getInstance(document, output);
//Step 3
document.open();
Paragraph top = new Paragraph("Quotation");
top.setAlignment(Element.ALIGN_CENTER);
top.setFont(titleFont);//completely ignored
document.add(top);
Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph p = new Paragraph("Text to the left");
p.add(new Chunk(glue));
p.add("Text to the right");
document.add(p);
//specify column widths
float[] columnWidths = {1.5f, 6f, 2f, 2f, 2f, 2f};
//create PDF table with the given widths
table = new PdfPTable(columnWidths);
// set table width a percentage of the page width
table.setWidthPercentage(90f);
insertCell("Item No.", Element.ALIGN_CENTER, 1, bfBold12);
insertCell("Description", Element.ALIGN_CENTER, 1, bfBold12);
insertCell("Qty", Element.ALIGN_CENTER, 1, bfBold12);
insertCell("Discount(%)", Element.ALIGN_CENTER, 1, bfBold12);
insertCell("Unit Price", Element.ALIGN_CENTER, 1, bfBold12);
insertCell("Line Total", Element.ALIGN_CENTER, 1, bfBold12);
table.setHeaderRows(1);
document.add(table);
document.close();
}
private void insertCell(String text, int align, int colspan, Font font){
//create a new cell with the specified Text and Font
PdfPCell cell = new PdfPCell(new Phrase(text.trim(), font));
//set the cell alignment
cell.setHorizontalAlignment(align);
//set the cell column span in case you want to merge two or more cells
cell.setColspan(colspan);
//in case there is no text and you wan to create an empty row
if(text.trim().equalsIgnoreCase("")){
cell.setMinimumHeight(10f);
}
//add the call to the table
table.addCell(cell);
}
そして、これが出力されます:あなたは、フォントを設定した後、段落にテキストを追加していないため
http://developers.itextpdf.comの公式ドキュメントの何が間違っていますか?なぜサードパーティの文書を好むのですか?私は公式の文書のほとんどの著者であり、私が何時間も、何日間、何週間も何ヶ月も間違っていたかを知りたいので、私は文書を書いています。 –
私は謝罪しています。私はAndroid(iTextG)に特有の例を探していました。単純なJavaだけでなく、他のリンクも、あなたの*素晴らしい*ライブラリを発見したものです。実装した後で(そして、何か他の公式のドキュメンテーションに行く)、私はサンプルが(iTextGとiText 5/7の間で)どのように交換可能であるかを認識しました。あなたのサイトでプラットフォーム固有の例を見つけるのはいいでしょう – BiGGZ