使用 ABAP 代码给 PDF 文件分配 usage right(未经笔者验证)

简介: 使用 ABAP 代码给 PDF 文件分配 usage right(未经笔者验证)
*&---------------------------------------------------------------------*
*& Report  ZTESTB
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  ZTESTB.
  PARAMETERS: pdf TYPE localfile OBLIGATORY,
              output TYPE localfile OBLIGATORY.
  DATA:
        lv_pdf TYPE xstring.
  START-OF-SELECTION.
  PERFORM upload USING pdf CHANGING lv_pdf.
  PERFORM assign_right CHANGING lv_pdf.
  PERFORM download USING lv_pdf output.
  AT SELECTION-SCREEN ON VALUE-REQUEST FOR pdf.
  DATA:
    file_table  TYPE filetable,
    action TYPE i,
    rc TYPE sysubrc.
  FIELD-SYMBOLS:
    <file> TYPE file_table.
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      default_extension       = 'pdf'
      file_filter             = cl_gui_frontend_services=>filetype_all
    CHANGING
      file_table              = file_table
      rc                      = rc
      user_action             = action
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  CHECK action = cl_gui_frontend_services=>action_ok.
  READ TABLE file_table INDEX 1 ASSIGNING <file>.
  pdf = <file>.
  AT SELECTION-SCREEN ON VALUE-REQUEST FOR output.
    DATA:
    file_table  TYPE filetable,
    user_action TYPE i,
    path TYPE string,
    full_path TYPE string,
    file_name TYPE string.
  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      default_extension    = 'pdf'
      file_filter          = cl_gui_frontend_services=>filetype_all
    CHANGING
      filename             = file_name
      path                 = path
      fullpath             = full_path
      user_action          = user_action
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  CHECK user_action = cl_gui_frontend_services=>action_ok.
  output = full_path.
  FORM upload USING l_filename TYPE localfile CHANGING source TYPE xstring.
  DATA:    l_rawtab   TYPE STANDARD TABLE OF raw255,
           l_len      TYPE i,
           filename   TYPE string.
  filename = l_filename.
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                = filename
      filetype                = 'BIN'
    IMPORTING
      filelength              = l_len
    CHANGING
      data_tab                = l_rawtab
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      not_supported_by_gui    = 17
      error_no_gui            = 18
      OTHERS                  = 19.
  IF sy-subrc IS NOT INITIAL.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    EXPORTING
      input_length = l_len
    IMPORTING
      buffer       = source
    TABLES
      binary_tab   = l_rawtab
    EXCEPTIONS
      failed       = 1
      OTHERS       = 2.
  IF sy-subrc IS NOT INITIAL.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  ENDFORM.
  FORM assign_right CHANGING lv_pdf.
  DATA: l_fp TYPE REF TO if_fp,
        l_pdfobj TYPE REF TO if_fp_pdf_object,
        l_dest TYPE rfcdest,
        error TYPE string,
        cx_obj TYPE REF TO cx_fp_runtime_system.
  MOVE cl_fp=>get_ads_connection( ) TO l_dest.
  l_fp = cl_fp=>get_reference( ).
  l_pdfobj = l_fp->create_pdf_object( connection = l_dest ).
  l_pdfobj->set_document( pdfdata = lv_pdf ).
  DATA: lt_rights TYPE TFPURIGHT.
  DATA: ls_rights LIKE LINE OF lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>formrights.
  ls_rights-value = if_fp_pdf_usage_rights=>FORMRIGHT_FILLIN.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>formrights.
  ls_rights-value = if_fp_pdf_usage_rights=>FORMRIGHT_IMPORT.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>formrights.
  ls_rights-value = if_fp_pdf_usage_rights=>FORMRIGHT_EXPORT.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>formrights.
  ls_rights-value = if_fp_pdf_usage_rights=>FORMRIGHT_ADD.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>formrights.
  ls_rights-value = if_fp_pdf_usage_rights=>FORMRIGHT_DELETE.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>formrights.
  ls_rights-value = if_fp_pdf_usage_rights=>FORMRIGHT_ONLINE.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>DOCRIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>DOCRIGHT_FULLSAVE.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>EMBEDFILERIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>EMBEDFILERIGHT_CREATE.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>EMBEDFILERIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>EMBEDFILERIGHT_DELETE.
  ls_rights-right = if_fp_pdf_usage_rights=>EMBEDFILERIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>EMBEDFILERIGHT_MODIFY.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>EMBEDFILERIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>EMBEDFILERIGHT_IMPORT.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>ANNOTRIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>ANNOTRIGHT_CREATE.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>ANNOTRIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>ANNOTRIGHT_DELETE.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>ANNOTRIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>ANNOTRIGHT_MODIFY.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>ANNOTRIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>ANNOTRIGHT_COPY.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>ANNOTRIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>ANNOTRIGHT_IMPORT.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>ANNOTRIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>ANNOTRIGHT_EXPORT.
  INSERT ls_rights INTO TABLE lt_rights.
  ls_rights-right = if_fp_pdf_usage_rights=>ANNOTRIGHTS.
  ls_rights-value = if_fp_pdf_usage_rights=>ANNOTRIGHT_ONLINE.
  INSERT ls_rights INTO TABLE lt_rights.
  TRY.
    l_pdfobj->set_usagerights( rights = lt_rights ).
    l_pdfobj->execute( ).
    l_pdfobj->get_document( IMPORTING pdfdata = lv_pdf ).
  CATCH cx_fp_runtime_system INTO cx_obj.
    error = cx_obj->get_text( ).
    MESSAGE error TYPE 'E'.
  ENDTRY.
  ENDFORM.
  FORM download USING source TYPE xstring file TYPE localfile.
  TYPES:  ty_bin_data(1024) TYPE x .                        "#EC NEEDED
  TYPES:  tt_bin_data       TYPE STANDARD TABLE OF ty_bin_data."#EC NEEDED
  DATA: lp_data TYPE REF TO data.
  DATA: lf_bin_length TYPE i,
        filename      TYPE string,
        lf_pos        TYPE i,
        info          TYPE string,
        lf_n          TYPE i.
  FIELD-SYMBOLS: <fs_data>   TYPE any,
                 <fs_binary> TYPE x.
  DATA: et_binary_tab TYPE tt_bin_data,
        e_binary_size TYPE i.
