对齐页面中心的段落

时间:2013-01-17 06:24:08

标签: java pdf itext

我使用itext生成pdf文件。我想在页面中间对齐我的标题。目前我正在使用这样的

Paragraph preface = new Paragraph();  
for (int i = 0; i < 10; i++) {
    preface.add(new Paragraph(" "));
}

是否正确或是否还有其他最佳方式。

7 个答案:

答案 0 :(得分:60)

使用Paragraph#setAlignment(int)

Paragraph preface = new Paragraph(); 
preface.setAlignment(Element.ALIGN_CENTER);

请参阅Element界面中的ALIGN_*常量,了解更多可能的值。

答案 1 :(得分:3)

如果有人正在寻找.NET / C#版本,下面是我如何实现CENTER对齐。

我正在使用用于.NET / C#的iText7库,我使用以下方法实现了这一目标:

Paragraph preface = new Paragraph();
preface.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);

答案 2 :(得分:2)

 public static final String DEST = "results/tables/centered_text.pdf";


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

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
    Paragraph para = new Paragraph("Test", font);
    para.setLeading(0, 1);
    PdfPTable table = new PdfPTable(1);
    table.setWidthPercentage(100);
    PdfPCell cell = new PdfPCell();
    cell.setMinimumHeight(50);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.addElement(para);
    table.addCell(cell);
    document.add(table);
    document.close();
}

答案 3 :(得分:1)

不确定这是否是旧版本,但对于PdfWriter,这些方法不存在。相反,我用过:

module PortalPaypalPaymentService
    def self.create_payment(request)
        #debugger
        price_in_egp = request.initial_price
        output = CurrencyExchange.convert(price_in_egp*100, 'EGP', 'USD')
        price_in_usd = (output.cents/100.0).round(2)
        params = {
            "intent": "sale",
            "redirect_urls": {
                "return_url": APP_CONFIG['paypal_return_url'],
                "cancel_url": APP_CONFIG['paypal_cancel_url']
            },
            "payer": {
                "payment_method": "paypal"
            },
            "transactions": [{
                "amount": {
                "total": price_in_usd,
                "currency": "USD"
                }
            }]
        }
        response = send_request_post(PAYMENT_URL, params)
        JSON.parse(response.body)["id"]
    end

    def self.execute_payment(payment_id, payer_id)
        params = {
            payer_id: payer_id
        }
        response = send_request_post(PAYMENT_URL + '/' + payment_id + '/execute', params)
        JSON.parse(response.body)
    end
end

答案 4 :(得分:0)

如果您正在寻找Itext7的解决方案,则可以使用方法setTextAlignment(...)

示例:

Paragraph preface = new Paragraph();
// add text
preface.setTextAlignment(TextAlignment.CENTER);

答案 5 :(得分:0)

这对我有用(itext 5.0.5):

Paragraph p3= new Paragraph("Hello" );
  p3.setAlignment(Element.ALIGN_CENTER);

答案 6 :(得分:0)

我有用于此的搜索解决方案,以将PdfPCell文本对齐到右侧并居中。修改并更改代码顺序后,它就可以工作。

此代码无法使文本居中对齐。

cols = list(df.columns.values)
random.shuffle(cols)
df.columns = cols

使用此代码修改代码后,它现在可以正常工作。

              PdfPCell cell = new PdfPCell();
              cell.addElement(new Phrase("Testing Page");
              cell.setHorizontalAlignment(Element.ALIGN_CENTER);
              table.addCell(cell);
相关问题