Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我正在使用itext生成pdf。现在,我想让paragrap与表对齐,我应该做什么?_Java_Itext - Fatal编程技术网

Java 我正在使用itext生成pdf。现在,我想让paragrap与表对齐,我应该做什么?

Java 我正在使用itext生成pdf。现在,我想让paragrap与表对齐,我应该做什么?,java,itext,Java,Itext,就像图片一样,我想让1和3与2对齐。该表是默认的对齐方式。 我该怎么办?这里有一个简单的方法,将两者添加到同一段落中 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentExceptio

就像图片一样,我想让1和3与2对齐。该表是默认的对齐方式。
我该怎么办?

这里有一个简单的方法,将两者添加到同一段落中

import java.io.File;
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.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class ItextMain {
    public static final String DEST = "simple_table4.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        // file.getParentFile().mkdirs();
        new ItextMain().createPdf(DEST);
    }

    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();

        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        document.add(new Paragraph("1-Not aligned with table"));

        document.add(new Chunk());

        Paragraph p = new Paragraph();
        p.setIndentationLeft(20);// (20);

        PdfPTable table = new PdfPTable(4);
        for (int aw = 0; aw < 16; aw++) {
            table.addCell("hi");
        }
        table.setHorizontalAlignment(Element.ALIGN_LEFT);
        p.add(table);
        //document.add(table);

        p.add("3- Aligned with table");

        document.add(p);
        document.close();
    }
}
iText 5 PdfPTable类有一个width percentage属性,该属性保存表在页面中将占用的宽度百分比。默认情况下,该值为80,生成的表格在页面上水平居中,即特别是缩进

要防止出现这种情况,只需将百分比值设置为100:

PdfPTable table = ...;
table.setWidthPercentage(100);
或者设置水平对齐:

table.setHorizontalAlignment(Element.ALIGN_LEFT);

Itext版本是5.5.9。请检查此项