我試圖用iText縮放圖像(在新的PDF文檔上),以便使其無需拉伸即可填充頁面的寬度,以便可能需要幾頁。使用iText縮放圖像以填充多個頁面
我發現了很多解決方案,但它們非常複雜,我不太喜歡這樣的編碼。迄今爲止我發現的最好的解決方案(從另一個問題來看)是使用PdfTable,但它總是使用單個頁面來縮放圖像。
// Load image from external storage
Image image = Image.getInstance(path + "/img.png");
// Calculate ratio
float width = PageSize.A4.getWidth();
float heightRatio = image.getHeight() * width/image.getWidth();
Document document = new Document();
document.open();
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
PdfPCell c = new PdfPCell(image, true);
c.setBorder(PdfPCell.NO_BORDER);
c.setPadding(0);
// Set image dimensions
c.getImage().scaleToFit(width, heightRatio);
table.addCell(c);
document.add(table);
// Write PDF file
document.close();
有什麼建議嗎?
在PDF中,每個頁面都有自己的畫布。如果圖像是在一個頁面畫布上繪製的,則這隻會影響該頁面的外觀,對其他頁面沒有影響。你可以做的是在適當選擇圖像位置的多個頁面上繪製相同的圖像。圖片位置 – mkl
謝謝@布魯諾,我明白了。我正試圖現在解決這個問題。我也發現你的這個答案http://developers.itextpdf.com/question/how-show-image-large-dimensions-across-multiple-pages(至少我假設你是圖片);)。 – afe
評論由@mkl提供,但你是對的照片中的人是我,我旁邊的女人是我的妻子;-)這個例子是否能解決你的問題?這並不複雜,是嗎? –