gpt4 book ai didi

java - 我想使用 itextpdf 动态设置表格行的高度

转载 作者:行者123 更新时间:2023-12-04 17:31:02 42 4
gpt4 key购买 nike

如何在 iTextpdf 中设置表格的行位置和宽度?这里我需要如何设置每个单元格的位置以及如何在 itextpdf 中设置行间距

testTable11_close_right.getDefaultCell().setBorder(0);
testTable11_close_right.getDefaultCell().setPadding(0);
testTable11_close_right.addCell(aarr2);
testTable11_close_right.addCell(s6);


testTable111.addCell(single_tender_id);
testTable111.addCell(s2);
testTable111.addCell(s3);
testTable111.addCell(testTable11_close_right);

PdfPCell cell7 = new PdfPCell();
PdfPTable testTable_dept= new PdfPTable(1);
testTable_dept.setWidthPercentage(100);
testTable_dept.getDefaultCell().setPaddingTop(0);
testTable_dept.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
testTable_dept.addCell(new Phrase(""+temp_department_name,myBoldFont1));
cell7.addElement(testTable_dept);
cell7.setBackgroundColor(BaseColor.BLACK);
cell7.setHorizontalAlignment(Element.ALIGN_CENTER);


table_odd1.addCell(cell7);
table_odd1.addCell(testTable111);
table_odd.addCell(table_odd1);

return table_odd;

最佳答案

引用网页ITEXT IN ACTION: CHAPTER 3: ADDING CONTENT AT ABSOLUTE POSITIONS

package part1.chapter03;

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.BaseColor;

public class FoobarFilmFestival {

public static final String RESULT
= "results/part1/chapter03/foobar_film_festival.pdf";

/**
* Main method.
*
* @param args no arguments needed
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args)
throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer
= PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// step 3
document.open();
// step 4
Chunk c;
String foobar = "Foobar Film Festival";
// Measuring a String in Helvetica
Font helvetica = new Font(FontFamily.HELVETICA, 12);
BaseFont bf_helv = helvetica.getCalculatedBaseFont(false);
float width_helv = bf_helv.getWidthPoint(foobar, 12);
c = new Chunk(foobar + ": " + width_helv, helvetica);
document.add(new Paragraph(c));
document.add(new Paragraph(String.format("Chunk width: %f", c.getWidthPoint())));
// Measuring a String in Times
BaseFont bf_times = BaseFont.createFont(
"c:/windows/fonts/times.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
Font times = new Font(bf_times, 12);
float width_times = bf_times.getWidthPoint(foobar, 12);
c = new Chunk(foobar + ": " + width_times, times);
document.add(new Paragraph(c));
document.add(new Paragraph(String.format("Chunk width: %f", c.getWidthPoint())));
document.add(Chunk.NEWLINE);
// Ascent and descent of the String
document.add(new Paragraph("Ascent Helvetica: "
+ bf_helv.getAscentPoint(foobar, 12)));
document.add(new Paragraph("Ascent Times: "
+ bf_times.getAscentPoint(foobar, 12)));
document.add(new Paragraph("Descent Helvetica: "
+ bf_helv.getDescentPoint(foobar, 12)));
document.add(new Paragraph("Descent Times: "
+ bf_times.getDescentPoint(foobar, 12)));
document.add(Chunk.NEWLINE);
// Kerned text
width_helv = bf_helv.getWidthPointKerned(foobar, 12);
c = new Chunk(foobar + ": " + width_helv, helvetica);
document.add(new Paragraph(c));
// Drawing lines to see where the text is added
PdfContentByte canvas = writer.getDirectContent();
canvas.saveState();
canvas.setLineWidth(0.05f);
canvas.moveTo(400, 806);
canvas.lineTo(400, 626);
canvas.moveTo(508.7f, 806);
canvas.lineTo(508.7f, 626);
canvas.moveTo(280, 788);
canvas.lineTo(520, 788);
canvas.moveTo(280, 752);
canvas.lineTo(520, 752);
canvas.moveTo(280, 716);
canvas.lineTo(520, 716);
canvas.moveTo(280, 680);
canvas.lineTo(520, 680);
canvas.moveTo(280, 644);
canvas.lineTo(520, 644);
canvas.stroke();
canvas.restoreState();
// Adding text with PdfContentByte.showTextAligned()
canvas.beginText();
canvas.setFontAndSize(bf_helv, 12);
canvas.showTextAligned(Element.ALIGN_LEFT, foobar, 400, 788, 0);
canvas.showTextAligned(Element.ALIGN_RIGHT, foobar, 400, 752, 0);
canvas.showTextAligned(Element.ALIGN_CENTER, foobar, 400, 716, 0);
canvas.showTextAligned(Element.ALIGN_CENTER, foobar, 400, 680, 30);
canvas.showTextAlignedKerned(Element.ALIGN_LEFT, foobar, 400, 644, 0);
canvas.endText();
// More lines to see where the text is added
canvas.saveState();
canvas.setLineWidth(0.05f);
canvas.moveTo(200, 590);
canvas.lineTo(200, 410);
canvas.moveTo(400, 590);
canvas.lineTo(400, 410);
canvas.moveTo(80, 572);
canvas.lineTo(520, 572);
canvas.moveTo(80, 536);
canvas.lineTo(520, 536);
canvas.moveTo(80, 500);
canvas.lineTo(520, 500);
canvas.moveTo(80, 464);
canvas.lineTo(520, 464);
canvas.moveTo(80, 428);
canvas.lineTo(520, 428);
canvas.stroke();
canvas.restoreState();
// Adding text with ColumnText.showTextAligned()
Phrase phrase = new Phrase(foobar, times);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 200, 572, 0);
ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, phrase, 200, 536, 0);
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 500, 0);
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 464, 30);
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase, 200, 428, -30);
// Chunk attributes
c = new Chunk(foobar, times);
c.setHorizontalScaling(0.5f);
phrase = new Phrase(c);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 572, 0);
c = new Chunk(foobar, times);
c.setSkew(15, 15);
phrase = new Phrase(c);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 536, 0);
c = new Chunk(foobar, times);
c.setSkew(0, 25);
phrase = new Phrase(c);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 500, 0);
c = new Chunk(foobar, times);
c.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 0.1f, BaseColor.RED);
phrase = new Phrase(c);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 464, 0);
c = new Chunk(foobar, times);
c.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 1, null);
phrase = new Phrase(c);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 400, 428, -0);
// step 5
document.close();
}
}

关于java - 我想使用 itextpdf 动态设置表格行的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21183281/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com