Skip to content

编辑文本和图像属性

ComPDFKit 支持修改文本与图像属性。您可以在CPDFReaderView自定义的上下文菜单getEditSelectTextContentView方法中来设置文本和图像属性。

编辑文本属性

ComPDFKit 支持修改文本属性,如字体大小、名称、颜色、对齐方式、斜体、粗体、透明度等。

以下是如何将文本设置为12pt、红色和粗体的示例代码:

java
public class DemoContextMenuHelper extends CPDFContextMenuShowHelper {

  public DemoContextMenuHelper(CPDFReaderView cpdfReaderView) {
    super(cpdfReaderView);
  }

  @Override
  public  View getEditSelectTextContentView(CPDFPageView pageView, LayoutInflater layoutInflater, CPDFEditSelections selections) {
		View contentView = ....
    invokeOnClickListener(contentView, view ->{
      if (view.getId() == R.id.edit_attr){
        CPDFEditTextSelections textSelections = (CPDFEditTextSelections) selections;
        textSelections.setFontSize(12);
        textSelections.setColor(Color.RED);
        textSelections.setBold(true);
        textSelections.setItalic(true);
        textSelections.setAlign(CPDFEditTextArea.PDFEditAlignType.PDFEditAlignLeft);
        textSelections.setTransparancy(255);
        textSelections.setFontName(CPDFEditTextArea.FontTimesRoman);
      }
    }, R.id.edit_attr);
    return contentView;
  }
}
kotlin
public class DemoContextMenuHelper(readerView: CPDFReaderView?) : CPDFContextMenuShowHelper(readerView) {

  override fun getEditTextAreaContentView(
    pageView: CPDFPageView?,
    layoutInflater : LayoutInflater?,
    selections: CPDFEditSelections?
  ): View {
    View contentView = ...
    invokeOnClickListener(pageView, {view ->
       if (view.id == R.id.edit) {
          val textSelections = selections as CPDFEditTextSelections
          textSelections.fontSize = 12F
          textSelections.color = Color.RED
          textSelections.isBold = true
          textSelections.isItalic = true
          textSelections.align = CPDFEditTextArea.PDFEditAlignType.PDFEditAlignLeft
          textSelections.transparancy = 255F
          textSelections.fontName = CPDFEditTextArea.FontTimesRoman
    		}
    }, R.id.edit)
    return contentView
  }
}

编辑图像属性

ComPDFKit 支持修改图像属性,如旋转、裁剪、镜像和设置透明度。

以下是如何如何旋转图像并将其设置为半透明的示例代码:

java
public class DemoContextMenuHelper extends CPDFContextMenuShowHelper {

  public DemoContextMenuHelper(CPDFReaderView cpdfReaderView) {
    super(cpdfReaderView);
  }

  @Override
  public View getEditImageAreaContentView(final CPDFPageView pageView, LayoutInflater layoutInflater, RectF area) {
    View contentView = ...
      this.pageView = pageView;
    invokeOnClickListener(pageView, view ->{
      if (view.getId() == R.id.edit){
        pageView.operateEditImageArea(CPDFPageView.EditImageFuncType.ROTATE, 90.0F);
        pageView.operateEditImageArea(CPDFPageView.EditImageFuncType.TRANCPARENCY, 127.5F);
      }
    }, R.id.edit);
    return contentView;
  }
}
kotlin
public class DemoContextMenuHelper(readerView: CPDFReaderView?) : CPDFContextMenuShowHelper(readerView) {

  @Override
  public View getEditImageAreaContentView(final CPDFPageView pageView, LayoutInflater layoutInflater, RectF area) {
    this.pageView = pageView;
    invokeOnClickListener(pageView, view ->{
      if (view.getId() == R.id.edit){
        pageView.operateEditImageArea(CPDFPageView.EditImageFuncType.ROTATE, 90.0F);
        pageView.operateEditImageArea(CPDFPageView.EditImageFuncType.TRANCPARENCY, 127.5F);
      }
    }, R.id.edit);
    return super.getEditImageAreaContentView(pageView, layoutInflater, area);
  }
}