*&---------------------------------------------------------------------*
*& Report ZALTCHUNK
*&---------------------------------------------------------------------*
*& Report demonstrates altChunk usage in ABAP
*& Pavol Olejar 23.4.2017
*&---------------------------------------------------------------------*
REPORT zaltchunk.
DATA: lr_merge1 TYPE REF TO cl_docx_document,
lr_merge2 TYPE REF TO cl_docx_document,
lr_merge3 TYPE REF TO cl_docx_document,
lr_final TYPE REF TO zcl_docx_document,
lr_main TYPE REF TO zcl_docx_maindocumentpart,
lr_altpart1 TYPE REF TO cl_docx_alternativeformatpart,
lr_altpart2 TYPE REF TO cl_docx_alternativeformatpart,
lr_altpart3 TYPE REF TO cl_docx_alternativeformatpart,
docx TYPE xstring,
mainx TYPE xstring,
lv_id TYPE string,
s TYPE string,
lv_current_chunk TYPE string,
lv_replace TYPE string,
lv_length TYPE i,
lt_data_tab TYPE STANDARD TABLE OF x255.
* READ final document. Note we are using z-class.
PERFORM load_file USING 'C:\final.docx'
CHANGING docx.
lr_final = zcl_docx_document=>load_document( iv_data = docx ).
lr_main = lr_final->get_maindocumentpart( ).
* ADD alternative parts
lr_altpart1 = lr_main->add_alternativeformatpart( iv_content_type = cl_docx_alternativeformatpart=>co_content_type_word ).
lr_altpart2 = lr_main->add_alternativeformatpart( iv_content_type = cl_docx_alternativeformatpart=>co_content_type_word ).
lr_altpart3 = lr_main->add_alternativeformatpart( iv_content_type = cl_docx_alternativeformatpart=>co_content_type_word ).
* Read document to be merged/inserted
PERFORM load_file USING 'C:\merge1.docx'
CHANGING docx.
* Provide data to store in alternative part
lr_altpart1->feed_data( iv_data = docx ).
* REPEAT for 2nd and 3rd file
PERFORM load_file USING 'C:\merge2.docx'
CHANGING docx.
lr_altpart2->feed_data( iv_data = docx ).
PERFORM load_file USING 'C:\merge3.docx'
CHANGING docx.
lr_altpart3->feed_data( iv_data = docx ).
* Get xml of main part to insert altChunk tags using string operations
mainx = lr_main->get_data( ).
CALL FUNCTION 'CRM_IC_XML_XSTRING2STRING'
EXPORTING
inxstring = mainx
IMPORTING
outstring = s.
lv_id = lr_main->get_id_for_part( lr_altpart1 ).
CONCATENATE '<w:altChunk r:id="' lv_id '" />' INTO lv_current_chunk.
lv_id = lr_main->get_id_for_part( lr_altpart2 ).
CONCATENATE lv_current_chunk '<w:altChunk r:id="' lv_id '" />' INTO lv_current_chunk.
lv_id = lr_main->get_id_for_part( lr_altpart3 ).
CONCATENATE lv_current_chunk '<w:altChunk r:id="' lv_id '" />' INTO lv_current_chunk.
* Prepare alt chunk tags
CONCATENATE '<w:body>'
lv_current_chunk
'</w:body>' INTO lv_replace.
* Replace body tag
REPLACE FIRST OCCURRENCE OF REGEX '<w:body>.*</w:body>' IN s WITH lv_replace.
CALL FUNCTION 'CRM_IC_XML_STRING2XSTRING'
EXPORTING
instring = s
IMPORTING
outxstring = mainx.
* Provide new main part with alt chunk tags and save document
lr_main->feed_data( iv_data = mainx ).
docx = lr_final->get_package_data( ).
lv_length = xstrlen( docx ).
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = docx
TABLES
binary_tab = lt_data_tab.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = lv_length
filename = 'C:\final_new.docx'
filetype = 'BIN'
confirm_overwrite = 'X'
CHANGING
data_tab = lt_data_tab.
FORM load_file USING path TYPE string
CHANGING docx TYPE xstring.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = path
filetype = 'BIN'
IMPORTING
filelength = lv_length
CHANGING
data_tab = lt_data_tab
EXCEPTIONS
OTHERS = 19.
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_length
IMPORTING
buffer = docx
TABLES
binary_tab = lt_data_tab
EXCEPTIONS
OTHERS = 2.
ENDFORM.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
3 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |