TextWidget border color won't set

Product: pdfnet-node

Product Version: 9.3.0"

Please give a brief summary of your issue:
The method TextWidget.setBorderColor method seems to set the value, but the appearance is always black.

Please provide a link to a minimal sample where the issue is reproducible:

import { PDFNet } from '@pdftron/pdfnet-node';

const wontSetBorderColor = async () => {
  const doc = await PDFNet.PDFDoc.create();
  const pageRect = await PDFNet.Rect.init(
    0, // x1, left
    0, // y1, bottom
    500, // x2, right
    500 // y2, top
  );
  const page = await doc.pageCreate(pageRect);

  await doc.pagePushBack(page);

  const textWidget = await PDFNet.TextWidget.create(
    doc,
    new PDFNet.Rect(100, 100, 300, 100),
    'test'
  );

  await textWidget.setText('why my border no go white?');

  const white = await PDFNet.ColorPt.init(255, 255, 255);
  const solid = await PDFNet.AnnotBorderStyle.create(0, 3);

  await textWidget.setBorderColor(white, 3);
  await textWidget.setBorderStyle(solid);

  const shouldBeWhite = await textWidget.getBorderColor();
  const r = await shouldBeWhite.get(0);
  const g = await shouldBeWhite.get(1);
  const b = await shouldBeWhite.get(2);

  console.log({ r, g, b }); // 255, 255, 255

  await textWidget.refreshAppearance();

  await page.annotPushBack(textWidget);

  await doc.save(
    './whiteBorder.pdf',
    PDFNet.SDFDoc.SaveOptions.e_compatibility
  );
};

(async () => {
  return await PDFNet.runWithCleanup(async () => {
    await wontSetBorderColor();
  }, '<key>');
})();

Hi Mark,

For some widgets, such as text widgets, the customer sets the appearance of the widget. The exact behaviour depends on the specific PDF viewer you are using (Adobe, Safari, etc.) and its specifications. For more info view the following:

Hm… ok, thanks. Is there any way to set a ‘padding’ value on a TextWidget? This was what I was trying to do as a hack with a thick white border. It didn’t seem like something apparent from the docs.

Ah, no brainer, nevermind.

I just drew a separate Rect for the border, and inset the TextWidget by 5.

@kmirsalehi I have a similar issue with a ColorPt being rendered as white instead of the input value, but htis time on a Rect fill:

/** Creates a text field with data set as the text. If `header` param is true, field will be flattened */
  const drawCell = async ({
    fieldName,
    data,
    position: { x, y },
    width,
    height,
    fontSize = cellFontSize,
    font = helvetica,
    header,
  }: {
    fieldName: string;
    data: string;
    position: { x: number; y: number };
    width: number;
    height: number;
    fontSize?: number;
    font?: PDFNet.Font;
    header?: boolean;
  }) => {
    await builder.reset();

    const gray = await PDFNet.ColorPt.init(190, 190, 190);

    // draw rect for border
    const border = await builder.createRect(x, y, width, -height);
    await border.setPathStroke(true);

    if (header) {
      // gray background fill
      await border.setPathFill(true);
      const gState = await border.getGState();
      const rgbColorSpace = await PDFNet.ColorSpace.createDeviceRGB();
      await gState.setFillColorSpace(rgbColorSpace);
      await gState.setFillColorWithColorPt(gray);
      // issue: color is rendered as white, overtop of the flattened textwidget below
    }

    await writer.writeElement(border);

    const textWidget = await PDFNet.TextWidget.create(
      mainDoc,
      new PDFNet.Rect(x + 5, y - height + 5, x + width - 5, y - 5),
      fieldName
    );

    await textWidget.setFont(font);
    await textWidget.setFontSize(fontSize);
    await textWidget.setPage(page);

    const textField = await textWidget.getField();
    await textField.setFlag(PDFNet.Field.Flag.e_multiline, true);
    await textField.setFlag(PDFNet.Field.Flag.e_no_scroll, true);

    const text = await textField.getValueAsString();

    // gamingAgency may already exist
    if (!text) await textField.setValueAsString(data);

    await page.annotPushBack(textWidget);
    await textField.refreshAppearance();

    if (header) {
      await textWidget.flatten(page);
    }
  };

Hi Mark,

One thing to note is that for ColorPt, the colour value should be in the range [0… 1] so you need to divide your input parameters by 255 first.

View the documentation for more info:
https://www.pdftron.com/api/PDFTronSDK/dotnet/pdftron.PDF.ColorPt.html

1 Like

Thank you, that solved part of it.

Do you know if there is a way to make the flattened TextWidget appear overtop of the Rect?

Actually, for my use case, I managed to get the same result by just making the Rect black and decreasing the opacity.