* --------------------------------------------------------------------
  CLEAR et_binary_tab.
  e_binary_size = xstrlen( source ).
  CREATE DATA lp_data LIKE LINE OF et_binary_tab[].
  ASSIGN lp_data->* TO <fs_data>.
  ASSIGN COMPONENT 1 OF STRUCTURE <fs_data> TO <fs_binary> CASTING TYPE x.
  IF sy-subrc = 4.
    ASSIGN COMPONENT 0 OF STRUCTURE <fs_data> TO <fs_binary> CASTING TYPE x.
  ENDIF.
  DESCRIBE FIELD <fs_binary> LENGTH lf_bin_length IN BYTE MODE.
  lf_n = ( e_binary_size + lf_bin_length - 1 ) DIV lf_bin_length.
  DO lf_n TIMES.
    <fs_binary> = source+lf_pos.
    lf_pos = lf_pos + lf_bin_length.
    APPEND <fs_data> TO et_binary_tab.
  ENDDO.
  filename = file.
  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      bin_filesize              = e_binary_size
      filename                  = filename
      filetype                  = 'BIN'
    CHANGING
      data_tab                  = et_binary_tab
    EXCEPTIONS
      file_write_error          = 1
      no_batch                  = 2
      gui_refuse_filetransfer   = 3
      invalid_type              = 4
      no_authority              = 5
      unknown_error             = 6
      header_not_allowed        = 7
      separator_not_allowed     = 8
      filesize_not_allowed      = 9
      header_too_long           = 10
      dp_error_create           = 11
      dp_error_send             = 12
      dp_error_write            = 13
      unknown_dp_error          = 14
      access_denied             = 15
      dp_out_of_memory          = 16
      disk_full                 = 17
      dp_timeout                = 18
      file_not_found            = 19
      dataprovider_exception    = 20
      control_flush_error       = 21
      not_supported_by_gui      = 22
      error_no_gui              = 23
      OTHERS                    = 24.
    IF sy-subrc = 0.
      info = 'File:' && filename && 'download successuflly!'.
      MESSAGE info TYPE 'I'.
    ELSE.
      info = 'File:' && filename && 'download error,code=' && sy-subrc.
      MESSAGE info TYPE 'E'.
    ENDIF.
  ENDFORM.
相关文章
|
4天前
|
存储 传感器 编解码
CVPR 2023 最全分割类论文整理:图像/全景/语义/实例分割等【附PDF+代码】
CVPR 2023 最全分割类论文整理:图像/全景/语义/实例分割等【附PDF+代码】
|
24天前
|
XML 持续交付 开发工具
SAPGUI 里 ABAP 代码导出成 PDF 格式的隐藏小技巧
SAPGUI 里 ABAP 代码导出成 PDF 格式的隐藏小技巧
15 0
|
4月前
|
BI 数据安全/隐私保护
SAP ABAP 生成 PDF 的几种技术综述
SAP ABAP 生成 PDF 的几种技术综述
75 0
|
2月前
有关文件转pdf的代码示例
有关文件转pdf的代码示例
37 0
|
2月前
|
安全 程序员 Python
PDF转Word,1行Python代码就够了,免费用
PDF转Word,1行Python代码就够了,免费用
|
4月前
|
XML 存储 Java
使用 ABAP 调用 Adobe Document Service 生成 PDF 文档
使用 ABAP 调用 Adobe Document Service 生成 PDF 文档
48 0
|
1月前
|
Java API Apache
使用 Apache PDFBox 操作PDF文件
Apache PDFBox库是一个开源的Java工具,专门用于处理PDF文档。它允许用户创建全新的PDF文件,编辑现有的PDF文档,以及从PDF文件中提取内容。此外,Apache PDFBox还提供了一些命令行实用工具。
39 6
|
2月前
|
JavaScript 前端开发
nodejs实现解析chm文件列表,无需转换为PDF文件格式,在线预览chm文件以及目录,不依赖任何网页端插件
nodejs实现解析chm文件列表,无需转换为PDF文件格式,在线预览chm文件以及目录,不依赖任何网页端插件
|
22天前
|
编解码 数据可视化 数据挖掘
【办公自动化】用Python将PDF文件转存为图片
【办公自动化】用Python将PDF文件转存为图片
33 1
|
23天前
|
数据安全/隐私保护 Python Windows
Python办公自动化【Word转换PDF、PDF读取内容、PDF合并文件、PDF拆分文件、PDF加密文件、PPT基本操作-增加幻灯片、增加内容】(六)-全面详解(学习总结---从入门到深化)
Python办公自动化【Word转换PDF、PDF读取内容、PDF合并文件、PDF拆分文件、PDF加密文件、PPT基本操作-增加幻灯片、增加内容】(六)-全面详解(学习总结---从入门到深化)
19 0

热门文章

最新文章