Double space 未保存在 PDF 中

Double space not being preserved in PDF

我正在使用 lowagie.text.pdf 库从我的 java 应用程序中创建 PDF 文档,但是我意识到其中有双倍间距的字符串,即 "DOUBLE SPACE" 不是' t preserved.Is 这是 .PDF 的限制还是我忽略了其他东西?

Font font = FontFactory.getFont(FontFactory.HELVETICA, 8);
float[] columnWidths = new float[columnCount];

PdfPCell headerCell = new PdfPCell(new Phrase(gridColumn.getCaption(), font));

PdfPTable table = new PdfPTable(columnWidths);
table.getDefaultCell().setBorderWidth(0.5f);
table.getDefaultCell().setBorderColor(Color.LIGHT_GRAY);
table.setHeaderRows(1);

for (PdfPCell headerCell : headerCells) {
    table.addCell(headerCell);
}

String value = "DOUBLE  SPACED";

table.addCell(new Phrase(value, font));

Document pdfDocument = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);
ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();
PdfWriter.getInstance(pdfDocument, pdfStream);
pdfDocument.addTitle(caption);
pdfDocument.open();
pdfDocument.add(table);
pdfDocument.close();'

谢谢。

I have realised that strings with double spacing in them i.e. "DOUBLE SPACE" aren't preserved

这取决于你所说的保存是什么意思。

我扩展了您的样本位以打印单张、双张和三张 space

table.addCell(new Phrase("SINGLE SPACED", new Font(BaseFont.createFont(), 36)));
table.addCell(new Phrase("DOUBLE  SPACED", new Font(BaseFont.createFont(), 36)));
table.addCell(new Phrase("TRIPLE   SPACED", new Font(BaseFont.createFont(), 36)));

并进一步将使用的 类 更新为当前的 iText 5。5.x 变体。

如果您查看生成的 PDF 内部说明,您会看到

(SINGLE SPACED) Tj
...
(DOUBLE  SPACED) Tj
...
(TRIPLE   SPACED) Tj

因此,iText 确实保留了 spaces。

视觉结果:

如您所见,差距越来越大。因此,双倍和三倍 spaces 保留在 PDF 的视觉表示中!


另一方面,如果您使用 Adob​​e Reader 进行复制和粘贴,您会得到:

SINGLE SPACED
DOUBLE SPACED
TRIPLE SPACED

因此,当前的 Adob​​e Reader 不会 复制和粘贴 PDF 中的 space 字符,但会将多个字符折叠为一个字符。

所以:

Is this a .PDF limitation

这既不是 PDF 限制也不是 iText 限制,这是 Adob​​e Reader(以及其他一些 PDF 查看器)的怪癖。