第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

如何使用 itext 在 pdf 的頁腳上添加頁碼,它應(yīng)該照顧它的寬度?

如何使用 itext 在 pdf 的頁腳上添加頁碼,它應(yīng)該照顧它的寬度?

PIPIONE 2023-06-14 16:25:22
我的代碼示例如下    @Override    public void onEndPage(PdfWriter writer, Document document) {        addFooter(writer);    }    private void addFooter(PdfWriter writer){        PdfPTable footer = new PdfPTable(2);        try {            // set defaults            footer.setWidths(new int[]{2, 24});            footer.setWidthPercentage(50);            footer.setTotalWidth(527);            footer.setLockedWidth(true);            footer.getDefaultCell().setFixedHeight(30);            footer.getDefaultCell().setBorder(Rectangle.TOP);            footer.getDefaultCell().setBorderColor(BaseColor.RED);            // here for the text Page 100 of, word of goes below in next line.             //It should be in same line.            footer.addCell(new Phrase(String.format("Page %d of", Integer.parseInt(writer.getPageNumber()+"33")), new Font(Font.FontFamily.HELVETICA, 8)));            // add placeholder for total page count            PdfPCell totalPageCount = new PdfPCell(total);            totalPageCount.setBorder(Rectangle.TOP);            totalPageCount.setBorderColor(BaseColor.GREEN);            footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);            footer.addCell(totalPageCount);            // write page            PdfContentByte canvas = writer.getDirectContent();            canvas.beginMarkedContentSequence(PdfName.ARTIFACT);            footer.writeSelectedRows(0, -1, 34, 20, canvas);            canvas.endMarkedContentSequence();        } catch(DocumentException de) {            throw new ExceptionConverter(de);        }    }}此代碼在個(gè)位數(shù)頁碼的情況下工作正常。但是當(dāng)頁面增加到兩位數(shù)時(shí),很少有文本會轉(zhuǎn)移到下一行。例如,如果文本是第 10 頁,共 10 頁,則“of”一詞會出現(xiàn)在下一行。
查看完整描述

1 回答

?
千巷貓影

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

頁腳表的第一列非常小。實(shí)際上太小以至于較大的頁碼會自動導(dǎo)致?lián)Q行。因此,通過替換為第一列提供更多空間

footer.setWidths(new?int[]{2,?24});

例如

footer.setWidths(new?int[]{2,?20});

在評論中,您對擴(kuò)大第一欄的提議做出了回應(yīng)

但即使將其更改為僅多一點(diǎn),也會增加不必要的空間。它給出的輸出類似于第 10 頁,共 10 頁。我的意思是在“第 10 頁”之后,還有更多的空間。

為了減少差距,您可以右對齊第一列的內(nèi)容,例如通過添加

footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);

在添加"Page %d of"單元格之前。

因此,

private void addFooterImproved(PdfWriter writer) {

? ? PdfPTable footer = new PdfPTable(2);

? ? try {

? ? ? ? // set defaults

? ? ? ? footer.setWidths(new int[]{2, 20});

? ? ? ? footer.setWidthPercentage(50);


? ? ? ? footer.setTotalWidth(527);

? ? ? ? footer.setLockedWidth(true);

? ? ? ? footer.getDefaultCell().setFixedHeight(30);

? ? ? ? footer.getDefaultCell().setBorder(Rectangle.TOP);

? ? ? ? footer.getDefaultCell().setBorderColor(BaseColor.RED);

? ? ? ? footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);

? ? ? ? footer.addCell(new Phrase(String.format("Page %d of", writer.getPageNumber()), new Font(Font.FontFamily.HELVETICA, 8)));


? ? ? ? // add placeholder for total page count

? ? ? ? PdfPCell totalPageCount = new PdfPCell(total);

? ? ? ? totalPageCount.setBorder(Rectangle.TOP);

? ? ? ? totalPageCount.setBorderColor(BaseColor.GREEN);

? ? ? ? footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);

? ? ? ? footer.addCell(totalPageCount);


? ? ? ? // write page

? ? ? ? PdfContentByte canvas = writer.getDirectContent();

? ? ? ? canvas.beginMarkedContentSequence(PdfName.ARTIFACT);

? ? ? ? footer.writeSelectedRows(0, -1, 34, 20, canvas);

? ? ? ? canvas.endMarkedContentSequence();

? ? } catch(DocumentException de) {

? ? ? ? throw new ExceptionConverter(de);

? ? }

}

(CreateFooter輔助方法)

100 頁文檔的結(jié)果:

http://img1.sycdn.imooc.com/6489799e00017cc812280083.jpg

...

http://img2.sycdn.imooc.com/648979ac0001474012220081.jpg

查看完整回答
反對 回復(fù) 2023-06-14
  • 1 回答
  • 0 關(guān)注
  • 161 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號