gpt4 book ai didi

java - 需要使用 iText 的 Lowagie 实现在 PDF 标题的表格单元格内写入阿拉伯文本

转载 作者:行者123 更新时间:2023-12-02 06:20:01 33 4
gpt4 key购买 nike

客户要求我们在生成的 PDF 标题中创建一个包含一些字段和值的表格。需要重复此 header 。

早期的实现将 PDFPTable 和 PDFPCell 中的数据放在正文内容中,即使经过多次尝试也未能重复。我们已经从 PDFPTable --> Table 和 PDFPCell --> Cell 转移,并且它在标题中工作。现在表的一列包含阿拉伯文本,并且在简单的表和单元格中没有 RUNDIRECTION 设置 RTL 的选项。以 Unicode 编写的文本打印错误。任何帮助表示赞赏。指定供引用的源代码是示例,不包含完整的处理。

public class DataReportFiller {

public static void main(String[] args) {

ff.setFileType(FileFormat.FILE_TYPE_PDF);
PDFWriter pw = new PDFWriter(ff, basePath);

pw.open(fileName, recordSize);
Document document = pw.getDocument();
document.setPageSize(PageSize.A4);

pw.openFileForHeader();

String accStr10[] = null;

accStr10 = new String[] { "TESTING DATA", "", "", "", "", "",
"Total Pages ", Integer.toString(totalPages), "", "",
"\u0629\u062d\u0641\u0635\u0644\u0627 \u0645\u0642\u0631", "" };

pw.setBorder = false;
pw.setBold = true;
pw.fontColour = PDFWriter.COLOUR_BLACK;
pw.bgColour = PDFWriter.COLOUR_WHITE;
pw.horAlignment = pw.ALIGN_LEFT;

// ----------------------------------
int totalPages = 1;
totalPages = totalPages + ((recordSize + 5) / 12);

Table headerTable = pw.getHeaderTable();
headerTable.setBorder(Rectangle.BOTTOM);
headerTable.setBorderColor(Color.WHITE);
pw.writeStatementHeaderLine(accStr10, DataWriter.LINE_TYPE_GENERAL);
headerPhrase.add(2, headerTable);

HeaderFooter header = new HeaderFooter(headerPhrase, false);
header.setAlignment(0);
header.setBorder(Rectangle.NO_BORDER);
document.setHeader(header);
pw.openFile();

pw.createEmptyLine();
pw.commitEmptyLine();

pw.close();

}

// Another method to write data in header, unable to put arabic text here
public boolean writeStatementHeaderLine(String line[], int type)
throws Exception {
try {

// Increment the current line number
currentLineNo++;

// Check if the text is not null or empty
if (line == null || line.length == 0) {

for (int i = 0; i < fileFormat.getFileColCount(); i++) {
chunk = new Chunk(" ", font);
Cell simpleCell = new Cell(new Phrase(chunk));
headerTable.addCell(simpleCell);
}

return false;
}

for (int i = 0; i < line.length; i++) {
setFont();

Cell simpleCell = null;
if (line[i] == null) {
chunk = new Chunk("", font);
simpleCell = new Cell(new Phrase(chunk));
} else if ((type == LINE_TYPE_GENERAL && i >= line.length - 2)) {
final String FONT = applicationContext
.getProperty("arabic.font.path");
font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H,
BaseFont.EMBEDDED, 8);
if (line[i].equalsIgnoreCase("iban"))
setFont();
chunk = new Chunk(line[i], font);
simpleCell = new Cell(new Phrase(chunk));

} else {
chunk = new Chunk(line[i], font);
if (type == LINE_TYPE_GENERAL) {
simpleCell = new Cell(new Phrase(chunk));
}

}
simpleCell.setBorder(Rectangle.NO_BORDER);

if ((type == LINE_TYPE_GENERAL && i >= line.length - 2)) {
simpleCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
// simpleCell.rotate();

}
headerTable.addCell(simpleCell);
}
} catch (Exception e) {
e.printStackTrace();
deleteFile();
e.printStackTrace();
throw e;
}

return true;
}

}

