Skip to content
ComPDF

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Edit Text and Image Properties

ComPDFKit supports to modify the properties of text and images.

Edit Text Properties

ComPDFKit supports modifying text properties, such as text font size, name, color, alignment, italic, bold, transparency, etc when you are in content editor mode. The following code shows you how to set text to 22px, black, bold, font Times-Roman, and opacity 60.

ComPDF for Web supports Helvetica, Courier, Times Roman, and DroidSansFallbackFull font family. DroidSansFallbackFull font is a font family that supports Chinese, Japanese, Korean, and other fonts.

Set Text Properties

Use docViewer.setContentEditorProperty to set properties for the selected text or the active text box.

javascript
docViewer.setContentEditorProperty(type, props);
NameRequiredTypeOptionsDescription
typeyesstringtext | image | commonThe content-editor type. Use text to set text properties.
propsyesobjectN/AThe properties to set.
props.boldnobooleantrue | falseAdds or removes bold without changing italic.
props.italicnobooleantrue | falseAdds or removes italic without changing bold.
props.fontStylenonumber | string0 | 1 | 2 | 3 | regular | bold | italic | oblique | boldItalic | boldObliqueReplaces the complete bold and italic style. 0, 1, 2, and 3 represent regular, bold, italic, and bold italic, respectively.
props.underlinenobooleantrue | falseAdds or removes an underline.
props.strikeThroughnobooleantrue | falseAdds or removes a strikethrough.
props.strikethroughnobooleantrue | falseDeprecated alias for strikeThrough.
props.rangenoobjectN/ASets the character range to update.

The following example sets the selected text to bold italic and adds an underline. If no characters are selected, the properties are applied to the entire active text box.

javascript
docViewer.setContentEditorProperty('text', {
  bold: true,
  italic: true,
  underline: true,
  strikeThrough: false
});

The bold, italic, underline, and strikeThrough properties are applied independently. If fontStyle is used with bold or italic, fontStyle is applied first, and then bold and italic override their corresponding styles. If both strikeThrough and the deprecated strikethrough are provided, strikeThrough takes precedence.

Without range, the properties apply to the currently selected characters. If there is no character selection, they apply to the whole active text box. To update an explicit range, pass the camelCase range returned by the contentSelected event. The event data contains:

NameTypeDescription
selectedTextstringThe selected text.
pageNumbernumberThe one-based page number containing the active text editor.
textRectsArray<{ left, top, right, bottom }>The selected text rectangles.
rangeCharacterRangeThe selected character range in the public camelCase format.
javascript
docViewer.addEvent('contentSelected', ({
  selectedText,
  pageNumber,
  textRects,
  range
}) => {
  docViewer.setContentEditorProperty('text', {
    bold: true,
    underline: true,
    range
  });
});

A character range has the following format:

javascript
const range = {
  start: {
    sectionIndex: 0,
    lineIndex: 0,
    runIndex: 0,
    charIndex: -1
  },
  end: {
    sectionIndex: 0,
    lineIndex: 0,
    runIndex: 0,
    charIndex: 4
  }
};

Both start and end must contain all four integer fields. sectionIndex, lineIndex, and runIndex must be non-negative; charIndex can be -1 to represent the position before the first character. start must not be less than end. If the explicit range is invalid, the API returns false and does not change the text.

The legacy replacement form remains supported. For example, the following sets the complete bold/italic state to bold italic:

javascript
docViewer.setContentEditorProperty('text', {
  fontStyle: 3
});
javascript
const contentEditManager = docViewer.getContentEditManager();
const contentEditBoxes = contentEditManager.getContentBoxesList();
const contentEditBox = contentEditBoxes[0];
// Set text properties
contentEditBox.color = '#FF0000'
contentEditBox.opacity = 60;
contentEditBox.fontSize = 22;
contentEditBox.fontFamily = 'Times-Roman';
contentEditBox.fontStyle = 'bold';
contentEditBox.lineSpacing = 1.5;
contentEditBox.paragraphSpacing = 10;
contentEditBox.characterSpacing = 2;
contentEditBox.textAlign = 'center'; // left | right | center

// You can also set text style by using setTextStyles API
contentEditBox.setTextStyles && contentEditBox.setTextStyles({
  color: '#000000',
  opacity: 60,
  fontSize: 22,
  fontFamily: 'Times-Roman',
  fontStyle: 'bold',
  textAlign: 'center',
})

Edit Image Properties

ComPDFKit supports modifying image properties, such as rotating, cropping, mirroring, and setting transparency.

This example shows how to rotate an image and set it to semi-transparent:

javascript
const contentEditManager = docViewer.getContentEditManager();
const contentEditBoxes = contentEditManager.getContentBoxesList();
const contentEditBox = contentEditBoxes[0];

contentEditBox.opacity = 60;

// Rotate the image 90 degrees clockwise, -90 means rotate 90 degrees counterclockwise
contentEditBox.rotateImage(90);
// Flip the image horizontally or vertically
contentEditBox.flipImage('horizontal'); // horizontal | vertical
// Crop the image with the specified left, top, right, and bottom values
contentEditBox.cropImage({ left: 0, top: 0, right: 100, bottom: 100 });