java - 无法设置参数使用itextpdf生成的PDF

标签 java itext

我正在使用 itextpdf 将 HTML 处理为 PDF,但遇到问题。 HTML 具有某些应在转换后设置的参数。当我操作 pdf 文件来设置具有垂直水印的图像时,生成 pdf 的 byte[] 具有如下内容:

[(C)-7(ódi)8(g)-3(o)16(:)-7(${)15(C)-7(O)4(D)3(_)5(D)3(O)4(C)-7(}  )20(R)-3(e)-3(v)-4(i)7(s)-3(i)7(ón)15(:)-7(${)15(R)-3(E)10(V)9(_)5(D)3(O)4(C)-7(}  )5(F)-3(e)-3(ch)15(a:)-5(${)15(F)-3(E)10(C)-7(H)19(A)-5(_)5(D)3(O)4(C)-7(})] TJ

它有一些参数,例如${COD_DOC},但是当我设置它们时,问题是图像链接

我的问题是:这些数字意味着什么?因为当我尝试设置字母 P 时它不起作用。

代码:

public Image manipulatePdfTemplate(File pdfTemplate, PdfDocument pdfDoc, Map<String, Object> fileVariables) 
        throws Exception 
{
    log.info("Iniciando el procesamiento del la plantilla pdf");
    PdfPage firstPage = pdfDoc.getFirstPage();

    // Sustituimos las variables por los valores de los parametros
    PdfObject object = firstPage.getPdfObject().get(PdfName.Contents);
    if (object instanceof PdfStream) {
        PdfStream stream = (PdfStream) object;
        byte[] data = stream.getBytes();

        // Método para escribir los bytes del contenido del pdf en un txt
        // writePdfContent(data);

        // Falla el caracter "5" y "X"
        if (fileVariables != null) {
            if (fileVariables.get("COD_DOC") != null && fileVariables.get("REV_DOC") != null
                    && fileVariables.get("FECHA_DOC") != null) {
                String replacedData = new String(data, StandardCharsets.ISO_8859_1)
                        .replace(AppConstant.COD_DOC, " (" + fileVariables.get("COD_DOC") + "  )")
                        .replace(AppConstant.REV_DOC, " (" + fileVariables.get("REV_DOC") + "  )")
                        .replace(AppConstant.FECHA_DOC, " (" + fileVariables.get("FECHA_DOC") + ")");
                stream.setData(replacedData.getBytes(StandardCharsets.ISO_8859_1));
            }
            else if(fileVariables.get("VERTICAL_TEXT") != null) {
                String replacedData = new String(data, StandardCharsets.ISO_8859_1)
                        .replace(AppConstant.VERTICAL_TEXT, "(" + fileVariables.get("VERTICAL_TEXT") + ")");
                stream.setData(replacedData.getBytes(StandardCharsets.ISO_8859_1));
            }
        }
    }

    // Convertimos el resultado a imagen que añadiremos de fondo en cada página del pdf resultante
    PdfFormXObject pageCopy = firstPage.copyAsFormXObject(pdfDoc);
    Image image = new Image(pageCopy);

    return image;
}

设置参数前: enter image description here

设置参数后: enter image description here

最佳答案

首先,帮自己一个忙,在转换为 PDF 之前替换您的 ${COD_DOC} 和类似变量。 PDF 不是一种用于编辑的格式。对于您的情况,您会看到一些原因,请阅读 this answer更多信息。

话虽如此,您的实际问题是:

[(C)-7(ódi)8(g)-3(o)16(:)-7(${)15(C)-7(O)4(D)3(_)5(D)3(O)4(C)-7(}  )20(R)-3(e)-3(v)-4(i)7(s)-3(i)7(ón)15(:)-7(${)15(R)-3(E)10(V)9(_)5(D)3(O)4(C)-7(}  )5(F)-3(e)-3(ch)15(a:)-5(${)15(F)-3(E)10(C)-7(H)19(A)-5(_)5(D)3(O)4(C)-7(})] TJ

My question is: what does the numbers mean?

您发布了TJ操作。因此,让我们看看 PDF 规范是如何描述此操作的:

array TJ – Show one or more text strings, allowing individual glyph positioning. Each element of array shall be either a string or a number. If the element is a string, this operator shall show the string. If it is a number, the operator shall adjust the text position by that amount; that is, it shall translate the text matrix, Tm. The number shall be expressed in thousandths of a unit of text space (see 9.4.4, "Text Space Details"). This amount shall be subtracted from the current horizontal or vertical coordinate, depending on the writing mode. In the default coordinate system, a positive adjustment has the effect of moving the next glyph painted either to the left or down by the given amount.

(ISO 32000-1,表 109 – 文本显示运算符)

阅读this answer有关第 9.4.4 节的详细信息,了解这些数字如何准确地转换文本矩阵,Tm

因此,从本质上讲,这些数字缩短(正)或延长(负)前后字符串的绘图之间的间隙。

通常,就像您的情况一样,此功能用于实现 kerning ,即调整比例字体中字符之间的间距,因为如果字符不使用其标准宽度,某些字符组看起来会更好。

关于java - 无法设置参数使用itextpdf生成的PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61286703/

相关文章:

javascript - 如何获得后退按钮链接以返回原始搜索页面?

java - Maven verify 命令有什么作用?

java - 如何从类路径加载 ChromeDriver 二进制文件?

java - iText/BouncyCaSTLe ClassNotFound org.bouncycaSTLe.asn1.DEREncodable 和 org.bouncycaSTLe.tsp.TimeStampTokenInfo

java - 我生成了一个 pdf,但保存时更新的是同一个 pdf,而不是新的 pdf

java - 如何修改 PdfPCell 以便可以删除边框的一部分(例如 BOTTOM、TOP、LEFT)?

java - 使用波兰语字母的 iText 和 XMLWorker 将 HTML 转换为 PDF

java - 在 Spring Boot 中添加具有不同客户端 ID 的 MQTT 连接

java - 有没有办法在现有的继承树中插入自定义类?

java - 如何在 Java 中使用 JDBC 从 MySQL 检索数学方程式?