0

I am processing an HTML to PDF using itextpdf but I have a problem. The HTML has certain parameters that should be set after transformation. When I manipulate the pdf file to set the image that has the vertical watermark, The byte[] that generates the pdf has something like this:

[(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

this has some parameters like ${COD_DOC}, but when I set them the problem is image linked

My question is: what does the numbers mean? because when I try to set the letter P it doesnt work.

Code:

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;
}

Before setting parameters: enter image description here

After setting parameters: enter image description here

kedar sedai
  • 1,687
  • 3
  • 16
  • 25
  • First of all, do yourself a favor and replace your `${COD_DOC}` and similar variables *before* transforming to PDF. PDF is not a format meant for editing. In your case you see some reasons why, read [this answer](https://stackoverflow.com/a/60655298/1729265) for some more. – mkl Apr 18 '20 at 10:53
  • 1
    Please do not post a screenshot of your post, but post the actual code itself. Why? Because people who want to give you an answer, first want to run your code on their computer. If we have to type it over, then we don't even bother giving you an answer. Too much effort. If we can just copy/paste, then you make it a lot easier for people to give you an answer. – Amedee Van Gasse Apr 20 '20 at 03:07

1 Answers1

0

First of all, do yourself a favor and replace your ${COD_DOC} and similar variables before transforming to PDF. PDF is not a format meant for editing. In your case you see some reasons why, read this answer for some more.

That being said, your actual question is:

[(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?

You posted a TJ operation. Thus, let's see what the PDF specification says about this operation:

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, Table 109 – Text-showing operators)

Read this answer for details from section 9.4.4 to see how exactly these numbers translate the text matrix, Tm.

Essentially, therefore, these numbers shorten (positive) or lengthen (negative) the gap between drawings of the preceding and the following strings.

Often, like in your case, this feature is used to implement kerning, i.e. the adjustment of the spacing between characters in a proportional font, because certain character groups look better if the characters don't use their standard widths.

Community
  • 1
  • 1
mkl
  • 90,588
  • 15
  • 125
  • 265