需要在 PDF 标题表中将分支打印为“فف ف٬ْ”,其中显示“فففف”。

最佳答案

最终通过PageEvent的onEndPage方法实现,代码引用如下

pw.open File();

        pw.writer.setPageEvent(new PdfPageEventHelper(){


protected ApplicationContext applicationContext = ApplicationContext.getInstance();




@Override
public void onEndPage(PdfWriter writer, Document document)
{


PdfPTable table1=new PdfPTable(12);
try{


String accStr[] = null;
String accStr10[] = null;

accStr = new String[] { accountTitle ,"","","","","","Period Covered " ,startDate +" to " +endDate,"","","\u0645\u062f\u0629 \u0627\u0644\u062a\u063a\u0637\u064a\u0629",""};
accStr10 = new String[] {address1 ,"","","","","","Page No ",Integer.toString(writer.getPageNumber()-1),"","","\u0631\u0642\u0645 \u0627\u0644\u0635\u0641\u062d\u0629",""};

table1.setTotalWidth(522);
float f[] = {45,10,0,0,0,0,25,35,0,0,20,0};
table1.setWidths(f);

pw.setBorder = true;
pw.setBold = true;
pw.fontColour = AccStmtPDFWriter.COLOUR_BLACK;
pw.bgColour = AccStmtPDFWriter.COLOUR_WHITE;
pw.horAlignment = pw.ALIGN_LEFT;
PdfPCell pdfCell=null;

//table1=pw.getTable1();



if(!FieldNameUtil.isApplicableForBahrain){
pdfCell = new PdfPCell(new Phrase(new Chunk("")));
pdfCell.setBorder(0);
pdfCell.setColspan(12);
table1.addCell(pdfCell);
table1=writeHeaderLine(accStr, DataWriter.LINE_TYPE_GENERAL,table1);
}

pdfCell = new PdfPCell(new Phrase(new Chunk("")));
pdfCell.setBorder(0);
pdfCell.setColspan(12);
table1.addCell(pdfCell);
table1=writeHeaderLine(accStr10, DataWriter.LINE_TYPE_GENERAL,table1);


table1.writeSelectedRows(0, -1, 57, 730, writer.getDirectContent());
//document.add(table1);
table1.deleteBodyRows();
}catch(Exception e){
e.printStackTrace();
}

}

public PdfPTable writeHeaderLine(String line[], int type,PdfPTable table2)
throws Exception {
Chunk chunk = null;
PdfPCell cell =null;
PdfPTable tableNew =table2;
Font font=FontFactory.getFont("Arial",8,Font.BOLD,Color.BLACK);
try {



for (int i = 0; i < line.length; i++) {
//changed by shruti
if (line[i] == null)
chunk = new Chunk("", font);
else if(type==115 || (type==pw.LINE_TYPE_GENERAL && i>=line.length-2) || (type==112 && i==line.length-1)){
final String FONT = applicationContext.getProperty("arabic.font.path");
font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED,8);
if(line[i].equalsIgnoreCase("iban"))
font=FontFactory.getFont("Arial",8,Font.BOLD,Color.BLACK);
chunk = new Chunk(line[i], font);
}else
chunk = new Chunk(line[i], font);

cell = new PdfPCell(new Phrase(chunk));
cell.setBorder(Rectangle.NO_BORDER);
cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
if((type==pw.LINE_TYPE_GENERAL && i>=line.length-2)){
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
tableNew.addCell(cell);
}else{
tableNew.addCell(cell);
}

}

return tableNew;

} catch (Exception e) {
e.printStackTrace();
throw e;
}
}




});

关于java - 需要使用 iText 的 Lowagie 实现在 PDF 标题的表格单元格内写入阿拉伯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55842192/

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