This taxation applies to both residents and non-residents. More about this in this file.
Since this concerns a specific matter, it is strongly recommended to ask for the assistance of your bank, Spanish lawyer, and other accountants.
We explain this tax system here: